簡體   English   中英

cakephp模型關聯中的編寫條件hasMany和belongsTo

[英]Writing conditions in cakephp model associations hasMany and belongsTo

這是我的數據庫架構。 數據庫方案 在我的雇員控制器中,如果要傳遞dept_no,我想顯示屬於特定部門的雇員,否則顯示所有雇員。

以下是我的選項數組。 當前它正在顯示所有帶有員工部門名稱的記錄。

$options = array('contain' => array(
                   'DeptEmp' => array(
                       'fields' => array('DeptEmp.dept_no')
                    ),
                    'DeptEmp.Department' => array(
                       'fields' => array('Department.dept_name')
                    )
                )
           );

我的員工模型

$hasMany = array(
            'DeptEmp' => array(
              'className' => 'DeptEmp',
              'foreignKey' => 'emp_no',
              'dependent' => false
             )
          );

我的DeptEmp模型

public $belongsTo=array(
                   'Employee'=>array(
                      'className'=>'Employee',
                      'foreignKey'=>'emp_id',
                      'dependent'=>false
                    ),
                    'Department'=>array(
                      'className'=>'Department',
                      'foreignKey'=>'dept_no',
                      'dependent'=>false
                    )
                 );

我的部門模型

public $hasMany = array(
                   'DeptEmp' => array(
                      'className' => 'DeptEmp',
                      'foreignKey' => 'dept_no',
                      'dependent' => false
                    )
                 );

我試過了

$this->Employee->DeptEmp->dept_no ='d006'

但這沒有任何作用。

如果我是cakephp的新手,如果我做錯了什么,請指導我。

可悲的是, hasOne除了hasOne關聯以外, hasOne任何內容進行hasOne ,而是執行多個查詢,因此您對所包含數據執行的任何條件都不會過濾原始模型。

但是您可以采用另一種方法:查找DeptEmpdept_no為'd006',並包含結果中的所有Employee

或者執行聯接的查找查詢,在$options數組中提供字段joins http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

您需要再次查看您的實體關系圖。 經理也是員工。 您實際上只需要兩個主要模型:員工模型和部門模型。 另外,最好使用id列來標識記錄,因為您可能希望更改部門的dep_no,這將需要更新該部門的所有員工中的dep_no。

員工模式

<?php

class Employee extends AppModel {

    public $name = 'Employee';

/**
 * Model Schema
 *
 * @var array
 * @access protected
 */
    protected $_schema = array(
        'id' => array('type' => 'integer', 'length' => 8, 'key' => 'primary'),
        'first_name' => array('type' => 'string', 'null' => false),
        'last_name' => array('type' => 'string', 'null' => false),
        'birth_date' => array('type' => 'datetime', 'null' => false),
        'gender' => array('type' => 'string', 'null' => false),
        'hire_date' => array('type' => 'datetime', 'null' => false),
        'department_id' => array('type' => 'integer', 'length' => 8),
        'manager_id' => array('type' => 'integer', 'length' => 8),
        'created' => array('type' => 'datetime', 'null' => false),
        'modified' => array('type' => 'datetime', 'null' => true, 'default' => null)
        );

/**
 * Model Associations
 *
 * @var array
 * @access public
 */
    public $belongsTo = array(
        'Department' => array(
            'className'   => 'Department',
            'foreignKey'  => 'department_id',
            'dependent' => true
            ),
        );

部門模式

<?php

class Department extends AppModel {

    public $name = 'Department';

/**
 * Model Schema
 *
 * @var array
 * @access protected
 */
    protected $_schema = array(
        'id' => array('type' => 'integer', 'length' => 8, 'key' => 'primary'),
        'number' => array('type' => 'string', 'length' => 8),
        'name' => array('type' => 'string', 'null' => false),
        'created' => array('type' => 'datetime', 'null' => false),
        'modified' => array('type' => 'datetime', 'null' => true, 'default' => null)
        );

/**
 * Model Associations
 *
 * @var array
 * @access public
 */
    public $hasMany = array(
        'Employee' => array(
            'className'   => 'Employee',
            'foreignKey'  => 'department_id',
            'dependent'   => true
            )
        );
    public $hasOne = array(
        'DepartmentManager' => array(
            'className' => 'Employee',
            'foreignKey'  => 'department_id',
            'conditions' => array('DepartmentManager.manager_id' => null),
            'dependent' => true
        )
    );
}

部門主管

$data = $this->Department->find('first', array(
    'conditions' => array('Department.number' => 'd006'),
    'contain' => array(
        'DepartmentManager', 'Employee',
        )
    ));

產量

Array
(
    [Department] => Array
        (
            [id] => 1
            [number] => d006
            [name] => Human Resources
            [created] => 2014-02-25 00:00:00
            [modified] =>
        )

    [DepartmentManager] => Array
        (
            [id] => 1
            [first_name] => David
            [last_name] => Scott
            [birth_date] => 2014-02-25
            [gender] => M
            [hire_date] => 2014-02-25
            [department_id] => 1
            [manager_id] => 
            [created] => 2014-02-25 00:00:00
            [modified] => 
        )

    [Employee] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [first_name] => David
                    [last_name] => Scott
                    [birth_date] => 2014-02-25
                    [gender] => M
                    [hire_date] => 2014-02-25
                    [department_id] => 1
                    [manager_id] => 
                    [created] => 2014-02-25 00:00:00
                    [modified] => 
                )

            [1] => Array
                (
                    [id] => 2
                    [first_name] => Joe
                    [last_name] => Bloggs
                    [birth_date] => 2014-02-25
                    [gender] => M
                    [hire_date] => 2014-02-25
                    [department_id] => 1
                    [manager_id] => 1
                    [created] => 2014-02-25 00:00:00
                    [modified] => 
                )

            [2] => Array
                (
                    [id] => 3
                    [first_name] => Jane
                    [last_name] => Bloggs
                    [birth_date] => 2014-02-25
                    [gender] => F
                    [hire_date] => 2014-02-25
                    [department_id] => 1
                    [manager_id] => 1
                    [created] => 2014-02-25 00:00:00
                    [modified] => 
                )

        )

)

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM