CakePHP:如何使用内连接从两个表中检索数据?

I have two tables in a database, one as user(id,first_name,last_name), and the other one as location(id,country).

I need to perform an inner join with these two tables based on the condition user.id = location.id, and the query result should contain the columns first_name, last_name and country.

I have tried the following query in CakePHP:

$this->set('users', $this->User->find('list', array(
    'fields' => array(
        'User.id',
        'User.first_name',
        'location.country'
    ),
    array(
        'joins' => array(
            array(
                'table' => 'location',
                'alias' => 'location',
                'type' => 'INNER',
                'conditions' => array('User.id = location.id')
            )
        )
    )
)));

which however produces this error:

Unknown column 'location.country' in 'field list'

What could be the problem?

I think your syntax is wrong because the options array should have a key for the joins. You appear to have an extra array. Try:

$this->set('users',$this->User->find('list', 
  array(
       'fields' => array('User.id', 'User.first_name','location.country'),
       'joins' => array(array('table' => 'location',
                               'alias' => 'location',
                               'type' => 'INNER',
                               'conditions' => array('User.id = location.id')
                         ))
         )
  ));