Cakephp 1.3 - Ordering a group of results - sql

I have the following model:
$values = $this->PtlUserdata->find('all', array(
'fields' => array(
'PtlUserdata.field',
'PtlUserdata.value',
'PtlUserdata.timestamp'
),
'conditions' => array(
'PtlUserdata.user_id' => $user->get('id'),
),
'order' => array('PtlUserdata.timestamp' => 'DESC'),
'group' => array('PtlUserdata.field')
));
I'm trying to order the results by timestamp and then group the results by field, so I can get the most recent record with that field name.
Does anybody know how to do this is cakephp?
Thanks in advance for any help, much appreciated :)

This is the default behavior for the database. You will have to come up with some sort of workaround to do it.
An idea: CakePHP: Group by ID and Order by date

Does this work:
$values = $this->PtlUserdata->find('all', array(
'fields' => array(
'PtlUserdata.field',
'PtlUserdata.value',
'max(PtlUserdata.timestamp)'
),
'conditions' => array(
'PtlUserdata.user_id' => $user->get('id'),
'PtlUserdata.timestamp' => 'max(PtlUserdata.timestamp)',
),
'group' => array(
'PtlUserdata.field',
'PtlUserdata.timestamp'
)
));
I haven't had the chance to test this but you definitely need to be grouping by PtlUserdata.timestamp.

Related

extendedSummary Yiiboster not show the sum total of gridview

I have gridview using TbExtendedGridView yii booster, in footer gridview i want show the sum of field using extendedSummary, i have follow the tutorial in yiibooster web but i can't show the extendedSummary to show the sum of column. Pls help me what's wrong in my code below ?. in the footer just show the box with blank text.
//this isi my gridview code
$no_loan= $_GET[no_loan];
$sql2="SELECT * from tbangsuran where nomor_pinjaman = '$no_loan' and status_bayar=1 order by no ASC";
$sqlProvider = new CSqlDataProvider($sql2);
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
//'filter'=>$model,
'id'=>'tbangsuran-grid',
'type'=>'striped bordered',
'template' => "{items}\n{extendedSummary}",
'dataProvider' =>$sqlProvider,
'columns'=>array(
array(
'name'=>'Angsuran Ke',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:90px;'),
'htmlOptions'=>array('style'=>'text-align:center'
),
'value'=> '$data[\'no\']',
),
array(
'name'=>'Tanggal Tagihan',
'headerHtmlOptions'=>array('style'=>'text-align:center;width:90px;'),
'htmlOptions'=>array('style'=>'text-align:center'),
'value'=> 'date("d-m-Y",strtotime($data[\'tanggal_bayar\']))',
),
//'nomor_pinjaman',
array(
'name'=>'Tunggakan Pokok',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_pokok\'],0,"",".")',
),
array(
'name'=>'Tunggakan Bunga',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_bunga\'],0,"",".")',
),
array(
'name'=>'Total Tunggakan',
'headerHtmlOptions'=>array('style'=>'text-align:center;width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_pokok\']+$data[\'pastdue_bunga\'],0,"",".")',
),
),
'extendedSummary' => array(
'title' => 'Total Tunggakan',
'columns' => array(
'pastdue_pokok' => array('label'=>'Total Tunggakan','class'=>'TbSumOperation')
)
),
'extendedSummaryOptions' => array(
'class' => 'well pull-right',
'style' => 'width:300px'
),
));
You must use the same column name in extendedSummary field.
In your case would be:
'extendedSummary' => array(
'title' => 'Total Tunggakan',
'columns' => array(
'Total Tunggakan' => array('label'=>'Total Tunggakan','class'=>'TbSumOperation')
)
),
I recommend you to use short column name to this tasks.
Hope this helps!

WP_Query $args and compare operators

I'm having troubles to generate an sql query with operators.Actually wirte this code:
$args = array(
'post_type' => 'listings',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//CITY
array(
'key' => 'city',
'value' => $city,
'compare' => 'LIKE'
),
//type
array(
'key' => 'type',
'value' => $type,
'compare' => 'LIKE'
),
//PRICE
array(
'key' => 'price',
'value' => array( 100, 5000 ),
'compare' => 'BETWEEN'
),
)
);
This code work fine, but now, i need to get the posts with price between $start and $end, OR price = -1. In other words, post that not have set the price, or set to -1 must show in the query. The others conditions (city, type,etc) must be match with AND
Any ideas? Thanks!
My initial thought is to add a filter onto posts_where or posts_join to inject a custom SQL condition into the query.
Something along these lines:
wp_postmeta.meta_key='price' AND
(wp_postmeta.meta_value='-1' OR
CAST(wp_postmeta.meta_value AS INT) BETWEEN $start AND $end)

like OR operator in Cakephp

I am new to cakephp and I want to add or, and, and like to my existing query.
I want to make a condition like this
WHERE 'Message.user_id = Contact.user_id' AND 'Message.mobileNo LIKE'=>"%Contact.mobileNo" OR LIKE'=>"%Contact.homeNo" OR LIKE'=>"%Contact.workNo"
My query is
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
),
'type' => 'inner'
)
)
), false);
$this->find('all', array('conditions' => array(),
'fields' => array('DISTINCT mobileNo')));
you can use like below in your existing query.
$this->find('all', array(
'conditions' => array(
'OR' => array(
array(
"Message.mobileNo LIKE" => "%Contact.mobileNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.homeNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.workNo",
)
)
),
'fields' => array('DISTINCT mobileNo')
));
And you can also refer Detail Document for simple search with like
you can use: for "like"
$this->Post->find("all",array('condition'=>array('Author
LIKE'=>"ad%")));
above query will give You the data from table posts where author name starts with "ad".
for "OR"
$this->Post->find("all",array('condition'=>array("OR"=>array('Author
LIKE'=>"ad%",'Author LIKE'=>"bo%"))));
above query will give You the data from table posts where author name starts with "ad" OR "bo"
This blog post can b useful to u as well!

order data by desc order in default in YII

I am tyring to display somedata in desending order in YII view.HOw Do I order the data by studentID desc by default ?
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'student-grid',
'criteria'=>array(
'order'=>'StudentID DESC',
),
'dataProvider' => Agent::getStudents($model->agent_id),
'columns' => array(
'StudentID',
'first_name',
'last_name',
'dob',
'gender',
array(
'header' => 'Options',
'class' => 'CButtonColumn',
'template'=>'{View}',
'buttons'=>array(
'View' => array(
'url'=> 'Yii::app()->createUrl("/students/view/" . $data->StudentID)',
),
),
),
),
)); ?>
Thanks Ab
In your dataProvider (i assume that's what getStudents() function returns) add another array to your config array :) Like:
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$some_criteria,
/* Your array */
'sort'=>array(
'defaultOrder'=>array(
'StudentID'=>true,
),
),
/***/
));
Value 'false' in 'defaultOrder' array refers to ascending order, 'true' refers to descending order.
I hope that's what you're looking for :).
Also check out this forum thread: http://www.yiiframework.com/forum/index.php/topic/8428-cgridview-default-sort/
and this doc: http://www.yiiframework.com/doc/api/1.1/CSort#defaultOrder-detail
Regards.

How to do this query condition in cakephp?

How can I make this query the CakePHP way?
SELECT *
FROM uploaded_sales us, sales s
WHERE us.item_id = s.audience_id
The column item_id of uploaded_sales table is not its primary key.
The column audience_id of sales table is not its primary key too.
I tried this one on my model, I'm not getting any errors but it still returns sales as empty:
$reports = $this->find('all',
array(
'joins' => array(
array(
'table' => 'sales',
'alias' => 'Sale',
'type' => 'left',
'conditions' => array('Sale.audience_id' => 'UploadedSale.item_id')
)),
'conditions' => array(
'UploadedSale.month' => $month,
'UploadedSale.year' => $year,
'UploadedSale.company_id' => $company_id,
'UploadedSale.item_type' => $item_type
),
'fields' => $fields
));
return $reports;
Learn about using the Containable behavior in CakePHP:
http://book.cakephp.org/view/1323/Containable
It will make joins a helluva lot easier.