【问题标题】:cakephp relations modelcakephp关系模型
【发布时间】:2012-08-25 04:07:40
【问题描述】:

我有一个使用 CakePHP 2.0 开发的网站。 我有一个包含许多表的数据库,我想关联两个表。我已经这样做了很多次,但是有两张桌子我不能这样做,我不知道为什么。 这是我的两张桌子:

成分别名:

id             INT(10) UNSIGNED AUTO_INCREMENT 
ingredient_id  INT(10)          
user_id        INT(10)          
alias          VARCHAR(100) latin1_swedish_ci 

活性成分

id             INT(10) UNSIGNED     
activity_id    INT(11)      
ingredient_id  INT(10)          
created        DATETIME

ingredient_id 是我的外键,这是我的模型,我想将ingredient_id 设为我的外键。

class ActivityIngredients extends AppModel{
        public $name = 'ActivityIngredients'; 
        public $useTable = 'activity_ingredients';

        public $belongsTo = array(
            'IngredientAlias' => array(
                'className'     => 'IngredientAlias',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'ingredient_id'
            )
        );


    }

class IngredientAlias extends AppModel {
    public $name = 'IngredientAlias';
    public $useTable = 'ingredient_aliases';
    public $belongsTo = array(
        'Ingredient' => array(
            'className'    => 'Ingredient',
            'foreignKey'   => 'ingredient_id'
        ),
         'User' => array(
            'className'    => 'User',
            'foreignKey'   => 'user_id'
        )
    );

    public $hasMany = array (
        'ActivityIngredients' => array (
            'className'     => 'ActivityIngredients',
            'dependent' => true,
            'foreignKey'   => false,
            'associatedKey'   => 'ingredient_id'            
        )
    );

当我在IngredientAlias 中创建变量的 var_dump 时,什么都没有,是空的,就像它不带外键一样。为什么? 问题出在关系上? 我试着写了

'foreignKey'   => 'ingredient_id'

但没什么……一样

该查询是一个简单的 3 递归查询,我认为全选不是问题,而是与表的关系..

【问题讨论】:

    标签: sql cakephp cakephp-model


    【解决方案1】:

    hasMany/belongsTo 关系不是这样运作的。

    您当前的代码描述了以下关系:

    • ActiveIngredient 属于 IngredientAlias
    • IngredientAlias 有很多 ActiveIngredient

    这是从IngredientAliasActiveIngredient一对多关系。

    但是您的数据库架构描述了以下表关系:

    • ActiveIngredient 属于 Ingredient
    • Ingredient 有很多 ActiveIngredient
    • IngredientAlias 属于 Ingredient
    • Ingredient 有很多 IngredientAlias

    换句话说,从IngredientActiveIngredient一对多 关系,以及从Ingredient 到另一个一对多 关系IngredientAlias.

    因此,您的模型关系应该与数据库架构所描述的完全一致。 (此外,您的外键应该引用候选键。MySQL 允许您创建不引用候选键的外键约束,但我认为大多数其他数据库不允许这样做;因此 CakePHP 可能也不允许这样做.)

    最后,CakePHP 中的recursive 级别范围从-12(含)。没有recursive 级别的3。如果您只是将recursive 设置为2,那么ActiveIngredient 上的findAll 将返回其父Ingredient 和该父Ingredient 的子IngredientAliases


    编辑:
    recursive 在后台使用多个查询。然而,大多数时候,它会从你不需要的模型中获取大量不必要的数据。最好的办法是使用unbindModel() 删除不需要的关联,或者在可能的情况下将recursive 设置为-1use joins 以获取相关数据。

    第一个选项的另一个变体是使用the Containable behavior。这使您可以定义要返回的确切关系和字段。这通常是优化需要递归查询的查找的最简单方法。

    【讨论】:

    • 我明白了,现在我试图建立这种关系,但问题现在在 findAll 中,这是我的代码:$user = $this->User->findAll (array('id' =>$id)); $this->set('user', $user);
    • 并且可以使用特定的 id 来制作 findAll 吗?因为在我看来,我为 te Ingredient 传递了一个 id
    • @Alessandro:如果您想通过特定字段findAll(例如id),您可以使用findAllByFieldname(即findAllById($id))。但是,findAll 是为返回多个结果而设计的,因此使用 findAllById 而不是 findById 没有意义,因为 ID 是唯一的。 See the cookbook 了解如何使用这些方法。
    • 我已经完成了数据库之类的关系,但是当我创建 findAllById 时,我无法到达 IngredientAlias .. 因为 User 表中的查询与许多其他人相关.. 是一个非常大的查询来检索像 Profile Activity (ActivityIngredients, Ingredient,IngredientAlias) 这样的数据,这是代码:$this->User->recursive = 2; $user = $this->User->findAllById ($id); $this->set('user', $user);
    • 我不知道是否更好地进行更多查询或仅使用 findAllById 进行查询
    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多