2012-02-24 26 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の本の "Containable"動作を調べることもできます。 – Dave

答えて

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 a lot、それは今動作します:) – ReynierPM

関連する問題