【问题标题】:cakephp HABTM associationcakephp HABTM 协会
【发布时间】:2012-09-04 22:45:47
【问题描述】:

您好,我有一个用 cakephp 开发的网站。 我有两张桌子: 成分 属性

表之间的关系是 HasAndBelongsToMany(HABTM)。 这是我的模型:

class Ingredient extends AppModel {
    public $name = 'Ingredient';
    public $useTable = 'ingredients';
    public $belongsTo = 'User';

    public $hasAndBelongsToMany = array (
        'Property' => array (
            'className'             => 'Property',
            'joinTable'             => 'ingredients_properties',
            'foreignKey'            => 'ingredient_id',
            'associationForeignKey' => 'property_id',
            'unique'                => false
        )
    );

}

class Property extends AppModel{
        public $name = 'Property'; 
        public $useTable = 'properties';

}

在我的成分控制器中,我必须添加一个我在这种模式下尝试过的新属性:

$this->Ingredient->Property->create();
                $this->Ingredient->Property->set('user_id',$this->Session->read('Auth.User.id'));
                $this->Ingredient->Property->set('ingredient_id',$ing['IngredientAlias']['ingredient_id']);
                if ($this->Ingredient->Property->save($this->request->data)){
                    $this->set('flash_element','success');
                    $this->Session->setFlash ('Ingrediente modificato con successo.');
                    $this->redirect(array('action'=>'edit',$alias));
                }
                else{
                    $this->set('flash_element','error');
                    $this->Session->setFlash('Errore di salvataggio activity');
                }

在数据库中插入一条新记录,但是如果我想在成分属性(连接表)中插入一条记录的成分之间的关​​系,我该怎么做?在这种模式下,就像 Ingredients hasMany Properties 但不正确。 我该如何解决? 还是我必须手动将记录创建到成分属性中?

【问题讨论】:

    标签: cakephp many-to-many cakephp-model


    【解决方案1】:

    【讨论】:

    • 我已经用这个链接解决了,因为错误在属性模型中我也必须插入 HABTM 关系
    【解决方案2】:

    解决办法是:

    class Ingredient extends AppModel {
        public $name = 'Ingredient';
        public $useTable = 'ingredients';
        public $belongsTo = 'User';
    
        public $hasAndBelongsToMany = array (
            'Property' => array (
                'className'             => 'Property',
                'joinTable'             => 'ingredients_properties',
                'foreignKey'            => 'ingredient_id',
                'associationForeignKey' => 'property_id',
                'unique'                => false
            )
        );
    
    }
    
    class Property extends AppModel{
            public $name = 'Property'; 
            public $useTable = 'properties';
            public $hasAndBelongsToMany = array (
            'Ingredient' => array (
                'className'             => 'Ingredient',
                'joinTable'             => 'ingredients_properties',
                'foreignKey'            => 'property_id',
                'associationForeignKey' => 'ingredient_id',
                'unique'                => false
            )
        );
    
    }
    

    【讨论】:

    • 如果表ingredientsproperties在不同的数据库中,连接表在哪个数据库中是否重要?如何指定连接表的位置或蛋糕足够聪明以检查两个数据库?
    • 我认为如果你在不同的数据库中有表,你必须手动建立这种关系。阅读这篇文章:sanisoft.com/blog/2011/04/04/…@Scott
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多