cakephp外键不是主键

5 php cakephp cakephp-model

我有一个在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        
Run Code Online (Sandbox Code Playgroud)

行动

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    
Run Code Online (Sandbox Code Playgroud)

我想将两个表与字段"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'
        )
    );
Run Code Online (Sandbox Code Playgroud)

}

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'
            )
        );
    }
Run Code Online (Sandbox Code Playgroud)

它不会检索正确的数据..它似乎需要外键的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
}
?>
Run Code Online (Sandbox Code Playgroud)

控制器是一个简单的查询,查找全部和递归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);
Run Code Online (Sandbox Code Playgroud)

请帮帮我

Aru*_*ain 2

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

\n\n

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

\n\n

即使您尝试使用 type_id 作为“actions”表中的外键,那么 type_id 在您的 Activity_ingredients 表中也必须是唯一的,如果是这样,那么您可以将 ActivityIngredient 模型定义为:

\n\n
class ActivityIngredients extends AppModel{\n    public $primaryKey = \'type_id\';\n    public $name = \'ActivityIngredients\'; //non utilizzata nel sito \xc3\xa8 il nome del modello alla fine per migliorare la compatibilit\xc3\xa0\n\n    public $belongsTo = array(\n        \'Activity\' => array(\n            \'className\'     => \'Activity\',\n            \'conditions\'    => \'\',\n            \'order\'         => \'\',\n            \'foreignKey\'    => \'activity_id\'\n        ),\n        \'Ingredient\' => array(\n            \'className\'     => \'Ingredient\',\n            \'conditions\'    => \'\',\n            \'order\'         => \'\',\n            \'foreignKey\'    => \'ingredient_id\'\n        )\n    );\n\n    public $hasMany = array(\n        \'Action\' => array(\n            \'className\' => \'Action\',\n            \'conditions\' => \'\',\n            \'dependent\' => true,\n            \'foreignKey\'   => \'type_id\',\n            \'associatedKey\'   => \'type_id\'\n        )\n    );\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

即使您不同意将“type_id”定义为表中的外键。那么这段代码将非常适合您的情况。

\n\n
class ActivityIngredients extends AppModel{\npublic $name = \'ActivityIngredients\'; //non utilizzata nel sito \xc3\xa8 il nome del modello alla fine per migliorare la compatibilit\xc3\xa0\n\npublic $belongsTo = array(\n    \'Activity\' => array(\n        \'className\'     => \'Activity\',\n        \'conditions\'    => \'\',\n        \'order\'         => \'\',\n        \'foreignKey\'    => \'activity_id\'\n    ),\n    \'Ingredient\' => array(\n        \'className\'     => \'Ingredient\',\n        \'conditions\'    => \'\',\n        \'order\'         => \'\',\n        \'foreignKey\'    => \'ingredient_id\'\n    )\n);\n\npublic $hasMany = array(\n    \'Action\' => array(\n        \'className\' => \'Action\',\n        \'conditions\' => \'\',\n        \'dependent\' => true,\n        \'foreignKey\'   => false,\n        \'finderQuery\'   => \'select * from actions as `Action` where\n                            `Action`.`type_id` = {$__cakeID__$} \'\n    )\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n

}

\n\n

我相信这会给您带来想要的结果。请询问它是否不适合您。

\n