Magento 1.9.1.1 - Duplicate results displayed - magento

I have a Magento issue. I'm using Magento ver. 1.9.1.1.
The issue is that I am seeing duplicate results for a page which contains a list of jobs (its a recruitment page). The problem is each of the results is duplicated. I've never done any real Magento development before and it seems like a steep learning curve.
I've checked the content and that has only been entered once.
This is the offending code:
//I tried this line with no effect
//$collection = Mage::getModel('cms/page')->getCollection()->distinct(true);
// $collection contains the duplicate results
$collection = Mage::getModel('cms/page')->getCollection();
Can anyone give me any ideas on how I can solve this? Even an idea of where to look in the code would be good.
I've found two data structures when iterating through the collection. These are _origData and _Data. Don't know why its using both of these but I managed to fix/hack it by doing:
if($key == "_origData"){
continue;
}
Surely there's a better way to do this?
Thank you in advance :)

Have you tried applying filters to your collection?
See below:
$getStoreId = Mage::app()->getStore()->getId();
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter($getStoreId)
->addFieldToFilter('is_active', 1);
Resources:
Class Mage_Cms_Model_Mysql4_Page_Collection

You can see the code details on above mentioned Model class.But if you want custom code you can try like this :
$collection = Mage::getModel('cms/page')->getCollection();
$collection->getSelect()
->join(
array('s' => $collection->getTable('cms/page_store')),
's.page_id = main_table.page_id AND s.store_id != 0',
array('store_id')
)
->columns(array('stores_count' => new Zend_Db_Expr('COUNT(s.store_id)')))
->group('main_table.page_id')
->having('stores_count = ?', 1)
->having('s.store_id = ?', $storeId)
;

Related

How to correctly combine where and or_where (with foreach loop)

I am trying the following code:
//NEEDS TO BE 'OR's
foreach ($market_ids as $market_id) {
$this->CI->db->or_where('market_id',$market_id,FALSE);
}
//NEEDS TO BE 'AND'
$this->CI->db->where('market_product.product_id',$product_id,FALSE);
$this->CI->db->from('market_product');
$this->CI->db->join('product', 'market_product.product_id = product.product_id');
by the result i see that 'where' with the market_product.product_id it also hads a "and".
What i need is ('OR's for the markets and one 'AND' for the product_id.)
but whatever i tried didn't work.. I also looked at other similar questions in stackoverflow but none of them contains the 'foreach' thing or how to solve it.
Any assistance would be appreciated. I know version 3.0 would solve it by grouping but its not officially released yet.
a way to solve your problem
//NEEDS TO BE 'OR's
$where_or = array();
foreach ($market_ids as $market_id) {
// $this->CI->db->or_where('market_id',$market_id,FALSE);
$where_or[] = "market_id = $market_id";
}
$this->CI->db->where("(".implode(' OR ',$where_or).")");
//NEEDS TO BE 'AND'
$this->CI->db->where('market_product.product_id',$product_id,FALSE);
$this->CI->db->from('market_product');
$this->CI->db->join('product', 'market_product.product_id = product.product_id');
hope this will work for you

Merging multiple objects which uses same id

I'm trying to merge multiple objects (like Receipts, Reports, etc) with Collection->merge().
This is the code I used:
$receipts = Receipt::all();
$reports = Report::all();
$collection = $receipts->merge($reports);
This is the result:
The above screenshot shows two elements, but the third element is missing because it has the same id (id: "1") as the first one. What I'm trying to achieve is to display all three of them as a collection.
EDIT:
I need the result to be objects (collection) because I also use the code on my view, where I check the class to determine what to display. Also, I use this function to sort the objects in the collection.
$collection->sort(function($a, $b)
{
$a = $a->created_at;
$b = $b->created_at;
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
});
I know that this is an old question, but I will still provide the answer just in case someone comes here from the search like I did.
If you try to merge two different eloquent collections into one and some objects happen to have the same id, one will overwrite the other. I dunno why it does that and if that's a bug or a feature - more research needed. To fix this just use push() method instead or rethink your approach to the problem to avoid that.
Example of a problem:
$cars = Car::all();
$bikes = Bike::all();
$vehicles = $cars->merge($bikes);
// if there is a car and a bike with the same id, one will overwrite the other
A possible solution:
$collection = collect();
$cars = Car::all();
$bikes = Bike::all();
foreach ($cars as $car)
$collection->push($car);
foreach ($bikes as $bike)
$collection->push($bike);
Source: https://medium.com/#tadaspaplauskas/quick-tip-laravel-eloquent-collections-merge-gotcha-moment-e2a56fc95889
I know i'm bumping a 4 years old thread but i came across this and none of the answers were what i was looking for; so, like #Tadas, i'll leave my answer for people who will come across this. After Looking at the laravel 5.5 documentation thoroughly i found that concat was the go-to method.
So, in the OP's case the correct solution would be:
$receipts = Receipt::all();
$reports = Report::all();
$collection = $receipts->concat($reports);
This way every element in the Report collection will be appended to every element in the Receipts collection, event if some fields are identical.
Eventually you could shuffle it to get a more visual appealing result for e.g. a view:
$collection->shuffle();
Another way to go about it is to convert one of your collections to a base collection with toBase() method. You can find it in Illuminate\Support\Collection
Method definition:
/**
* Get a base Support collection instance from this collection.
*
* #return \Illuminate\Support\Collection
*/
public function toBase()
{
return new self($this);
}
Usage:
$receipts = Receipt::all();
$reports = Report::all();
$collection = $receipts->toBase()->merge($reports);
You could put all collections in an array and use this. Depends on what you want to do with the collection.
$list = array();
$list = array_merge($list, Receipt::all()->toArray());
$list = array_merge($list, Report::all()->toArray());

Magento 1.7: Unable to incorporate joinField in product collection

I'm having a problem where I can't get add joinField() to a product collection.. I have no clue why it doesn't work because it should be really simple or throw some errors at least. Needless to say, it is driving me nuts. I'm interested in looking at products and the total dollar amount in sales from them. This is what I have from a book called "Magento PHP Developer's Guide" and the Magento Wiki.
public function getProducts($categoryId) {
$productCollection = Mage::getModel('catalog/category')->load($categoryId)
->getProductCollection()
->joinField('o', 'sales_flat_order_item', array('o.row_total', 'o.product_id'), 'main_table.entity_id = o.product_id');
}
// die; when uncommented, this function WILL NOT die here
return $productCollection;
}
I'm getting the ->joinField() method right out of the book, but it doesn't grab any product. Strangely, the function doesn't even return anything because when the die line is uncommented, the function does not terminate there. Instead, the front-end will simply just skip this function without throwing any errors (that I can see at this time) and just doesn't display any blocks using this function. What am I missing here?
It works when I remove joinField() like below.
$productCollection = Mage::getModel('catalog/category')->load($categoryIds)
->getProductCollection();
UPDATE:
Further testing show that the following works. Note that if I use main_table instead of e, it does not work. If I look at the query generated from this, main_table is not replaced by the main table; instead, query contains the literal string "main_table".
$productCollection = Mage::getModel('catalog/category')->load($categoryIds)
->getSelect()
->join(array('o' => 'sales_flat_order_item'),
'e.entity_id = o.product_id',
'o.row_total'
);
While this doesn't.
$productCollection = Mage::getModel('catalog/category')->load($categoryIds)
->joinTable(
array('o' => 'sales_flat_order_item'),
'e.entity_id = o.product_id',
'o.row_total'
);
Maybe I don't see some simple mistake.. but I just don't see what's wrong.
public function getProducts($categoryId) {
$productCollection = Mage::getModel('catalog/category')->load($categoryId)
->getProductCollection()
->joinTable( // JoinTable makes more sense.
array('o' => 'sales/order_item'),
'main_table.entity_id = o.product_id'
array('row_total'),
)
;
return $productCollection;
}
It will probably return all fields in the catalog_product_entity table PLUS the row_total from the sales_order_item table. You might be able to use addAttributeToSelect('o.product_id') right before the join, just to clear the unwanted fields.

In Prestashop, how to get category of last product added?

I don't have that much experience with Prestashop, php, and Smarty.
How do I get the category of lastProductAdded?
I am trying to make the "continue shopping" button redirect to the category of the last product added.
« {l s='Continue shopping'}
The following code doesn't seem to work, giving category id of 0 for some reason. (I have no idea whether it makes sense either)
Any help would be much appreciated. Thank you!
(The variable lastProductAdded and function getCategoryLink are already defined in-built)
For Prestashop 1.4.x you need to modificate Cart::getLastProduct() with this code:
public function getLastProduct()
{
$sql = '
SELECT cp.`id_product`, cp.`id_product_attribute`, p.`id_category_default`
FROM `'._DB_PREFIX_.'cart_product` cp
JOIN `'._DB_PREFIX_.'product` p ON (cp.`id_product` = p.`id_product`)
WHERE `id_cart` = '.(int)($this->id).'
ORDER BY cp.`date_add` DESC';
$result = Db::getInstance()->getRow($sql);
if ($result AND isset($result['id_product']) AND $result['id_product'])
return $result;
return false;
}
Regards
you need to use $lastProductAdded.id_category_default instead of $lastProductAdded.category->id
Regards

Magento Filter By Relative Value

I would like to filter a collection by relative value per that row. For example,
SELECT * FROM table WHERE column_1 > column_2
The only thing I know how to do in Magento would be
$q = Mage::getModel('table')->getCollection()
->addAttributeToFilter('column_1', array('gt' => $some_number));
or something of that sort. I can only give it a value to compare against, not a column name. I also looked at the Zend_Db_Select method at the where clause but didn't find anything that would help. Do I actually have to go all the way down to a direct SQL query (something which is, of course, avoided at all costs)? (I'm running Magento 1.3.2.4)
Thank you.
Try something like
$q = Mage::getModel('table')->getCollection()
->addAttributeToSelect('column_1')
->addAttributeToSelect('column_2')
->addAttributeToFilter('column_1', array('gt' => Zend_Db_Expr('`column_2`')));
OK, this is probably not the ideal solution, but it's one way of getting what you need.
This is how I got a collection of products that were on sale, where a products final price is lower than its price.
I first made a new empty data collection. Then defined the collection I was going to loop through filtering what I could first. After that I looped through that collection and any products that matched my requirements got added to the empty collection.
$this->_itemCollection = new Varien_Data_Collection();
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addCategoryFilter($category)
->addAttributeToSort('position', 'asc');
$i=0;
foreach($collection as $product){
if($product->getFinalPrice() > $product->getPrice() && !$this->_itemCollection->getItemById($product->getId())){
$this->_itemCollection->addItem($product);
$i++;
}
if($i==$this->getProductsCount()){
break;
}
}
$this->setProductCollection($this->_itemCollection);

Resources