CakePHP paginator isn't sorting - sorting

In CakePHP, I would like to sort my index list (created by the paginator component) on sequence ASC, but it won't work. If I see the query setup in the CakePHP docs (http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#query-setup), my paginator settings should look like this:
public function index()
{
$this->Paginator->settings = array(
'Attraction' => array(
'conditions' => array(
'Attraction.deleted' => null
),
'order' => array(
'Attraction.sequence ASC',
'Attraction.id ASC'
),
'limit' => 15
)
);
$attractions = $this->Paginator->paginate('Attraction');
$this->set('attractions', $attractions);
}
But every time I load my index file, the list is sorted on ID and ignores the "order" setting. Can anybody tell me if there's anything wrong with my "order" item in my paginator settings? ;)
Thx!

try
'order' => array(
'Attraction.sequence' => 'asc',
'Attraction.id' => 'asc'
),
edit:
The sorting direction must be in the array value, while the field name should be the index. The examles I found in the documentations are always showing that

I don't think you should have that Attraction index in your options array - in other words, I think your options array should look like this:
$this->Paginator->settings = array(
'conditions' => array(
'Attraction.deleted' => null
),
'order' => array(
'Attraction.sequence ASC',
'Attraction.id ASC'
),
'limit' => 15
);

Related

Sort by custom field (acf) date picker and publish date

I would like to sort posts using firstly the custom field date picker by ACF plugin for wordpress, and then when that's non existing, by the published date.
Our theme works with cases so I cooked up something like this but I failed misearbly, any suggestions ?
case 'concertdatum':
$wp_query_args['meta_query'] = array(
'relation' => 'AND',
'datum_clause' => array(
'meta_key' => 'datum',
'meta_type' => 'DATETIME'
),
'publish_clause' => array(
'column' => 'post_date_gmt',
'compare' => 'EXISTS',
),
)
$wp_query_args['order'] = array(
'datum_clause' => 'DESC',
'publish_clause' => 'DESC'
);
break;

Magento 1.9: sort grid view

I've added a column to my gridview and it suppose to show remaining days of an event.
$this->addColumn('calculate_days', array(
'header' => Mage::helper('myodule')->__('Remaining'),
'type' => 'options',
'width' => '200px',
'options' => $options,
'frame_callback' => array($this, 'getRemaining'),
'filter_condition_callback' => array($this, '_calculateFilter')
));
getRemaining method:
public function getRemaining($value, $row, $column)
{
// some calculation to find out remaining days based on some conditions
return $days;
}
If I don't specify the index key, clicking on the Remaining column does not work. So I've added index key to addColumn like bellow:
$this->addColumn('calculate_days', array(
'header' => Mage::helper('myodule')->__('Remaining'),
'type' => 'options',
'width' => '200px',
'index' => 'created_at',
'options' => $options,
'frame_callback' => array($this, 'getRemaining'),
'filter_condition_callback' => array($this, '_calculateFilter')
));
now the sort is WORK but not correctly. when I specify the index, sort applied by index column(in this case will sort by created_at) but I want to sort by remaining days.
what should I do to solve this problem? or is there any sort callback?
Why don't you add this value as a new field in this grid? And then update this field values each day by a cronjob.
Then you will have in a native way the items sorted and also you avoid to recalculate each value each time you filter o see this grid.
Hope it helps,
Regards

Yii Sortable Column in CGridView

I have a calculated column I wanna have sortable.
I have a get method for it...
public function getCur_margin() {
return number_format(($this->store_cost / $this->store_price) / $this->store_cost * 100, 2) . '%';
}
And I followed this thread...
CGridview custom field sortable
But now it's looking for an actual column in the database. How can I fetch a field using <calculations> AS cur_margin only temporarily for this CActiveDataProvider?
Here is the controller code for the provider:
$dataProvider = new CActiveDataProvider('Products', array(
'criteria' => array(
'condition' => 'store_id IN (SELECT `id` FROM `stores` WHERE `user_id` = ' . Yii::app()->user->id . ')',
),
'pagination' => array(
'pageSize' => 15,
),
'sort'=>array(
'attributes'=>array(
'cur_margin' => array(
'asc' => 'cur_margin ASC',
'desc' => 'cur_margin DESC',
),
),
),
));
Have you tried to specify your custom field in select property of CDbCriteria?
'select' => array(
..., // fields to select
'(<calculations>) AS cur_margin'
)
You don't have to declare getCur_margin() method then but only declare a public $cur_margin member in your model. All required calculations will be done in SQL query.
You can also refer to this thread as an example.

How to get the custom values and image in Magento in Topmenu?

I'm using magento 1.7.0.2. I have add the custom value in database. But how to retrive the custom value and image in Topmanu. I have tried in below mentioned code in the palce of 'my_attribute' to replace my attribute, but i din't get the result.
Model: Mage_Catalog_Model_Observer
Method: _addCategoriesToMenu()
$categoryData = array(
'name' => $category->getName(),
'id' => $nodeId,
//'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
'is_active' => $this->_isActiveMenuCategory($category),
'my_attribute' => $category->getData('my_attribute') // Add our data in...
);
When i print the array i'll get this,
Array ( [name] => Matelas [id] => category-node-31 [is_active] => 1 [my_attribute] => )
Can any one guide me, Thanks in advance...
I am guessing you mean you have added a new custom attribute to the Category entity?
Becuase you are dealing with a Node_collection the full category object won't be loaded, try loading the full object to get what you're after:
$cat = Mage::getModel('catalog/category')->load($category->getId());
$categoryData = array(
name' => $category->getName(),
'id' => $nodeId,
//'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
'is_active' => $this->_isActiveMenuCategory($category),
'my_attribute' => $cat->getData('my_attribute') // Add our data in...
);

Add product with custom option to existing Order

$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)
->setQuote(Mage::getModel('sales/quote')->load($order->getQuoteId()));
$orderItem = Mage::getModel('sales/convert_quote')->itemToOrderItem($quoteItem)->setProduct($product);
this is the code i use to add a simple product to existing order , but i am having issues adding custom option product to the order.
$quoteItem->addOption(new Varien_Object(
array(
'product' => $quoteItem->getProduct(),
'code' => 'option_ids',
'value' => 1 // 45,46,55
)
));
$quoteItem->addOption(new Varien_Object(
array(
'product' => $quoteItem->getProduct(),
'code' => 'option_1', //45
'value' => 2 // ‘White’
)
));
after the first line , but no success.
Any help please.
Thanks
There is one method to change exitsting order:
http://prattski.com/2013/04/22/magento-adding-items-to-existing-orders/

Resources