i am a beginner Magento Developer and i have a difficult task to add in a grid a special column.
The column is named Type RMA. I made the sql query but i need to translate it in Magento my query is:
SELECT t1.*, (t3.total_qty_ordered - t2.qty_requested) as 'type_rma'
FROM enterprise_rma_grid as t1
JOIN enterprise_rma_item_entity as t2
ON t1.entity_id = t2.entity_id
JOIN sales_flat_order as t3 ON t1.order_id = t3.entity_id;
So in my _prepareCollection() i have something like these :
$collection = $this->getCollection();
$collection->getSelect()
->join();
And i have no idea how to translate my SQL code in a magento one. My goal is to add the values calculated above as a new column named "type_rma", and display it in my grid as:
$this->addColumn('type_rma', array(
'header' => Mage::helper('enterprise_rma')->__('Type RMA'),
'type' => 'options',
'width' => '100px',
'index' => 'type_rma'
));
Thanks in advance for all your ideas and help.
I found the solution after 10 hours of searching...
$collection->getSelect()
->join(
array('t2' => 'enterprise_rma_item_entity'), 'main_table.entity_id = t2.entity_id',
array('type_rma' => new Zend_Db_Expr("(t3.total_qty_ordered - t2.qty_requested)")
)
)->join(
array('t3' => 'sales_flat_order'), 'main_table.order_id = t3.entity_id',
array()
);
Related
I want make a query which looks like below to filter some products (using attributes) from product collection.
SELECT <attributes>
FROM <tables & joins>
WHERE (<some AND conditions>) OR (<some AND conditions>)
WHERE condition should filter products that match either first set of AND conditions or second set of AND conditions.
Problem is I can't find a way to add an OR condition in between multiple AND conditions.
Can anyone help me to code above where condition using Magento addAttributeToFilter()? or any other functions?
If i'm understanding you correctly I think you need to do some variation of this:
->addAttributeToFilter(...filter here...)
->addAttributeToFilter(array(
array(
'attribute' => 'special_to_date',
'date' => true,
'from' => $dateTomorrow
),
array(
'attribute' => 'special_to_date',
'null' => 1
)
));
which would be:
...filter here... AND (special_to_date >= '2012-07-03' OR special_to_date IS NULL)...
Through Documents provided by Codeigniter Its Cart Library doesnt Update its options We can add the Options like that
$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
)
);
$this->cart->insert($data);
Is there any other way or any tutorial to learn how we can update options of Cart just like
$qid = $this->input->post("qid");
$pairs = $this->input->post("pairs");
$males = $this->input->post("males");
$females = $this->input->post("females");
$data = array(
array(
'rowid' => $qid,
'qty' => 1,
'options' => array('pairs' => $pairs, 'males' => $males, 'females' => $females))
);
$this->cart->update($data);
I have searched it but seems no one has made any fix for it?
Though I have not found any solution therefore I am using simple solution and that is just rest the item in cart and adding new item with same id and updated option values. Its not great trick though but it is just working for me.
I am fallen yesterday like this kind of problem then i create Extending / Override update function. Here how to Extending Native library Link : Create CI Library
Here is my modified code in Github Link
I hope it will help someone and also can modify as their need.
No it did not work for me.. but there is no any errors, I check the array within the _update function as well,
Array
(
[id] => 177
[rowid] => 66bd8895e10f189f62bf3a65ada83630
[qty] => 1
[options] => Array
(
[vat] => 0
[discount] => 0
)
)
I know i'ts just too late but I want to share my solution:
I just got the the options in an array and updated it as I wanted and then update the entire options field in the cart with the array content.
Hope this help somebody.
Sorry for the english.
The way I tried was a bit difficult but it works. You can’t just update a single option value. To update you have to pass all the existing values to all the option values with the value you want to update as well.
If you want to update your Size option
$data = array(
‘rowid’ => $yourRowIdHere,
‘options’ => array ( ‘color’ => $yourExistingValue, ‘length’ => $yourExistingValue, ‘size’ => $newUpdatedValue)
));
$this->cart->update($data);
Give it a try :)
I am able to run AND and OR boolean conditions on Magento using addFieldToFilter and addAttributeToFilter.
But, I am curious about implementing more complex boolean conditions like below:
WHERE (`sku > 5` AND `price` > '10') OR (`name` = 'books')
for AND condition, I have
$collections = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
->addFieldToFilter( 'sku', array('gt' => 5 )
->addFieldToFilter('price', array('gt' => 10);
Which translates to the first parenthesis of the WHERE condition:
(`sku` > 5 AND `price` > 10)
I do not know how to proceed from here to express the OR condition
`name` = 'books'
Any help will be appreciated. Thanks
See my earlier post:
complex boolean conditions on Magento
You can create OR by passing an array:
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('price', array('gt' => 10))
->addAttributeToFilter(
array(
array('attribute'=>'firstname', 'like'=>'test%'),
array('attribute'=>'lastname', 'like'=>'test%'),
)
)
Each additional call to addAttributeToFilter() will ANDed, but firstname / lastname will be ORed.
If you need more complex select statements you can always get the select object from the collection and perform adjustments to the query. Look at Varien_Db_Select for hints :)
$collection->getSelect()->join(..)->orWhere(..); // etc
You can even debug the query to see if it's looking ok:
echo $collection->getSelect()->__toString();
I want to combine with OR "is null" and "eq" => array()
$collection->addAttributeToFilter('attributename', array('is' => null));
$collection->addAttributeToFilter('attributename', array(115,116));
But this does not work.
To use addAttributeToFilter() with an OR condition you can pass an array of arrays like this:
$collection->addAttributeToFilter(
array(
array(
'attribute' => 'name_of_attribute_1',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'name_of_attribute_2',
'in' => array(115, 116)
)
)
);
The definition of filtering against NULL keywords may look somewhat confusing at first glance, but it's plain simple, once you get used to it.
Magento supports two filter types of type string, namely 'null' and 'notnull', for comparisions against NULL keywords.
Be aware, that even though you need to pass key=>value pairs (because an associative array is used), the value will be always ignored, when filter type 'null' or 'notnull' is used.
Example
var_dump(
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter(
array(
array(
'attribute' => 'description',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'sku',
'in' => array(115, 116)
)
)
)
->getSelectSql(true)
);
The output of the dump may vary depending on your shop configuration (attribute ids, count of stores, etc.), but the OR logic in the WHERE clause should always remain the same:
SELECT
`e`.*,
IFNULL(_table_description.value, _table_description_default.value) AS `description`
FROM
`catalog_product_entity` AS `e`
INNER JOIN
`catalog_product_entity_text` AS `_table_description_default` ON
(_table_description_default.entity_id = e.entity_id) AND
(_table_description_default.attribute_id='57') AND
_table_description_default.store_id=0
LEFT JOIN
`catalog_product_entity_text` AS `_table_description` ON
(_table_description.entity_id = e.entity_id) AND
(_table_description.attribute_id='57') AND
(_table_description.store_id='1')
WHERE (
(IFNULL(_table_description.value, _table_description_default.value) is NULL) OR
(e.sku in (115, 116))
)
I have product attribute is_expired with Yes and No value and i want only those product have NO value.below query work for me that get record that have neq 1 and not null.
Combine not equal to and null condition WITH OR.
$collection->addAttributeToFilter('attribue_name', array('or'=> array(
0 => array( 'neq' => '1'),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
You can changed neq with eq.
To use addAttributeToFilter with an OR condition and with left join
you can do something like that:
$collection->addAttributeToFilter(
array(
array('attribute' => 'name_of_attribute_1','null' => 'this_value_doesnt_matter'),
array('attribute' => 'name_of_attribute_2','in' => array(115, 116))
)
'',
'left'
);
This is my CActiveDataProvider
$dataProvider = new CActiveDataProvider ('MyTable', array (
'pagination' => array (
'PageSize' => 4,
),
'criteria' => array (
'condition' => 'from_user_id='.$user->id,
'order' => 'date DESC',
),
));
My question is: I would like to have this CActiveDataProvider ordered first by date (as the posted code does), and in a second level, within those with the same date, order by a second criteria. Is this possible?
Maybe I'm not understanding you correctly, but won't the following work? :
'order' => 'date DESC, name DESC',