【问题标题】:cakephp hasmany relations give errorcakephp hasmany 关系给出错误
【发布时间】:2012-07-24 22:37:19
【问题描述】:

我有一个包含一些关系的数据库。 在这个例子中我有两个表:

activity_ingredients   ingredient_aliases
---------------        ---------------
id                     id
ingredient_id          ingredient_id

我想在我的模型中设置一个条件,以通过字段“ingredient_id”连接表。我已经在这种模式下完成了:

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

        public $hasMany = array (
        'IngredientAlias' => array (
            'className'     => 'IngredientAlias',
            'foreignKey'   => false,
            'conditions' => array('ActivityIngredients.ingredient_id = IngredientAlias.ingredient_id')
        )
        );
    }   

class IngredientAlias extends AppModel {

    public $name = 'IngredientAlias';
    public $useTable = 'ingredient_aliases';
    public $belongsTo = array(
         'ActivityIngredients' => array(
            'className'    => 'ActivityIngredients',
            'foreignKey'   => false
        )

    );  
} 

如果我写这个代码给我一个这样的错误:

找不到列:1054“where 子句”中的未知列“ActivityIngredients.ingredient_id”

但是,如果我写 hasOne 而不是 hasMany,它不会返回任何错误并且是完美的。但我的关系是 hasMany.. 为什么?

为了检索数据,我对我的 UserController 进行了查询。 User 与 Activity 用 hasMany 关联,Activity 与 ActivityIngredients 在这种模式下关联:

public $hasOne = array(
            'ActivityIngredients' => array(
                'className' => 'ActivityIngredients',
                'conditions' => '',
                'dependent' => true,
                'foreignKey'   => 'activity_id'
            )

要检索数据,我目前正在使用此查询,但我认为我现在必须更改所有查询

$this->User->Activity->recursive = 2;
        $activity = $this->User->Activity->findAllByUser_id($id);
        $this->set('activity', $activity);

【问题讨论】:

    标签: cakephp cakephp-model


    【解决方案1】:

    在定义模型类时,首先应该使用单数词。这意味着模型名称应该是ActivityIngredient,而不是ActivityIngredients

    虽然如果 activity_ingredients 表中如果ingredient_id id 是属于ingredient_aliases 表的外键。那么它应该被命名为ingredient_alias_id。类似地,如果您在ingredient_aliases 表中执行相同操作,即外键名称为activity_ingredient_id 那么完全没有问题。

    但是,我正在为您的问题提供解决方案。请检查并验证。

    那么你的代码应该是这样的:

    class ActivityIngredient extends AppModel{
        public $name = 'ActivityIngredient'; 
        public $useTable = 'activity_ingredients'; // if you use singular term then no need to define $useTable
    
        public $hasMany = array (
        'IngredientAlias' => array (
            'className'     => 'IngredientAlias',
            'foreignKey'   => false,
            'finderQuery' => 'select * from ingredient_aliases as `IngredientAlias`
                              where `IngredientAlias`.`ingredient_id` = {$__cakeID__$}'
                                   )
        );
    }   
    
    class IngredientAlias extends AppModel {
    
        public $name = 'IngredientAlias';
        public $useTable = 'ingredient_aliases';
        public $belongsTo = array(
                          'ActivityIngredient' => array(
                                       'className'    => 'ActivityIngredient',
                                       'foreignKey'   => 'ingredient_id' 
                                                       )
                                 );  
    } 
    

    【讨论】:

    • 使用此代码不检索数据,成分别名为空不采取正确的关联...
    • 有没有收到错误,如果没有请检查activity_ingredients表中是否有component_id对应的记录。
    • 没有错误,但对我来说现在问题出在对我的控制器的查询中。因为我在用户的控制器中使用了这个表,所以用户是与 Activity 的关系,Activity 与 ActivityIngredients 和 ActivityIngredients 与 IngredientAlias。我已经在这种模式下完成了我的查询:但是你的模式对我来说是不正确的,对吧? $this->User->Activity->recursive = 2; $activity = $this->User->Activity->findAllByUser_id($id); $this->set('activity', $activity);
    • 是的......好的,但你没有在你的问题中提到它。没有问题。 Activity 模型如何与 ActivityIngredient 模型相关联?请编辑您的问题并解释整个场景。
    • 我已经用更多信息编辑了我的问题,希望现在可以完全解决我的问题
    【解决方案2】:

    你用过

    conditions' => array('ActivityIngredients.ingredient_id = IngredientAlias.ingredient_id')

    应该是

    conditions' => array('ActivityIngredients.ingredient_id => IngredientAlias.ingredient_id')

    使用下面的代码

                public $hasMany = array (
                'IngredientAlias' => array (
                    'className'     => 'IngredientAlias',
                    'foreignKey'   => false,
                    conditions' => array('ActivityIngredients.ingredient_id => IngredientAlias.ingredient_id')
                )
                );
            }   
    
        class IngredientAlias extends AppModel {
    
            public $name = 'IngredientAlias';
            public $useTable = 'ingredient_aliases';
            public $belongsTo = array(
                 'ActivityIngredients' => array(
                    'className'    => 'ActivityIngredients',
                    'foreignKey'   => false
                )
    
            );  
        }
        ?>
    

    【讨论】:

    • 给我一个错误:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '=> IngredientAlias.ingredient_id' 附近使用正确的语法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多