How to translate this query into Criteria with propel?
I try to do the query with Symfony and Propel's Criteria, but it's doesn't work :
SELECT *
FROM `produit`
WHERE `nom` LIKE '%parasol%'
OR `chapeau` LIKE '%parasol%'
OR `description` LIKE '%parasol%'
This is my query with Propel :
$c = new Criteria();
$c->addOr(ProduitPeer::NOM, '%' . $search. '%', Criteria::LIKE);
$c->addOr(ProduitPeer::DESCRIPTION, '%' . $search. '%', Criteria::LIKE);
$c->add(ProduitPeer::CHAPEAU, '%' . $search. '%', Criteria::LIKE);
$req = ProduitPeer::doSelect($c);
The result of this is :
SELECT *
FROM produit
WHERE produit.NOM LIKE '%parasol%'
AND开发者_如何学编程 produit.DESCRIPTION LIKE '%parasol%'
AND produit.CHAPEAU LIKE '%parasol%'
How to make a query with 'OR' ??
The only thing missing is the 'Or' from the last add:
$c = new Criteria();
$c->addOr(ProduitPeer::NOM, '%' . $search. '%', Criteria::LIKE);
$c->addOr(ProduitPeer::DESCRIPTION, '%' . $search. '%', Criteria::LIKE);
$c->addOr(ProduitPeer::CHAPEAU, '%' . $search. '%', Criteria::LIKE);
$req = ProduitPeer::doSelect($c);
When dealing with ORs you have to use criterion which aren't the easiest things to understand. With the latest version of Propel that they are currently working on, the criteria object is going to change completely and become much more intuitive. But until then...
$c = new Criteria();
$c1 = $c->getNewCriterion(ProduitPeer::NOM, '%'.$search.'%', Criteria::LIKE);
$c2 = $c->getNewCriterion(ProduitPeer::DESCRIPTION, '%'.$search.'%', Criteria::LIKE);
$c3 = $c->getNewCriterion(ProduitPeer::CHAPEAU, '%'.$search.'%', Criteria::LIKE);
$c2->addOr($c3);
$c1->addOr($c2);
$c->add($c1);
$req = ProduitPeer::doSelect($c);
精彩评论