CakePHP模型JOINS,缺少字段

时间:2013-01-24 05:55:48

标签: php cakephp

嘿伙计们,我是CakePHP的新手,加入两张桌子时遇到了问题。

$options['joins'] = array(
          array('table' => 'options',
         'alias' => 'Options',
         'type' => 'LEFT',
         'fields' => array('Question.question', 'Options.option'),
         'conditions' => array(
         'Options.question_id = Question.id'))
         );

        $this->set('qq',$this->Question->find('all',$options));

我只获取问题表的字段而不是选项表的字段。我如何获得选项表的字段?

1 个答案:

答案 0 :(得分:0)

尝试绑定表。

$this->ModelName->bindModel(array
(
    'belongsTo' => array
    (
        'ModelToJoin' => array
        (
            'foreignKey' => false,
            'conditions' => array
            (
                'ModelToJoin.model_name_id = ModelName.id'
            )
        )
    )
));

根据你的情况。

<?php

$this->Question->bindModel(array
(
    'hasMany' => array
    (
        'Options' => array
        (
            'foreignKey' => false,
            'type'       => 'LEFT',
            'conditions' => array
            (
                'Options.question_id = Question.id'
            )
        )
    )
));

$this->Question->find('all',array
(
    'fields' => array('Question.question', 'Options.option')
));

Read More On