I have created a grid in Magento and there is a column which is not coming from database instead I have computed it's value from other columns using renderer.
lets say custom column in grid is total = columnA from DB - columnB from DB, have to sort the grid based on custom column.
I overrided setCollectionToOrder function in my grid., sorted the collection received from prepareCollection function and put the sorted collection in new collection object but I then my grid doesnt show any row , though I can echo the sorted collection and it works fine but no rows come in grid.
protected function _setCollectionOrder($column)
{
$collection = $this->getCollection();
if ($collection) {
switch ($column->getId()) {
case 'total':
$arr = array();
foreach($collection as $item) {
$colA= $item->getcolumnA();
$colB= $item->getcolumnB()
$total= $colA- $colB
$item->setTotal($total);
$arr[$i] = $item; $i++ ;
}
if($column->getDir()=='asc') {
$sorted = usort($arr, array('Grid_Class', '_cmpAscTotal'));
} else {
$sorted = usort($arr, array('Grid_Class', '_cmpDescTotal'));
}
$collection = $this->_tempCollection(); // A blank collection
for($i=0;$i<count($arr);$i++) {
$arr[$i]->setTotal(1);
$collection->addItem($arr[$i]);
}
$this->setCollection($collection);
break;
default:
parent::_setCollectionOrder($column);
break;
}
}
return $this;
}
tempCollection function just gives me a blank collection object (same what prepare collection function gives)
_cmpAscTotal is callback function which defines my custom sorting.
protected function _prepareCollection()
{
$collection = Mage::getModel('module/model')->getCollection();
$collection->getSelect()->joinLeft(array('table1' => 'table1'),
'table1.sku = main_table.sku_id',
Array('columnA, columnB, (1) as total')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Is there a better way of achieving sorted collection on a custom column, if not what I am doing wrong while modifying the collection that grid becomes empty
Actually, you should extend collection class and add your custom sorting in your collection _afterLoad method. If for some reason it isn't possible - you should do it in your grid _afterLoadCollection method. In any case, you can't/shouldn't/haven't do it in collection _setCollectionOrder method. Because if you look into Mage_Adminhtml_Block_Widget_Grid::_prepareCollection() code - you will see that _setCollectionOrder is called before collection load.
Updated:
protected function _afterLoadCollection()
{
foreach ($this->getCollection() as $item) {
$item->setTotal($item->getcolumnA() - $item->getcolumnB());
}
usort($this->getCollection()->getIterator(), array('Grid_Class', '_cmpAscTotal'));
return $this;
}
Thanks Zyava
I had to do it like -
$arr = $this->getCollection()->getItems();
if($dir=='asc') {
$sorted = usort($arr, array('Grid_Class', '_cmpAscSuggestion'));
} else {
$sorted = usort($arr, array('Grid_Class', '_cmpDescSuggestion'));
}
$this->getCollection()->setItems($arrt); // created a set item function in collection class for this module.
It sorts the items in collection.
Related
I have a collection of lists, each List has an array of items. I need to group items by id on each list.
When I try to reassign $list->items, nothing happens, but if I reassign $list->name, it works, so the problem is arrays or arrays properties of an object, or something like that...
What do I have to do to reassign an array inside a collection?
$lists = Listt::with('items')->get();
$newLists = $lists->map(function(Listt $list) {
// $list->items = $list->items->groupBy('id'); // this is what I want, it doesn't work
// $list->items = []; // this doesn't work either
// $list['items'] = []; // nope
// $list->items = new Collection(); // nope
$list->items = collect([... $list->items])->groupBy('id');// nope
$list->name = 'test'; //this works, so the problem is arrays <----
return $list;
});
return $newLists;
/* transform doesn't work */
// $lists->transform(function(Listt $list) {
//...
// });
// return $lists;
/* this, from a similar question, doesn't work either, besides I don't understand it and it doesn't use collection methods... */
// foreach($lists as &$list){
// $list['items'] = [];
// unset($list);
// }
// return $lists;
Because items is already a relationship, I don't really want to reuse that as an attribute.
I suggest you assign the grouped by items to a new attribute on each list. I names that grouped_items:
return List::with('items')
->get()
->each(function ($list) {
$list->grouped_items = $list->items->groupBy('id');
});
If you insist on assigning to items. you can still do so but I don't recommend this:
return List::with('items')
->get()
->each(function ($list) {
$items = $list->items;
// This step might not be necessary.
$list->unsetRelation('items');
$list->setRelation('items', $items->groupBy('id'));
});
Trying to save data while open page but stuck at error :
"Method Illuminate\Database\Eloquent\Collection::save does not exist."
I have 2 database :
Buffalodata
Buffalomilkrecord
From 2nd table i need to get the avg of totalmilk and update the same to main database (1). This help me to show updated avgmilk data on dashboard front page.
Route:
Route:: get('buffalo-details', 'App\Http\Controllers\BuffalodataController#buffalodetails');
BuffalodataController Controller :
public function buffalodetails()
{
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->get('buffaloID')->pluck('buffaloID')->toArray();
foreach ($buffalidforavgmilk as $id )
{
$milkperid = Buffalomilkrecord::where('buffaloID', $id)->sum('totalmilk');
$avgbuffalocount = Buffalomilkrecord::where('buffaloID',$id)->count();
$getavg = $milkperid / $avgbuffalocount;
$data = Buffalodata::find($buffalidforavgmilk);
$data->avgmilk = ($getavg);
$data->save ();
// dump([$milkperid,$avgbuffalocount,$getavg,$data,$id]);
}
return view ('pages.Buffalo.BuffaloDetails',[---------]);
}
Thanks again in Advance
When you pass an Array to ::find(), it returns a Collection, which doesn't have a save() method. This is your code:
// This is an Array of `buffaloID` values
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->get('buffaloID')->pluck('buffaloID')->toArray();
...
// `$data` is now a `Collection` of `Buffalodata` instances
$data = Buffalodata::find($buffalidforavgmilk);
// This now fails, as `Collection` doesn't have a `save()` method
$data->save();
You can rewrite your code as follows:
Buffalodata::whereIn('buffaloID', $buffalidforavgmilk)->update(['avgmilk' => $getavg]);
This will update all records in a single call. If you want to iterate, that's an option too:
$data = Buffalodata::find($buffalidforavgmilk);
foreach ($data as $record) {
$record->avgmilk = $getavg;
$record->save();
}
Or, since you have $id already:
$record = Buffalodata::find($id);
$record->avgmilk = $getavg;
$record->save();
I try to update or create a table with data from request, on update it works, but on create I can do just for 1. So, if I have 4 new items, just 1 is saved, not sure why, and no errors. If I dump the request I have all values.
$answers = new LkpAnswer;
foreach ($request->items as $key => $item) {
if (isset($item['id'])) {
$answers->where('id', $item['id'])->update($item);
} else {
$answers->fill($item)->save();
}
}
Please Try the following code:
foreach ($request->items as $key => $item) {
if (isset($item['id'])) {
LkpAnswer::where('id', $item['id'])->update($item);
} else {
$answers = new LkpAnswer; //new up an empty model(object) then fill it with your array of data, and finally save it.
$answers->fill($item)->save();
}
}
This should be achievable neatly with built in methods:
foreach ($request->items as $item) {
LkpAnswer::updateOrCreate($item['id'],$item);
}
Make sure that the relevant fields are fillable - see mass assignment protection.
I have a product table and device table.
I have joined these two tables where I have selected device.id & product.name, now I want to insert this data into my existing device table using device.id.
I want to insert multiple dropdown values in my database. Can anyone tell me how can I solve this problem? Below is my Controller Code-
foreach ($request->all() as $value)
{
$deviceById= Device::find($request->id);
if($request->destination_type == 'dealer')
{
$deviceById->destination_location=$value->dealer_id;
}else
{
$deviceById->destination_location=$value->office_id;
}
$deviceById->save();
}
flash('Device Dispatch added successfully!')->success()->important();
return redirect()->back();
You can make array of all dropdowns value and convert into a json string and store it in database
You can do something like this. Change according to your requirement. It just a logic demo.
$deviceById= Device::find($request->id);
$destination = array();
foreach ($request->all() as $value)
{
if($request->destination_type == 'dealer')
{
$destination[] = $value->dealer_id;
}else
{
$destination[] = $value->office_id;
}
}
$jsonString = json_encode($destination);
$deviceById->destination_location = $jsonString
$deviceById->save();
flash('Device Dispatch added successfully!')->success()->important();
return redirect()->back();
I solve it this way.I had used Elequent ORM
if($request->destination_type == 'Dealer')
{
DB::table('devices')
->whereIn('id',$request->device_list)
->update(['dealer_id'=>$request->destination_location,'device_status'=>"Dispatch"]);
flash('Device Dispatch dealer added successfully!')->success()->important();
}else
{
DB::table('devices')
->whereIn('id',$request->device_list)
->update(['office_id'=>$request->destination_location,'device_status'=>"Dispatch"]);
flash('Device Dispatch office added successfully!')->success()->important();
}
How do I add custom sort option in Magento. I want to add Best Sellers, Top rated and exclusive in addition to sort by Price. Please help
For Best Sellers
haneged in code/local/Mage/Catalog/Block/Product/List/Toolbar.php method setCollection to
public function setCollection($collection) {
parent::setCollection($collection);
if ($this->getCurrentOrder()) {
if($this->getCurrentOrder() == 'saleability') {
$this->getCollection()->getSelect()
->joinLeft('sales_flat_order_item AS sfoi', 'e.entity_id = sfoi.product_id', 'SUM(sfoi.qty_ordered) AS ordered_qty')
->group('e.entity_id')->order('ordered_qty' . $this->getCurrentDirectionReverse());
} else {
$this->getCollection()
->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}
return $this;
}
After setCollection I added this method:
public function getCurrentDirectionReverse() {
if ($this->getCurrentDirection() == 'asc') {
return 'desc';
} elseif ($this->getCurrentDirection() == 'desc') {
return 'asc';
} else {
return $this->getCurrentDirection();
}
}
And finally I changed mehod setDefaultOrder to
public function setDefaultOrder($field) {
if (isset($this->_availableOrder[$field])) {
$this->_availableOrder = array(
'name' => $this->__('Name'),
'price' => $this->__('Price'),
'position' => $this->__('Position'),
'saleability' => $this->__('Saleability'),
);
$this->_orderField = $field;
}
return $this;
}
for Top rated
http://www.fontis.com.au/blog/magento/sort-products-rating
try above code.
for date added
Magento - Sort by Date Added
i am not associate with any of the above link for any work or concern it is just for knowledge purpose and to solve your issue.
hope this will sure help you.
Thanks for your answer, Anuj, that was the best working module I could find so far.
Just add an extra bit to your code in order to solve no pagination caused by 'group by'
Copy '/lib/varien/data/collection/Db.php'
To 'local/varien/data/collection/Db.php'.
Change the getSize function to
public function getSize()
{
if (is_null($this->_totalRecords)) {
$sql = $this->getSelectCountSql();
//$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams); //============================>change behave of fetchOne to fetchAll
//got array of all COUNT(DISTINCT e.entity_id), then sum
$result = $this->getConnection()->fetchAll($sql, $this->_bindParams);
foreach ($result as $row) {//echo'<pre>'; print_r($row);
$this->_totalRecords += reset($row);
}
}
return intval($this->_totalRecords);
}
Hope it could help anyone.
update
The filter section need to be updated as well, otherwise just showing 1 item on all filter.
and the price filter will not be accurate.
What you need to do it to modify core/mage/catalog/model/layer/filter/attribute.php and price.php
attribute.php getCount() on bottom
$countArr = array();
//print_r($connection->fetchall($select));
foreach ($connection->fetchall($select) as $single)
{
if (isset($countArr[$single['value']]))
{
$countArr[$single['value']] += $single['count'];
}
else
{
$countArr[$single['value']] = $single['count'];
}
}
//print_r($countArr);//exit;
return $countArr;
//return $connection->fetchPairs($select);
Price.php getMaxPrice
$maxPrice = 0;
foreach ($connection->fetchall($select) as $value)
{
if (reset($value) > $maxPrice)
{
$maxPrice = reset($value);
}
}
return $maxPrice;
If you are having the same problem and looking for the question, you will know what I meant.
Good luck, spent 8 hours on that best sell function.
Update again,
just found another method to implement
using cron to collect best sale data daily saved in a table that includes product_id and calculated base sale figure.
then simply left join, without applying 'group by'
that means core functions do not need to changed and speed up the whole sorting process.
Finally finished! hehe.
To sort out pagination issue for custom sorting collection rewrite the resource model of it's collection from
app\code\core\Mage\Catalog\Model\Resource\Product\Collection.php
And modify below method from core
protected function _getSelectCountSql($select = null, $resetLeftJoins = true)
{
$this->_renderFilters();
$countSelect = (is_null($select)) ?
$this->_getClearSelect() :
$this->_buildClearSelect($select);
/*Added to reset count filters for Group*/
if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) {
$countSelect->reset(Zend_Db_Select::GROUP);
}
/*Added to reset count filters for Group*/
$countSelect->columns('COUNT(DISTINCT e.entity_id)');
if ($resetLeftJoins) {
$countSelect->resetJoinLeft();
}
return $countSelect;
}
Above will solve count issue for custom sorting collection.