Multiple condition in Join Magento - magento

Can Anyone tell me?
How can i write this type of query in magento
(Multiple condition in on)
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON (Customers.CustomerID=Orders.CustomerID and Orders.status = 1)
//I know this type
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key', array('table_alias.*'));
How can i add multiple condition in JOIN ?

I found the answer myself, It turnout to be very easy
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key and table_alias.columnname = ".."' , array('table_alias.*'));

Related

How to load products from a specific category based on special price availability?

I have following code
$promoCatId = (int)Mage::getStoreConfig('promoprodsec/promoprodgroup/promocategoryid');
$products = Mage::getModel('catalog/category')->setStoreId($store)->load($promoCatId)
->getProductCollection();
$allProductsInPromoCategory = $products->getAllIds();
It gives me all the products of $promoCatId category.
Now I want to get all the products which are not having special price or the special price date range has expired from all the products of $allProductsInPromoCategory
How can I accomplish this without using foreach loop and again loading every product collection again?
foreach ($allProductsInPromoCategory as $promoprod) {
Mage:getModel('catalog/product')->load( $promoprod);
//do validation for the special price criteria
}
Once you state ->getProductCollection() on your category, you end up with a object of type Mage_Catalog_Model_Resource_Product_Collection, which is basically a product collection like all the other.
Since this collection is an EAV collection, you can filter it with the help of the function addAttributeToFilter
And since you actually want an OR (where special price is null or special to date <= now()), you want your two conditions to be in the same addAttributeToFilter
So I would say you can achieve this by doing :
$promoCatId = (int)Mage::getStoreConfig('promoprodsec/promoprodgroup/promocategoryid');
$products = Mage::getModel('catalog/category')
->setStoreId($store)
->load(promoCatId)
->getProductCollection()
->addAttributeToFilter(
array(
array('attribute' => 'special_price', 'null' => true),
array('attribute' => 'special_from_date', 'lt' => Mage::getModel('core/date')->date(Varien_Date::DATETIME_PHP_FORMAT))
)
);
$allProductsInPromoCategory = $products->getAllIds();
echo $products->getSelect();
Useful to know :
I am actually using Mage::getModel('core/date')->date(Varien_Date::DATETIME_PHP_FORMAT) to get the current date relative to the configured locale timezone of the store
echo $products->getSelect(); on a collection will display you the query done by Magento, really useful to actually see if the where is what you really want it to be. In my case it displays :
SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`,
`at_special_price`.`value` AS `special_price`,
IF(at_special_from_date.value_id > 0, at_special_from_date.value,
at_special_from_date_default.value) AS `special_from_date` FROM
`catalog_product_entity` AS `e` INNER JOIN
`catalog_category_product_index` AS `cat_index` ON
cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND
cat_index.category_id = '15' INNER JOIN
`catalog_product_entity_decimal` AS `at_special_price` ON
(`at_special_price`.`entity_id` = `e`.`entity_id`) AND
(`at_special_price`.`attribute_id` = '76') AND
(`at_special_price`.`store_id` = 0) INNER JOIN
`catalog_product_entity_datetime` AS
`at_special_from_date_default` ON
(`at_special_from_date_default`.`entity_id` = `e`.`entity_id`)
AND (`at_special_from_date_default`.`attribute_id` = '77') AND
`at_special_from_date_default`.`store_id` = 0 LEFT JOIN
`catalog_product_entity_datetime` AS `at_special_from_date` ON
(`at_special_from_date`.`entity_id` = `e`.`entity_id`) AND
(`at_special_from_date`.`attribute_id` = '77') AND
(`at_special_from_date`.`store_id` = 1) WHERE
((at_special_price.value IS NULL) OR (IF(at_special_from_date.value_id
> 0, at_special_from_date.value, at_special_from_date_default.value) < '2016-01-08 22:05:14'))

codeigniter join not working

<?php
$this->db->select('*');
$this->db->from('venue');
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
$this->db->join('venue_type vt2', 'vt2.venue_type_id = venue.venue_type_id2');
$this->db->join('venue_subtype vst2', 'vst2.venue_subtype_id = venue.venue_subtype_id2');
$this->db->join('venue_type vt3', 'vt3.venue_type_id = venue.venue_type_id3');
$this->db->join('venue_subtype vst3', 'vst3.venue_subtype_id = venue.venue_subtype_id3');
$this->db->where('venue_id',$id);
$query = $this->db->get();
i have venue table it has more then 1 field relation b/w venue_type. When i try to give first relation
<?php
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
its working fine , but i try to access whole its not working.
Please Help me. (It may simple but i stuck)
By Saravanan.
You need to use alias for multiple joins.
SELECT st_id, table1.us_login, table2.us_login
FROM (stream)
LEFT JOIN users AS table1 ON table1.us_id = stream.st_id_user_from
LEFT JOIN users AS table2 ON table2.us_id = stream.st_id_user_to
see the link: http://codeigniter.com/forums/viewthread/151176/
There's no $this->db->from function in Codeigniter. Use instead $this->db->select('venue.*')

Always count return 0 (zero) from a product collection in Magento

Actually, I am trying to find if this product is in a wishlist or not. So I tried Daniel Sloof's answer in Stack Overflow question Check whether a product is in the wishlist or not, but the product collection always returns 0.
What I tried is here:
$_productCollection1 = Mage::helper('wishlist')
->getProductCollection()
->addFieldToFilter('sku','00114477oo0077');
$_productCollection1->count();
This one returns "0".
To debug, I print the query in directly applied in my database in returning one row.
Using
$_productCollection1->getSelect()->assemble()
and query
SELECT `e` . * , `cat_index`.`position` AS `cat_index_position`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id = e.entity_id
AND cat_index.store_id = '1'
AND cat_index.visibility
IN ( 3, 2, 4 )
AND cat_index.category_id = '2'
WHERE (
e.sku = '00114477oo0077'
)
So what's is wrong here? Is there any other way to do this?
Looking through the other thread you've linked I'm guessing that this way of loading the collection only returns a result for customers that have logged in and have the product in their wishlist.
The reason why the call to getProductCollection() doesn't work is because it is deprecated after version 1.4.2.0, according to offcial Magento documentation (Class Mage_Wishlist_Helper_Data).
Use getWishlistItemCollection() like :
$_productCollection1 = Mage::helper('wishlist')
->getWishlistItemCollection()
->addFieldToFilter('sku','00114477oo0077');
instead of using getProductCollection :
$_productCollection1 = Mage::helper('wishlist')
->getProductCollection()
->addFieldToFilter('sku','00114477oo0077');
Doing so will make the call to $_productCollection1->count() return you the current number of items in the logged user's wishlist.
I solved this by the below function.
function checkInWishilist($_product){
Mage::getSingleton('customer/session')->isLoggedIn();
$session = Mage::getSingleton('customer/session');
$cidData = $session->isLoggedIn();
$customer_id = $session->getId();
if ($customer_id){
$wishlist = Mage::getModel('wishlist/item')->getCollection();
$wishlist->getSelect()
->join(array('t2' => 'wishlist'),
'main_table.wishlist_id = t2.wishlist_id',
array('wishlist_id','customer_id'))
->where('main_table.product_id = '.$_product->getId().' AND t2.customer_id='.$customer_id);
$count = $wishlist->count();
$wishlist = Mage::getModel('wishlist/item')->getCollection();
}
else{
$count="0";
}
if ($count):
return true;
else:
return false;
endif;
}

Subquery nightmares in EF

I'm really, really struggling with what should otherwise be a straightforward query in anything other than LINQ (for example SQL!)
I have two entities:
Product
ProductApprover
The Product entity has a one to many relationship on the ProductApprover entity, e.g:
Product.ProductApprovers gives me all ProductApprover entities relating to the Product.
Getting a Product and associated ProductApprover data is simple enough when querying by my ProductID column on my Product entity as the ProductApprover data associated is bundled into the result automatically, but my problem comes when I want to alter my query by querying data WITHIN my associated ProductApprover entities. I have tried all sorts with use of the 'Where', 'Contains' and 'Any', functions, etc, to perform a subquery, but cannot seem to get the result I want.
The query I want to perform is:
SELECT * FROM Product p
INNER JOIN ProductApprover pa ON p.ProductId = pa.ProductId
WHERE p.ProductId = #id AND pa.Version = #version
Can anybody help me out please? Thank you kindly in advance.
Try this (I guess this is a LINQ interpretation of your SQL query):
int id = 123;
int version = 555;
var results = from p in context.Products
join pa in context.ProductApprovers
on p.ProductId = pa.ProductId
where p.ProductId equals id && pa.Version equals version
select new { Product = p, Approver = pa };
I suspect you want something like this:
var query = from product in db.Products
where product.ProductId == productId
select new {
Product = product,
Approvers = product.Approvers.Where(pa => pa.Version == version)
};
If that doesn't do what you want, could you explain where it falls down?

Symfony Doctrine_Query DELETE with INNER JOIN

I am using symfony+doctrine, and I want to perform a delete query with a join. See below for my code I am currently using that works fine.
$sql = 'DELETE a
FROM a
INNER JOIN b ON a.b_id = b.id
WHERE b.c_id = :c_id';
$pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$stmt = $pdo->prepare($sql);
$params = array('c_id' => $c_id);
$stmt->execute($params);
Anyone know how I can do this using:
Doctrine_Core::getTable('a')
Or
Doctrine_Query::create()->delete()->from('a')
I have had no luck with either.
I just don't really want to use raw SQL in my app.
Something like this should do it
Doctrine_Query::create()
->delete('a a')
->innerJoin('a.b b')
->where('b.c_id = ?', $c_id)
->execute()

Resources