belongsTo cakephp关系不工作


belongsTo cakephp relation not working

我是cakephp的新手,我遵循了一些教程。现在我试图构建我的第一个应用程序,我被这个关系'belongsTo'卡住了。它没有正常工作,更有可能是我做错了什么。这是我的表,模型和控制器。

这个视图不显示任何人员id或名称。我都快疯了!!位于app/View/pending/add.ctp:

<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>  

其中staff_id是此模型Pending的外键。和对应于staff表中的id。我尝试做的是Staff hasMany Pending和Pending belongsTo Staff。因为Pending是一种待定任务。这就是为什么staff有很多未决事项。

这是挂起模型,在app/model/Pending.php中使用'belongsTo'关系:

<?php
class Pending extends AppModel {
    public $validate = array(
        'subject' => array(
            'rule' => 'notEmpty', 
            'message' => 'Debe ingresar un Asunto.'
        ),
        'body' => array(
            'rule' => 'notEmpty', 
            'message' => 'Debe ingresar una Descripción.'
        )
    );
    public $belongsTo = array(  
            'Staff' => array(
                    'className' => 'Staff',
                    'foreignKey' => 'staff_id'
                    )           
            ); 
}
?>

这是Staff模型,在app/model/Staff.php中使用'hasMany'关系:

<?php
class Staff extends AppModel {
    public $displayField = 'name';
    public $validate = array(
        'last_name' => array(
            'rule' => 'notEmpty'
        ),
        'name' => array(
            'rule' => 'notEmpty'
        ),
        'passwd' => array(
            'rule' => 'notEmpty'
        ),
        'email' => array(
        'rule' => 'notEmpty'
        )
    );
    public $hasMany = array( 
                    'Pending' => array( 
                        'className' => 'Pending',
                        'foreignKey' => 'staff_id'
                        )
                    );
}
?>

最后是我的两张桌子,员工和待定的

mysql> describe staffs;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| name      | varchar(40)      | NO   |     | NULL    |                |
| last_name | varchar(40)      | NO   |     | NULL    |                |
| email     | varchar(40)      | NO   |     | NULL    |                |
| password  | varchar(20)      | YES  |     | NULL    |                |
| active    | tinyint(1)       | YES  |     | 1       |                |
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| created   | datetime         | YES  |     | NULL    |                |
| modified  | datetime         | YES  |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
mysql> describe pendings;
+----------+------------------------------------+------+-----+---------+----------------+
| Field    | Type                               | Null | Key | Default | Extra          |
+----------+------------------------------------+------+-----+---------+----------------+
| id       | int(10) unsigned                   | NO   | PRI | NULL    | auto_increment |
| staff_id | int(10) unsigned                   | YES  | MUL | NULL    |                |
| created  | datetime                           | NO   |     | NULL    |                |
| pdate    | datetime                           | YES  |     | NULL    |                |
| subject  | varchar(250)                       | YES  |     | NULL    |                |
| body     | tinytext                           | YES  |     | NULL    |                |
| state    | enum('pending','done','reasigned') | YES  |     | pending |                |
+----------+------------------------------------+------+-----+---------+----------------+

我想我遵循了cakephp的约定。我真的看不出我的错误。谢谢你的帮助。

对不起,这里是控制器(app/controller/PendingsController.php):

<?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');
    }
}
?>

蛋糕的神奇作品…

你的想法是正确的,也许,因为模型关系设置正确,它应该只是…工作。但是您遗漏了一个部分:将列表传递给视图。那部分并不神奇。

阅读文档的这一部分(几乎一直到inputs开始)

如果您想在使用belongsTo -或hasOne -关系时创建一个select字段,您可以添加以下[…]

(现在放你应该放的…)

$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'));