2012-02-24 119 views
0

我有一些相關的表和主是這樣的模式:如何獲取關係表的所有關聯信息?

<?php 
App::uses('AppModel', 'Model'); 
class Information extends AppModel { 
public $useTable = 'informations'; 
public $displayField = 'name'; 
public $validate = array(
    'name' => array(
     'notempty' => array(
      'rule' => array('notempty'), 
      'message' => 'The field name can not be empty', 
     ), 
    ), 
    'lastname' => array(
     'notempty' => array(
      'rule' => array('notempty'), 
      'message' => 'The field lastname can not be empty', 
     ), 
    ), 
    'email' => array(
     'email' => array(
      'rule' => array('email'), 
      'message' => 'The email address is not correct', 
     ), 
    ), 
); 

public $belongsTo = array(
    'Country' => array(
     'className' => 'Country', 
     'foreignKey' => 'countries_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

public $hasMany = array(
     'Education' => array(
       'className' => 'Education', 
       'foreignKey' => 'informations_id', 
       'dependent' => true 
     ), 
     'Experience' => array(
       'className' => 'Experience', 
       'foreignKey' => 'informations_id', 
       'dependent' => true 
     ), 
     'Attachment' => array(
       'className' => 'Attachment', 
       'foreignKey' => 'informations_id', 
       'dependent' => true 
     ) 
); 

}

我需要得到所有相關的ID數據,但是當我嘗試使用此代碼來獲取數據:

public function view($id = null) { 
    $this->Information->id = $id; 
    if (!$this->Information->exists()) { 
     throw new NotFoundException(__('Invalid information')); 
    } 
    $this->set('information', $this->Information->read(null, $id)); 
} 

的從表中教育,經驗和附件是不顯示的信息只有這個信息是得到:

Array 
(
    [id] => 9 
    [name] => Reynier 
    [lastname] => Perez Mira 
    [email] => [email protected] 
    [mobile_phone] => 04241805609 
    [home_phone] => 02735555899 
    [address] => asdas 
    [picture] => skills.png 
    [other_information] => asdasdasd 
    [recruitment_status] => Call for Interview 
    [countries_id] => 9 
) 

爲什麼?我想念什麼?

+0

嘗試設置$ this-> Information-> recursive = 1;在閱讀之前就行了。您可以/也應該查看CakePHP書中的「可容納」行爲。 – Dave 2012-02-24 21:33:39

回答

1

更新您的功能設置信息遞歸:

public function view($id = null) { 
    $this->Information->id = $id; 
    if (!$this->Information->exists()) { 
     throw new NotFoundException(__('Invalid information')); 
    } 
    $this->Information->recursive = 1; 
    $this->set('information', $this->Information->read(null, $id)); 
} 

然後,只需確保所有在您的信息模型的關係是建立指向您要包括的其他車型。

+0

Thaks很多,它現在的作品:) – ReynierPM 2012-02-25 02:25:19