我正在尝试以编程方式将一个DataObject添加到另一个$many_many / $belong_many_many关系中。Silverstripe-3似乎在这项任务中遇到了问题。
//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.没有错误消息
我已经四处寻找了几天的解决方案,但都没有结果。
请帮帮我!
发布于 2012-12-01 08:15:56
您的变量名中有一个拼写错误。belongs中缺少s。
这一点:
static $belong_many_many = array('ProductSubCategory' => 'ProductSubCategory');应该是这样:
static $belongs_many_many = array('ProductSubCategory' => 'ProductSubCategory');这里有一个关于SS3中多对多关系的很好的资源:
http://fake-media.com/2014/01/18/silverstripe-3-many-many-a-comprehensive-example/
发布于 2013-01-20 18:26:39
@3dg00是正确的,应该是带有“%s”的$belongs_many_many,而且您还必须调用
$this->write(null, null, null, true);
在foreach循环之后,这样就可以将内容写入数据库。http://api.silverstripe.org/3.0/framework/model/DataObject.html#methodwrite
https://stackoverflow.com/questions/13650948
复制相似问题