简体   繁体   中英

Cakephp: How to get data with specific fields on associated model

I'm using cakephp framework. I have two tables on my DB, courses and modules table. Those tables are related, courses has many modules. Basically I want to get data on courses and on modules but with specific fields. I want to get just the id and the title of the course and the id and the title of the module.

This code:

$courses_taken = $this->Course->find('all', array(
    'conditions' => array('Course.id' => $course_id_list),
    'fields' => array('Course.id', 'Course.title')
));

gives me:

array(
    (int) 0 => array(
        'Course' => array(
            'id' => '1',
            'title' => 'course 1'
        ),
        'Module' => array(
            (int) 0 => array(
                'id' => '1',
                'course_id' => '1',
                'title' => 'module 1',
                'image' => null,
                'content' => 'Lorem ipsum',
                'voice_over' => null,
                'created' => '2014-09-03 14:02:25',
                'modified' => '2014-09-03 14:02:28'
            ),
            (int) 1 => array(
                'id' => '2',
                'course_id' => '1',
                'title' => 'module 2',
                'image' => null,
                'content' => 'Sasdas',
                'voice_over' => null,
                'created' => null,
                'modified' => null
            )
        )
    ),
    (int) 1 => array(
        'Course' => array(
            'id' => '2',
            'title' => 'course 2'
        ),
        'Module' => array()
    )
)

But I wanna select a specific field for module as well so I tried this code:

$courses_taken = $this->Course->find('all', array(
    'conditions' => array('Course.id' => $course_id_list),
    'fields' => array('Course.id', 'Course.title', 'Module.id', 'Module.title')
));

but gives me an error: Column not found: 1054 Unknown column 'Module.id' in 'field list'. Please help me guys thanks.

Got the answer, To make sgt's answer work, $actsAs property should be added in Course model. Reference : http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

Either you use some behavior like Containable and your find() will look like this :

$courses_taken = $this->Course->find('all', array(
    'conditions' => array('Course.id' => $course_id_list),
    'fields' => array('Course.id', 'Course.title'),
    'contain' => array(
        'Module' => array(
            'fields' => array('Module.id', 'Module.title'),
        ),
    ), 
));

try this -

$courses_taken = $this->Course->find('all', array(
    'conditions' => array('Course.id' => $course_id_list),
    'fields' => array('Course.id', 'Course.title'),
    'contain' => array('Module.id', 'Module.title')
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM