2012-11-30 3 views
1

프로그래밍 방식으로 $ many_many/$ belongs_many_many 관계로 다른 하나에 DataObject를 추가하려고합니다. Silverstripe-3은 그 작업에 문제가있는 것 같습니다.Silverstripe S3 : many_many 관계로 추가하는 DataObject

//Object 1 
<?php 
class ProductSubCategory extends DataObject { 

    static $db = array( 
     'Name' => 'Text', 
     'Description' => 'Text', 
     'RemoteIndexId'=>'varchar', 
     'LegalName' => 'Text', 
     'CodeName' => 'Text' 
); 
static $many_many = array('Ingredients'=>'Ingredient'); 

function addIngredients($ingredientsArray){ 

    foreach($ingredientsArray as $k=>$value){ 
     $newIngredient = new Ingredient(); 
     $newIngredient->RemoteIndexId = $value->id; 
     $newIngredient->Name = $value->name; 
     $newIngredient->CodeName = $value->code_name; 
     $newIngredient->Description = $value->description;     
     $this->Ingredients()->add($newIngredient); 
     } 
    } 

} 
//Object 2 
    <?php 
    class Ingredient extends DataObject { 
    static $db = array( 
     'Name' => 'Text', 
     'RemoteIndexId'=>'Varchar', 
     'ScientificName' => 'Text', 
     'Description'=>'Text', 
     'Percentage'=>'Varchar', 
     'CodeName'=>'Varchar' 
    ); 

    static $belong_many_many = array('ProductSubCategory' => 'ProductSubCategory'); 


    //....(some fields for the UI)... 
    } 

문제 설명 : 1. 성분이 데이터베이스에 기록되지 않습니다. 2. 테이블 ProductSubCategory_Ingredient 레코드를 얻을 수 있지만,이 ProductSubCategoryID 만 ID를 포함, IngredientID하지 3. 내가
이 도와주세요 아무 소용로, 지금은 일에 대한 솔루션을 둘러보고있다

NO 오류 메시지!

답변

0

이 옳다 : 여기

static $belongs_many_many = array('ProductSubCategory' => 'ProductSubCategory'); 

가 SS3에서 다 대다 관계에 좋은 자원이다 , 's'가 포함 된 $belongs_many_many이어야하며

으로 전화해야합니다.

$this->write(null, null, null, true);

foreach 루프 이후에는 일들이 데이터베이스에 기록됩니다. http://api.silverstripe.org/3.0/framework/model/DataObject.html#methodwrite