user1561002
user1561002

Reputation: 11

Cakephp: find() Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Interestslogs.interest_id' in 'field list'

I have a table called 'Interestslogs' and model's name is Interestlog.

I need to get the client_id according to id from that table in Cakephp.

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

However I am getting database error:

Database Error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Interestslogs.interest_id' in 'field list'

SQL Query: SELECT Interestslogs.interest_id FROM efa.interestslogs AS Interestslog LEFT JOIN efa.interests AS Interest ON (Interestslog.interest_id = Interest.id) LEFT JOIN efa.clients AS Client ON (Interestslog.client_id = Client.id) WHERE Interestslogs.id = 1 LIMIT 1

Upvotes: 1

Views: 9971

Answers (4)

Fury
Fury

Reputation: 4776

Remove the plural "s" form Interestslogs

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

and also check your model. If every thing (Client and Interestslog) is associated properly you shouldn't get any error.

Upvotes: 1

Michael Grenzer
Michael Grenzer

Reputation: 501

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

You should write the table names in the condition/field array without the last "s" since it's added by cakephp automatically.

Upvotes: 0

user1548335
user1548335

Reputation:

Check if there is interest_id column in your Interestlogs table.

Or try

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

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

You can try: (if your relations are all right)

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

Upvotes: 0

Related Questions