belongsTo cakephp关系不起作用

时间:2014-07-31 20:33:03

标签: php cakephp foreign-key-relationship

我是cakephp的新手,我接下来的一些教程。现在我正在尝试构建我的第一个应用程序,并且我被这个关系'belongsTo'所困扰。这不应该工作,更可能是我做错了。所以这是我的桌子,我的模型和控制器。

这是不显示任何员工ID或名称的视图。它让我疯狂!!! 。 位于app / View / Pendings / 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是此模型待定的外键。并且对应于员工表中的id。所以我试图做的是工作人员有许多待定,待定属于工作人员。因为待定是员工的一项待定任务。这就是为什么员工有许多挂件。

这是Pending模型,在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');
    }
}
?>

1 个答案:

答案 0 :(得分:0)

蛋糕的神奇作品......有条件......

你认为可能是正确的,因为模型关系设置正确,它应该......工作。但是您缺少一部分:将列表传递给视图。那部分并不神奇。

阅读this part of the docs(在inputs开始之前几乎一直向下)

  

如果要在使用belongsTo - 或hasOne - Relation时创建选择字段,可以添加以下[...]

(现在把你应该放的东西......)

$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'));
相关问题