简体   繁体   中英

Writing conditions in cakephp model associations hasMany and belongsTo

Here is my database schema. 数据库方案 In my employees controller I want to show employees belonging to particular dept if dept_no is passed or else show all employees.

Below is my options array. Currently it is showing all records with employees department name.

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

My Employee model

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

My DeptEmp model

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

My Department model

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

I tried

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

but it does not have any effect.

Kindly guide me if I am doing something wrong since I am newbie to cakephp.

Contain sadly doesn't do a join on anything but hasOne association, instead it does multiple queries, so any conditions that you do on the contained data won't filter the original model.

But you can do it the other way around: Finding DeptEmp whose dept_no is 'd006' and contain all Employee that are in the results.

Or do a joined find query, providing in your $options array the field joins . http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

You need to look again at your entity-relationship diagram. Managers are also employees. You really only need two main models: Employee and Department. Also best to use id column to identify records as you may wish to change dep_no for a department which would require updating dep_no in all employees of that department.

Employee Model

<?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
            ),
        );

Department Model

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

Department Controller

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

Output

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] => 
                )

        )

)

Hope this is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM