cakephp外键不是主键

时间:2012-07-19 21:32:31

标签: php cakephp cakephp-appmodel

我有一个在cakephp 2.0中开发的网站,我想在两个表之间建立关系:

activity_ingredients

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   activity_id     int(11)     No  None        
4   ingredient_id   int(10)     No  None        
5   created     datetime        

动作

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   language    char(2)     No  None        
4   text    varchar(100)        No  None        
5   created     datetime    

我想将两个表与字段“type_id”相关联。 我已经在这种模式下完成了我的代码:

    class Action extends AppModel{
    public $name = 'Action'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

    public $belongsTo = array(
        'ActivityIngredients' => array(
            'className'     => 'ActivityIngredients',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'type_id'
        )
    );

}

class ActivityIngredients extends AppModel{
        public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

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

        public $hasMany = array(
            'Action' => array(
                'className' => 'Action',
                'conditions' => '',
                'dependent' => true,
                'foreignKey'   => 'type_id',
                'associatedKey'   => 'type_id'
            )
        );
    }

它不会检索正确的数据..它似乎需要外键的id。 这是观点:

<?php foreach ($user['Activity'] as $activities) {
var_dump($activities);
?>
    <div class="line-cnt"><div class="line">
    </div>
</div>
<h2>Attività</h2>
<div class="table">
    <div>
        <div>Activity created</div><div><?php echo $activities['created']; ?>
        </div>
    </div>
    <div>
        <div>Actions text</div><div><?php echo $activities['Action']['text']; ?></div>
    </div>
    <div>
        <div>ActivityIngredient ingredient_id</div><div><?php echo $activities['ActivityIngredients']['ingredient_id']; ?></div>
    </div>
</div>
<?php
}
?>

控制器是一个简单的查询,查找全部并递归3到用户表格中的表格

$this->User->recursive = 3;
        $user = $this->User->read();

        if (empty($username) || $username != $user['User']['username']) {
            $this->redirect(array ('action'=>'view',$id,$user['User']['username']));
        }

        $this->set('user', $user);

请帮帮我

2 个答案:

答案 0 :(得分:2)

如果您在“activity_ingredients”表中使用“id”字段,那么首先应该将其用作另一个表中的foreignKey。

外键是关系表中与另一个表的候选键匹配的字段。

即使你试图在“actions”表中使用type_id作为外键,那么type_id在activity_ingredients表中必须是唯一的,如果是,那么你可以将ActivityIngredient模型定义为:

class ActivityIngredients extends AppModel{
    public $primaryKey = 'type_id';
    public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

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

    public $hasMany = array(
        'Action' => array(
            'className' => 'Action',
            'conditions' => '',
            'dependent' => true,
            'foreignKey'   => 'type_id',
            'associatedKey'   => 'type_id'
        )
    );
}

您的行动模型将保持不变。因此,您将能够获取所需的记录。

即使您不同意在表中将“type_id”定义为外键。那么这段代码可以很好地适应你的情况。

class ActivityIngredients extends AppModel{
public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

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

public $hasMany = array(
    'Action' => array(
        'className' => 'Action',
        'conditions' => '',
        'dependent' => true,
        'foreignKey'   => false,
        'finderQuery'   => 'select * from actions as `Action` where
                            `Action`.`type_id` = {$__cakeID__$} '
    )
);

}

我相信这会给你想要的结果。请问它是否对你不起作用。

答案 1 :(得分:-1)

在cakephp中,您可以指定primary key of a model。我相信你也可以把classname放在外键关联中。例如

'foreignKey'   => 'Classname.type_id'