简体   繁体   中英

How to write sql query in cakephp 2

I am new in cakephp and i am using cakephp 2.8,I have one sql query and i want to convert into cakephp query,How can i do this ?

Here is my sql query which is working fine in sql but i want to implement in cakphp format,Following is sql query

SELECT meta_description.tag_id,meta_description.tag_name,poll_meta.poll_id,polls.content,polls.type,polls.poll_type,polls.user_id,users.username,user_profile.first_name,user_profile.last_name, user_profile.image, user_profile.display_name
FROM meta_description
JOIN poll_meta
ON meta_description.tag_id=poll_meta.tag_id
JOIN polls
ON poll_meta.poll_id=polls.id
JOIN users
ON polls.user_id=users.id
JOIN user_profile
ON users.id=user_profile.user_id
WHERE meta_description.tag_id = ? AND polls.status = 1
            ORDER BY polls.id DESC

I tried with following code but this is not working for me,showing error

$polls['joins'] = array(
    array('table' => 'meta_description',
        'alias' => 'BooksTag',
        'type' => 'inner',
        'conditions' => array(
            ' meta_description.tag_id =$tag_id, 
            'polls.status'="1"

        )
    ),
    array('table' => 'poll_meta',
        'alias' => 'poll_meta',
        'type' => 'inner',
        'conditions' => array(
            'poll_meta.poll_id = polls.id'
        )
    )

   array('table' => 'users',
        'alias' => 'users',
        'type' => 'inner',
        'conditions' => array(
            'polls.user_id = users.id'
        )
    )   
   array('table' => 'user_profile',
        'alias' => 'user_profile',
        'type' => 'inner',
        'conditions' => array(
            ' users.id = user_profile.user_id'
        )
    )   
);

 'order' => array('Poll.date_created' => 'DESC'),
                'limit' => 10

$options['conditions'] = array(
    'meta_description.tag_id' => '$tagid',
    'polls.status' => '1'
);

$polls = $poll->find('all', $options);

Above code showing internal server error,How can i remove this , Where i am wrong ? Thanks in advance.

不是 Cake 高手但是代码中有 PHP 语法错误,将' meta_description.tag_id =$tag_id,改为'meta_description.tag_id ='.$tag_id,去掉变量中的单引号并尝试读取 error_log 文件,这种所有错误都记录在那里

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