简体   繁体   English

多对多关系保存失败

[英]Many-To-Many relationship save fails

I have this Many-To-Many relation: 我有这种多对多关系:

M2M

These are my Models: 这些是我的模型:

Productvariant 产品变体

class Productvariant extends DataMapper {

    var $has_many = array('propertyvalue');
    var $has_one = array('product');

}

Propertyvalue 适当的价值

class Propertyvalue extends DataMapper {

    var $has_many = array('productvariant');
    var $has_one = array('property');

}

Controller 控制者

$productvariant = new Productvariant(1);
$prodval = new Propertyvalue(1);
$productvariant->save($prodval);

Message 信息

Unable to relate productvariant with propertyvalue.

Only thing I can find in the documentation is self Many-To-Many relationship and I seem to misread how they want you to use the models in that fashion. 我在文档中唯一能找到的就是自我多对多关系,而且我似乎误解了他们希望您如何以这种方式使用模型。

Do I need to define a model for the extra table too? 我是否还需要为额外的表定义模型?

========================== =========================

UPDATE 更新

I made an in-between Model for Many-To-Many relationship; 我建立了一个“多对多”关系模型。

Model 模型

class Productvariant_propertyvalues extends DataMapper {

    var $table = '__productvariants_propertyvalues';

    var $has_one = array('productvariant', 'propertyvalue');

}

Controller 控制者

$productvariant = new Productvariant(1);
$propval = new Propertyvalue(1);

$pv_vals = new Productvariant_propertyvalues();
$pv_vals->save(array($productvariant, $propval));

It works now, but shouldn't this be doable without the extra Model ? 现在可以使用,但是如果没有额外的Model这不应该可行吗?

You either have a many-to-many between Productvariant and Propertyvalue, and then you define them as you did. 您可以在Productvariant和Propertyvalue之间建立多对多的关系,然后按定义的方式进行定义。 Datamapper will automatically look for a junction table called "productvariants_propertyvalues", which contains the foreign keys to the two tables. Datamapper会自动查找一个称为“ productvariants_propertyvalues”的联结表,其中包含两个表的外键。

So your setup should work. 因此,您的设置应该可以正常工作。 They only issue that could pop up is that the CI plural() function that datamapper uses doesn't produce the correct table name, causing the not-found error to popup. 他们唯一可能弹出的问题是datamapper使用的CI plural()函数不能产生正确的表名,从而导致未找到的错误弹出。

If you create an in-between model, you split the many-to-many into two one-to-many's. 如果创建中间模型,则将多对多拆分为两个一对多。 Not a problem, but whether or not you need it depends on your application design. 这不是问题,但是是否需要取决于您的应用程序设计。

edit: I now see you have prefixed the table name with a double underscore. 编辑:我现在看到您在表名的前面加上了双下划线。 Datamapper will not generate that, so that is why the junction table can not be found. Datamapper不会生成该数据,因此这就是找不到联结表的原因。 Either remove those, or switch to an advanced relationship definition in which you define the "join_table" manually. 要么删除这些,要么切换到高级关系定义,您可以在其中手动定义“ join_table”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM