【问题标题】:Writing conditions in cakephp model associations hasMany and belongsTocakephp模型关联hasMany和belongsTo中写条件
【发布时间】:2014-02-25 14:15:54
【问题描述】:

这是我的数据库架构。 在我的员工控制器中,我想显示属于特定部门的员工 如果通过了 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 的新手。

【问题讨论】:

    标签: php cakephp cakephp-2.0 model-associations


    【解决方案1】:

    遗憾的是,包含除了 hasOne 关联之外不会对任何内容进行连接,而是执行多个查询,因此您对包含的数据执行的任何条件都不会过滤原始模型。

    但你可以反过来做:查找dept_no 为“d006”的DeptEmp,并包含结果中的所有Employee

    或者进行联合查找查询,在您的 $options 数组中提供字段 joinshttp://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

    【讨论】:

      【解决方案2】:

      您需要再次查看您的实体关系图。经理也是雇员。您实际上只需要两个主要模型:Employee 和 Department。最好使用 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] => 
                      )
      
              )
      
      )
      

      希望这有帮助。

      【讨论】:

        猜你喜欢
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多