如何在cakephp 2中编写SQL查询

时间:2019-02-05 06:57:57

标签: php cakephp cakephp-2.0

我是cakephp的新手,我正在使用cakephp 2.8,我有一个SQL查询,我想转换为cakephp查询,我该怎么做?

这是我的sql查询,在sql中工作正常,但我想以cakphp格式实现,下面是sql查询

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

我尝试使用以下代码,但这对我不起作用,显示错误

$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);

上面的代码显示了内部服务器错误,如何删除此错误?预先感谢。

1 个答案:

答案 0 :(得分:0)

不是Cake的专家,但是代码中存在PHP语法错误,请将[TestClass] public class MockLocalFunctions { [TestMethod] public void BasicUsage() { //Arrange var foo = Mock.Create<Foo>(Behavior.CallOriginal); Mock.Local.Function.Arrange<int>(foo, "GetResult", "GetLocal").DoNothing(); //Act var result = foo. GetResult(); //Assert Assert.AreEqual(100, result); } } 更改为 从变量中删除单个反向逗号并尝试读取error_log文件,此类所有错误均记录在其中