首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Silverstripe S3: DataObject在many_many关系中添加

Silverstripe S3: DataObject在many_many关系中添加
EN

Stack Overflow用户
提问于 2012-12-01 02:25:52
回答 2查看 2.4K关注 0票数 1

我正在尝试以编程方式将一个DataObject添加到另一个$many_many / $belong_many_many关系中。Silverstripe-3似乎在这项任务中遇到了问题。

代码语言:javascript
复制
//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.没有错误消息

我已经四处寻找了几天的解决方案,但都没有结果。

请帮帮我!

EN

回答 2

Stack Overflow用户

发布于 2012-12-01 08:15:56

您的变量名中有一个拼写错误。belongs中缺少s

这一点:

代码语言:javascript
复制
static $belong_many_many = array('ProductSubCategory' => 'ProductSubCategory');

应该是这样:

代码语言:javascript
复制
static $belongs_many_many = array('ProductSubCategory' => 'ProductSubCategory');

这里有一个关于SS3中多对多关系的很好的资源:

http://fake-media.com/2014/01/18/silverstripe-3-many-many-a-comprehensive-example/

票数 1
EN

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13650948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档