簡體   English   中英

多對多關系保存失敗

[英]Many-To-Many relationship save fails

我有這種多對多關系:

M2M

這些是我的模型:

產品變體

class Productvariant extends DataMapper {

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

}

適當的價值

class Propertyvalue extends DataMapper {

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

}

控制者

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

信息

Unable to relate productvariant with propertyvalue.

我在文檔中唯一能找到的就是自我多對多關系,而且我似乎誤解了他們希望您如何以這種方式使用模型。

我是否還需要為額外的表定義模型?

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

更新

我建立了一個“多對多”關系模型。

模型

class Productvariant_propertyvalues extends DataMapper {

    var $table = '__productvariants_propertyvalues';

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

}

控制者

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

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

現在可以使用,但是如果沒有額外的Model這不應該可行嗎?

您可以在Productvariant和Propertyvalue之間建立多對多的關系,然后按定義的方式進行定義。 Datamapper會自動查找一個稱為“ productvariants_propertyvalues”的聯結表,其中包含兩個表的外鍵。

因此,您的設置應該可以正常工作。 他們唯一可能彈出的問題是datamapper使用的CI plural()函數不能產生正確的表名,從而導致未找到的錯誤彈出。

如果創建中間模型,則將多對多拆分為兩個一對多。 這不是問題,但是是否需要取決於您的應用程序設計。

編輯:我現在看到您在表名的前面加上了雙下划線。 Datamapper不會生成該數據,因此這就是找不到聯結表的原因。 要么刪除這些,要么切換到高級關系定義,您可以在其中手動定義“ join_table”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM