belongsTo cakephp关系不起作用
我是cakephp的新手,我学习了一些关于它的教程。现在,我正在尝试构建我的第一个应用程序,我被这种关系“belongsTo”绊住了。这并没有起到应有的作用,更有可能是我做错了什么。这是我的桌子、模型和控制器 这是一个不显示任何员工id或姓名的视图。这让我快疯了!!!。 位于app/View/Pendings/add.ctp下:belongsTo cakephp关系不起作用,php,cakephp,foreign-key-relationship,Php,Cakephp,Foreign Key Relationship,我是cakephp的新手,我学习了一些关于它的教程。现在,我正在尝试构建我的第一个应用程序,我被这种关系“belongsTo”绊住了。这并没有起到应有的作用,更有可能是我做错了什么。这是我的桌子、模型和控制器 这是一个不显示任何员工id或姓名的视图。这让我快疯了!!!。 位于app/View/Pendings/add.ctp下: <div class='form'> <?php echo $this->Form->create('Pending'); ?&g
<div class='form'>
<?php echo $this->Form->create('Pending'); ?>
<fieldset>
<legend>Nueva Tarea</legend>
<?php
echo $this->Form->input('subject', array('label' => 'Asunto'));
echo $this->Form->input('body', array('label' => 'Descripcion', 'rows' => '3'));
echo $this->Form->input('staff_id', array('label' => 'Asignar a miembro'));
?>
</fieldset>
<?php echo $this->Form->end('Asignar'); ?>
</div>
我想我遵循了cakephp惯例。我真的看不出我的错误。非常感谢你的帮助
抱歉,这是控制器app/controller/PendingsController.php:
蛋糕的魔力。。。有条件的 您的想法是正确的,因为模型关系设置正确,它应该。。。工作但是您缺少一个部分:将列表传递给视图。那部分并不神奇 在输入开始之前,阅读几乎所有的内容 如果要在使用belongsTo-或hasOne-关系时创建选择字段,可以添加以下[…] 现在把你应该放的东西放进去
$this->set('staffs', $this->Pending->Staff->find('list'));
备注:注意变量名,是复数形式的模型。然后你给这个变量加上一个列表
在视图中,你做你正在做的事情
echo $this->Form->input('staff_id', array('label' => 'Asignar a miembro'));
你应该做到:
[编辑]
刚刚看了你的最后一次编辑。你失踪了
$this->set('staffs', $staffs);
//or
$this->set(compact('staffs'));
是否希望视图显示可用于链接新挂起的员工的列表或类似列表?我对你的理解正确吗?如果是的话。。。嗯,您需要在控制器中显示相应的操作,因为我看不到您正在将该列表传递到任何位置的视图。是的,这是正确的,当我使用add创建一个新的挂起时,我希望通过显示一个带有其名称的select来为其分配人员。我认为你不必向动作传递列表,因为cake通过模型关系知道这个列表。但我要检查一下。谢谢努瑟!!
<?php
class PendingsController extends AppController{
public function index(){
$this->Pending->recursive = 0;
$this->set('pendings', $this->paginate());
}
public function add(){
if( $this->request->is('post') )
{
if( $this->Pending->save($this->request->data) )
{
$this->Session->setFlash('Tarea Asignada');
$this->redirect(array('action' => 'index'));
}
}
$staffs = $this->Pending->Staff->find('list');
}
}
?>
$this->set('staffs', $this->Pending->Staff->find('list'));
echo $this->Form->input('staff_id', array('label' => 'Asignar a miembro'));
$this->set('staffs', $staffs);
//or
$this->set(compact('staffs'));