How do I use DISTINCT to get unique user id with highest value for total_time_driven_at_this_trip and also pull user_name from another table which has belongsto relations based on user_id?
I tried this...
$this->set('tripNsws', $this->TripNsw->find('all',array('limit' => 20,'fields' => array('DISTINCT(TripNsw.user_id)','TripNsw.total_time_driven_at_this_trip'),'group' => array('TripNsw.user_id') ,'order' => array('TripNsw.total_time_driven_at_this_trip desc'))));
but it's not working.
I suppose you need to get below....
SELECT DISTINCT(user_id),`total_time_driven_at_this_trip` FROM `trip_nsws` order by `total_time_driven_at_this_trip` desc
// see below url
http://book.cakephp.org/1.3/view/1018/find
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
'fields' => array('Model.field1', 'DISTINCT Model.field2'), //array of field names
'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order
'group' => array('Model.field'), //fields to GROUP BY
'limit' => n, //int
'page' => n, //int
'offset'=>n, //int
'callbacks' => true //other possible values are false, 'before', 'after'
)
// or try this
function some_function() {
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
$authors = $this->Article->User->find('count');
$publishedAuthors = $this->Article->find('count', array(
'fields' => 'DISTINCT Article.user_id',
'conditions' => array('Article.status !=' => 'pending')
));
}
Correct Syntax for DISTINCT keywork in cakephp
$this->set('banners', $this->Banner->find('all',array('fields'=>'DISTINCT Banner.id')));
Make sure DISTINCT uses in fields array.
The correct syntaxis is
$this->set('tripNsws', $this->TripNsw->find('all',array('fields'=>'DISTINCT TripNsw.user_id')));
I've always used a GROUP BY in the query to get the same result as DISTINCT
You can consider setting it as a Virtual Field ( http://book.cakephp.org/1.3/view/1608/Virtual-fields )
$this->Model->virtualFields['distinct_user'] = 'DISTINCT Model.user_id';
'fields' => array('DISTINCT Model.Modelfield')
or
'fields' => array('Model.id','DISTINCT Model.Modelfield')
Related
I want to match course_id with $courseInfo which is fetching from $cLID.
When I change this $clID['Relationscl']['course_id'] line as 1 everything is ok, but the other way an error appears like Undefined index: Relationscl.
Here is my code:
$this->loadModel('Course');
$this->loadModel('Lecturer');
$clID = $this->Relationscl->find('all', array(
'conditions' => array(
'student_id' => $id
),
'fields' => array(
'course_id','lecturer_id'
)
));
$this->set('clIDs', $clID);
$courseInfo = $this->Course->find('first',array(
'conditions' => array(
'course_id' => $clID['Relationscl']['course_id']
),
'fields' => array(
'course_name','course_code','course_credit'
)
));
$this->set('cInfos', $courseInfo);
Correct me if I'm wrong, but the find('all') function returns and indexed array, so it should be something like
$clID = array( 0 => array('Relationscl'=>array('course_id'=>1 /*and more fields*/)),
1 => array('Relationscl'=>array('course_id'=>2 /*and more fields*/))
/*etc*/);
So clearly $clID['Relationscl'] is undefined. Try with $clID[0]['Relationscl']. Though even that seems weird, why would you do a find('all') if you only plan on using the one record, isn't find('first') better? Or set that $courseInfo definition inside a loop?
how do I build a find() query in cakePHP using these conditions:
Find where
MyModel.x = 1 and MyModel.y = 2 OR
MyModel.x = 1 and MyModel.y value does not exist (or is equal to empty string)
Can somebody tell me how I can go about building such find query?
I'm gonna give you some pointers, but you need to try to do this as it's very basic and it's always good to practice.
A basic find in cake is in the form of
$this->ModelName->find('all');
This in its default form does a SELECT * from model_names (convention is to have singular ModelName for plural table name - model_names)
To add conditions:
$this->ModelName->find('all', array('conditions' => array('ModelName.x' => 1));
To add AND conditions
$this->ModelName->find('all', array('conditions' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
));
To add OR conditions
$this->ModelName->find('all', array('conditions' => array(
'OR' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
)
));
To combine both
$this->ModelName->find('all', array('conditions' => array(
'ModelName.y is not' => null,
'OR' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
)
));
// where y is not null and (x = 1 or y = 2)
http://book.cakephp.org/1.3/view/1030/Complex-Find-Conditions
(btw I'm sure there will be users giving you the exact answers, so just take my answer for your reference :) )
$this->MyModel->find('all', array('conditions' => array(
'OR' => array(
array(
'MyModel.x' => 1,
'MyModel.y' => 1
),
array(
'MyModle.x' => 1,
'OR' => array(
array('MyModel.y' => NULL),
array('MyModel.y' => '')
)
)
)
)));
No matter what I do I can't get it to respect the order I specify.
$this->paginate = array(
'Car' => array(
'limit' => 6,
'order' => array(
'Car.year' => 'desc'
),
'table' => 'cars'
)
);
Generated SQL:
SELECT `Car`.`id`, ... `Car`.`year`,... FROM `cars` AS `Car` WHERE 1 = 1 LIMIT 6
Turns out I was using a :sort in my url parameters. Once I took that out all was well :)
If you only have one item, you don't need/shouldn't use an array:
//one thing
var $order = "Model.field DESC";
//multiple things
var $order = array("Model.field" => "asc", "Model.field2" => "DESC");
(per this page)
Note too that virtual fields are ignored by default when paginating.
See "Control which fields used for ordering" in Cake 2.0 Pagination documentation.
Example:
$this->MyModel->virtualFields['count'] = 0;
$this->Paginator->settings = array(
'fields' => 'COUNT(id) AS MyModel__count',
'group' => ('MyModel.group_id'),
'order' => array('MyModel__count' => 'DESC'),
);
// IMPORTANT: pass sortable fields including the virtual field as 3rd parameter:
$log = $this->Paginator->paginate('MyModel', null, array('MyModel__count', 'id'));
$this->Release->recursive = 0;
$this->paginate['Release']['joins'] =
array(
array(
'table' => ' game_potential_amazon_matches',
'alias' => 'PotentialAmazonMatch',
'type' => 'inner',
'conditions' =>
array(
'PotentialAmazonMatch.release_id = Release.id'
)
)
);
$this->set('releases', $this->paginate('Release'));
$this->set('title_for_layout', 'Release with Potential Amazon Matches)');
I am getting multiple records and would just like to get distinct by Release.id?
You have to use DISTINCT in fields in that case.
just after conditions array use fields array like
fields => array('DISTINCE Release.id','field 2','field 3',...etc)
I have a user model which gives me latest users as output. How can I limit the record to just output me 200 records instead of all the users in database?
According to the documentation, the second argument to the find() method is a $params array.
One of the possible values to pass in this array is a limit key. So you could do the following:
$users = $this->User->find('all', array('limit' => 200));
"i have it like array('limit' => 21, 'page' => 1) for paging 21 users in one page.. if i change the limit there to 200 then it paginates 200 users in one page only...in this case how to limit along with proper pagination?? – Anonymous May 14 '09 at 7:22"
yes you can use the cakePHP pagination helper as someone has mentioned. But there may be some cases where you want to do your own pagination or just limit the number of records retrieved per call. For what it's worth here's how I handled one such situation.
Say for example you want to retrieve a certain number of records per page, Then:
$start = 0; -> this is in order to start retrieving records starting from the first one. If you need to say for example, start from the 31st, then $start = 30;
So,
$start = 0;
$length = 20; // we are going to retrieve 20 records starting from the first record
And the code will be something like:
// To retrieve a number of Products per page
$products = $this->Product->find('all', array(
'order' => 'product_number ASC',
'limit' => $start.','.$length,
'recursive' => -1
)
);
Don't paginate with find().
Cake Pagination: http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
//array of field names
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
//string or array defining order
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'), //fields to GROUP BY
'limit' => n, //int
'page' => n, //int
)
Limit * page = 200 set your values according to your comfortable view in pages. This might help
You can also try this out
$results = $this->Model->find('all',
array('limit'=>10,
'order'=>array('date DESC')));
open the model file of user and do as follows:
you will need to change the 'limit' property in the relationship variable named
var $hasMany = array( 'Abus' =>
array('className' => 'Abus',
'foreignKey' => 'user_id',
'dependent' => false, 'conditions'
=> '',
'fields' => '', 'order' => '', 'limit' => '200', 'offset'
=> '', 'exclusive' => '', 'finderQuery' => '',
'counterQuery' => '' ) );
OR you can also try this out...
in your users controller set the $paginate to like this.
var $paginate = array('limit' => 200);
The records will be limited to 200 now wherever you use paginate.