无法从可包含的模型cakephp中获取必填字段

时间:2015-03-24 01:02:03

标签: cakephp

我与Guardian ans Student之间有很多关系。卫报有很多学生。我无法从可容纳的对象学生那里获得所需的字段,而是从学生处获得所有内容,但我确实从Guardian获得了必需的字段。

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

   $this->Guardian->Behaviors->load('Containable'); 
    $guardians =$this->Guardian->find('all',array( 
                'contain'=>array('Student',
                    array( 'fields'=> array('Student.guardian_id,Student.id,Student.first_name' ))),

               'order' => array('guardian_first_name ASC'),
             'fields'=> array('Guardian.guardian_first_name,Guardian.guardian_last_name,Guardian.id' ),
               'recursive'=> -1      
              ));

array(
    (int) 0 => array(
        'Guardian' => array(
            'guardian_first_name' => '',
            'guardian_last_name' => '',
            'id' => '166'
        ),
        'Student' => array(
            (int) 0 => array(
                'id' => '166',
                'student_inactive' => true,
                'student_enq' => false,
                'student_unallocated' => false,
                'first_name' => 'Kala',
                'last_name' => 'narayanan',

2 个答案:

答案 0 :(得分:0)

fields选项未正确嵌套,如果您正确格式化代码,您可能会注意到这一点,其中包含以下内容:

$this->Guardian->Behaviors->load('Containable'); 
$guardians = $this->Guardian->find('all', array( 
    'contain' => array(
        'Student',
        array(
            'fields'=> array(
                'Student.guardian_id,Student.id,Student.first_name'
            )
        )
    ),
    'order' => array(
        'guardian_first_name ASC'
    ),
    'fields' => array(
        'Guardian.guardian_first_name,Guardian.guardian_last_name,Guardian.id'
    ),
    'recursive'=> -1
));

持有fields选项的数组必须作为Student键的值传递。

// ...
'contain' => array(
    'Student' => array(
        'fields'=> /* ...*/
    )
),
// ...

在旁注中,当将字段作为逗号分隔的字符串传递时(可能不是最好的想法),没有必要在数组中传递它们。

答案 1 :(得分:0)

阵列(             '字段' = GT;阵列(                 guardian_id,id,first_name'             )         )

使用没有型号名称的字段。