Cakephp:find()错误:SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'Interestslogs.interest_id'

时间:2012-07-29 13:41:51

标签: cakephp

我有一个名为'Interestslogs'的表,模型的名称是Interestlog。

我需要根据Cakephp中该表的id获取client_id。

$client_id = $this->Interestslog->find('first',array(
        'conditions' => array('Interestslogs.id' => $id),
        'fields' => array('Interestslogs.client_id'),
        )
    );

但是我收到了数据库错误:

Database Error

错误:SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'Interestslogs.interest_id'

SQL查询:SELECT Interestslogsinterest_id FROM efainterestslogs AS Interestslog LEFT JOIN efainterests AS Interest ON(Interestsloginterest_id = Interestid)LEFT JOIN efaclients AS Client ON(Interestslogclient_id = Clientid)WHERE Interestslogsid = 1 LIMIT 1

4 个答案:

答案 0 :(得分:1)

删除复数“s”表单Interestslogs

$client_id = $this->Interestslog->find('first',array(
    'conditions' => array('Interestslog.id' => $id),
    'fields' => array('Interestslog.client_id'),
    )
);

并检查您的模型。如果每件事(Client和Interestslog)都正确关联,您不应该收到任何错误。

答案 1 :(得分:0)

你可以尝试:(如果你的关系没事的话)

$this->Interestslog->recursive = -1;
$client_id = $this->Interestslog->find('first',array(
     'conditions' => array('Interestslogs.id' => $id),
     'fields' => array('Interestslogs.client_id'),
    )
);

答案 2 :(得分:0)

检查您的Interestlogs表中是否有interest_id列。

或尝试

$variable_temp = $this->Interestslog->findById($id);
//debug($variable_temp);
$client_id = $variable['client_id'];

答案 3 :(得分:0)

$client_id = $this->Interestslog->find('first',array(
        'conditions' => array('Interestslog.id' => $id),
        'fields' => array('Interestslog.client_id'),
        )
    );

您应该在条件/字段数组中编写表名而不使用最后的“s”,因为它是由cakephp自动添加的。