2014-02-25 64 views
1

這是我的數據庫模式。 Database Scheme 在我的員工控制員中,我想要顯示屬於特定部門 的員工是否通過了dept_no,否則顯示所有員工。在cakephp模型關聯中寫入條件hasMany和belongsTo

下面是我的選項數組。目前它正在顯示包含員工部門名稱的所有記錄。

$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的新手。

回答

0

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

但是你可以用相反的方法做到這一點:查找DeptEmpdept_no是'd006'幷包含結果中的所有Employee

或者做一個聯合查找查詢,在您的$options數組中提供字段joinshttp://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables

0

你需要再看看你的實體關係圖。經理也是員工。你真的只需要兩個主要模型:員工和部門。同樣最好使用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] => 
       ) 

     ) 

) 

希望這是有幫助的。

相關問題