Why Collection showing only first field value
-
12-12-2019 - |
Question
I am just new to Magento and trying to get collection from sales/order_item model i don't know whether i am asking it right way or not but i am having following problem in getting values from db in magento 1.9.0.1
My db table looks like this
-------------------------------------------------------------------------- item_id order_id customid ...... remaining column ..... 1 1 1 2 2 1 3 2 2 4 2 1 5 3 1 --------------------------------------------------------------------------
what i am trying to do is to get customid from order_id value which i have.
but it's showing only first value of customid which is 1 if order_id is 2
i am using below code
public function getCustomValue()
{
$id = $this->getRequest()->getParam('order_id');
$collections = Mage::getModel('sales/order_item')->getCollection()
->addFieldToFilter('order_id', $id);
foreach ($collections as $c) {
$customid = $c->getCustomid();
}
return $customid;
}
So if i use
echo $this->getCustomValue();
it shows 1 only.
but as in my table order_id got 3 entry with 1,2,1 customid
Solution
There are logic issue in your code,you need to return array.Please $customid as Array type variable.
public function getCustomValue()
{
$id = $this->getRequest()->getParam('order_id');
$collections = Mage::getModel('sales/order_item')->getCollection()
->addFieldToFilter('order_id', $id);
// define as array varible
$customid=array();
foreach ($collections as $c) {
// assign the value at array
$customid[] = $c->getCustomid();
}
return $customid;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange