cakePhp 和外键:我无法在视图中写入外键
我的外键字段有问题:我使用 MySQLWorkbench
创建了一个 categories
表,该表通过 1:n
关系链接到另一个表:Articles
。它似乎有效,因为在 Articles
中,有一个 categories_id
"INT" 字段。
我烘烤了整个项目,一切正常,除了这个外键:而不是一个会接收“数字”的输入,我会编写指定categories_id
数字,有一种空的“选择” “(列表)不包含任何内容,因此我无法在数据库中的 categories_id
字段中输入任何数字:
这是图像:
如果我尝试“强制”并添加“文章”(因此没有类别),则会出现这个错误 :
错误:SQLSTATE[23000]:违反完整性约束:1452 无法添加或更新子行:外键约束失败 (
ecommerce
.articles
,CONSTRAINTfk_articles_categories
外键 (categories_id
) 参考categories
(id
)删除时不执行任何操作,更新时不执行任何操作)
这是我的 add.ctp
文件(它是为 categories_id
编写的 input
>,所以我不知道要更改什么):
<div class="articles form">
<?php echo $this->Form->create('Article');?>
<fieldset>
<legend><?php echo __('Add Article'); ?></legend>
<?php
echo $this->Form->input('nom');
echo $this->Form->input('prix');
echo $this->Form->input('categories_id');
?>
感谢您的帮助
编辑:
这是我添加的内容:
class Article extends AppModel {
var $belongsTo = array(
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id'
)
);
}
和:
class Category extends AppModel {
var $hasMany = array(
'Article' => array(
'className' => 'Article',
'foreignKey' => 'category_id'
)
);
}
以及在控制器中(add()
):
public function add() {
//$this->set('categories',$this->Article->Category->find('all'));
//$c = $this->Article->Category->find('all', array("recursive"=>-1, 'fields'=>array('id', 'nom')));
$c = $this->Article->Category->find('list');
$this->set('cat', $c);
但在我看来, pr($cat)
返回一个空数组...
<?php
echo $this->Form->input('nom');
echo $this->Form->input('prix');
echo $this->Form->input('category_id', array('option'=>$cat));
pr($cat);
?>
i've got a problem with a foreign key field : i created a categories
table with MySQLWorkbench
, which is linked by a 1:n
relation to another table : Articles
. It seems to work, because in Articles
, there is a categories_id
"INT" field .
I baked the whole project, everything works fine, except this foreign key : instead of an input that would receive the "number" i would write to specify the categories_id
number, there is a kind of empty "select" (list) that contains nothing, so i cannot enter any number for the categories_id
field in my database :
here's the image :
If i try to "force" and add an "article" (so without the category), there is this error :
Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
ecommerce
.articles
, CONSTRAINTfk_articles_categories
FOREIGN KEY (categories_id
) REFERENCEScategories
(id
) ON DELETE NO ACTION ON UPDATE NO ACTION)
And here's my add.ctp
file (it is written input
for the categories_id
, so i don't know what to change) :
<div class="articles form">
<?php echo $this->Form->create('Article');?>
<fieldset>
<legend><?php echo __('Add Article'); ?></legend>
<?php
echo $this->Form->input('nom');
echo $this->Form->input('prix');
echo $this->Form->input('categories_id');
?>
Thanks for your help
EDIT:
here's what i added :
class Article extends AppModel {
var $belongsTo = array(
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id'
)
);
}
and :
class Category extends AppModel {
var $hasMany = array(
'Article' => array(
'className' => 'Article',
'foreignKey' => 'category_id'
)
);
}
and in the controller (add()
) :
public function add() {
//$this->set('categories',$this->Article->Category->find('all'));
//$c = $this->Article->Category->find('all', array("recursive"=>-1, 'fields'=>array('id', 'nom')));
$c = $this->Article->Category->find('list');
$this->set('cat', $c);
but in my view, pr($cat)
returns an empty Array...
<?php
echo $this->Form->input('nom');
echo $this->Form->input('prix');
echo $this->Form->input('category_id', array('option'=>$cat));
pr($cat);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为命名约定..如果您的表名为
categories
,那么articles
中的外键应该是category_id
(单数)和模型必须称为类别
。控制器应该向视图传递一个名为“$categories”(复数)的变量$this->set('categories',$this->Article->Category->find('list') )
并且视图中的输入必须是:
echo $this->Form->input('category_id');
不要忘记更改外键也在模型中
希望这有帮助:)
It's because of the naming convention.. if your table is called
categories
then the foreign key inarticles
should becategory_id
(singular) and the model must be calledCategory
. The controller should pass to the view a variable called '$categories' (plurar)$this->set('categories',$this->Article->Category->find('list'))
and the input in the view has to be:
echo $this->Form->input('category_id');
don't forget to change the foreign key in the model too
Hope this helps :)