how to get list data from associative model in cakephp - cakephp-2.0

below is my query
$students_idsss = $this->Purchase->find('list', array(
'fields'=>array('Student.id','Student.first_name'),
'conditions' => array(
'Purchase.publisher_id' => $publisherID
),
'group'=>'student_id'
)
);
i am getting error : QLSTATE[42S22]: Column not found: 1054 Unknown column 'Student.first_name' in 'field list'

Related

Group By on Containable models using cake php

Hi i have model logo with the following associations
var $belongsTo = array(
'Attachment' => array(
'className'=>'Attachment',
'foreignKey'=>'attachment_id'
),
'Employee' => array(
'className'=>'Employee',
'foreignKey'=>'employee_id'
)
);
var $hasMany = array(
'Voting' => array(
'className'=>'Voting',
'foreignKey'=>'program_id'
)
);
i wrote a query in the following way it throws error
$PrgCond['contain'] = array(
'Attachment',
'Voting' => array(
'fields' => array(
'logo_program_id',
'COUNT(employee_id) as noOfEmps'
),
'group' => array('LogoVoting.logo_program_id')
),
'Employee' => array(
'fields' => array('id', 'employee_number', 'first_name', 'last_name')
)
);
$logoPrgDatas = $this->Program->find('all',$PrgCond);
Thrown Error is
SQL Error: 1054: Unknown column 'LogoVoting.logo_program_id' in 'field list'
This is an old question but still comes up in Google results..
I had a similar issue and found out that you cannot use group in the Model find options when you have associations or either use containable behavior(which uses associations) in cakephp 1.* or 2.*.
However, this issue is fixed in cakephp 3.* by using query builder! :)
It's a good idea to reference the model in your fields. For example, when you are using a column in a condition or a fields array, you should call it Model.column_name.
Having said that, the problem you are having is most likely related to the group:
'group' => array('LogoVoting.logo_program_id')
There is no LogoVoting in your model. It should be:
'group' => array('Voting.logo_program_id')
So I would update the query to be:
$PrgCond['contain'] = array(
'Attachment',
'Voting' => array(
'fields' => array(
'Voting.logo_program_id',
'COUNT(Voting.employee_id) as noOfEmps',
),
'group' => array('Voting.logo_program_id'),
),
'Employee' => array(
'fields' => array(
'Employee.id',
'Employee.employee_number',
'Employee.first_name',
'Employee.last_name',
),
),
);

CakePHP : Joining table with max value

Sorry im new in cakePHP. i use cakePHP 2.2. I have two table, Books and Transactions.
Table : Books
id
title
author
Table : Transactions
id
book_id
status
borrow_date
In table transaction list all transaction of the book.
My question, how to get status of max transaction id?
I try use this
options['joins'] = array(
array(
'table' => 'transactions',
'alias' => 'Transaction',
'type' => 'RIGHT OUTER',
'fields' => array('MAX(Transaction.id)', '*'),
'conditions' => array(
'Catalogue.id = Transaction.catalogue_id',
//'Transaction.user_id' => $userId
),
'order' => array('Transaction.id' => 'desc'),
)
);
$this->set('Book', $this->Book->find('all',$options));
at view:
$Book['Transaction']['status'];
But it shows an error:
Notice (8): Undefined index: Transaction [APP\View\Catalogues\user_katalog.ctp, line 32]
You are doing find(all) so you have a list of books
var_dump($Book);
you will see
array(
0 => array(
'Transaction' => ...
),
1 => array(
'Transaction' => ...
)
.....
);
not
array(
'Transaction' => ...
);
Either loop through $Book or use find('first', ...) if you want one.
dogmatic69 is right. Here's an example:
since you've used this in controller:
$this->set('Book', $this->Book->find('all',$options));
you should use this in view:
foreach( $Book as $b ) {
echo $b['Transaction']['status'];
}

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
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.
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'),
)
);
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'];
$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.

Drupal 7 database API error "You have an error in your SQL syntax"

Using the following code:
db_update('nodesequence_nodes')
->fields(array(
'order' => 1,
))
->condition('nid', 1, '=')
->condition('nsid', 1, '=')
->execute();
I get the following error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'order='1' WHERE (nid = '1') AND (nsid = '1')' at line 1: UPDATE
{nodesequence_nodes} SET order=:db_update_placeholder_0 WHERE (nid =
:db_condition_placeholder_0) AND (nsid = :db_condition_placeholder_1)
; Array ( [:db_update_placeholder_0] => 1
[:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => 1
) in nodesequence_init() (line 13 of
/var/www/eventbooking2/sites/all/modules/nodesequence/nodesequence.module).
I apologize I can't offer any more insight, but I hope you can.
The simple db_update code seems to me that it should work but I can't figure out why it isn't.
The database schema:
$schema['nodesequence_nodes'] = array(
'description' => 'Relating nodesequences to their consituent nodes.',
'fields' => array(
'nsid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
'order' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
),
'primary key' => array('nsid', 'nid'),
);
You can't use the column named 'order' in a table as this is a reserved word. You need to change it to something else.
More info here http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html take a look at table Table 9.2

conditions in find method

I have 3 models,
Mentor, MentorAttrib, Attrib where MentorAttrib is a join table. it lists Mentor.id -> Attrib.id
This is my find
$cond = array("Mentor.is_listed"=>1);
$contain_cond = array();
$contain = array(
'MentorAttrib' => array(
'fields' => array('MentorAttrib.id' ,'MentorAttrib.attrib_id'),
'Attrib'
)
);
if(! empty($this->request->data))
{
debug($this->request->data);
//skills
if(! empty($this->request->data['bookingSkills']))
{
$cond = array('MentorAttrib.attrib_id' => $this->request->data['bookingSkills']);
}
}
$this->request->data = $this->Mentor->find('all', array(
'conditions' => $cond,
'fields' => array('Mentor.id','Mentor.first_name','Mentor.last_name','Mentor.img'),
'contain' => $contain
));
I want to filter the result by the skills.
[bookingSkills] => Array
(
[0] => 2
[1] => 4
[2] => 10
)
The error im getting is
Column not found: 1054 Unknown column 'MentorAttrib.attrib_id' in 'where clause'
This is the data set
http://pastebin.com/85uBFEfF
You need a join
By default, CakePHP will only create joins for hasOne and belongsTo associations - any other type of association generates another query. As such you cannot filter results based on a condition from a hasMany or hasAndBelongsToMany association by just injecting conditions and using contain.
Forcing a join
On option to achieve the query you need is to use the joins key. As shown in the docs You can also add a join, easily, like so:
$options['joins'] = array(
array('table' => 'mentor_attribs',
'alias' => 'MentorAttrib',
'type' => 'LEFT',
'conditions' => array(
'Mentor.id = MentorAttrib.mentor_id',
)
)
);
$data = $this->Mentor->find('all', $options);
This allows the flexibility to generate any kind of query.

Resources