Doctrine orm Doctrine2通过多对一映射查找
我有两个具有单向多对一映射的实体 以下是Doctrine orm Doctrine2通过多对一映射查找,doctrine-orm,many-to-one,arraycollection,findby,Doctrine Orm,Many To One,Arraycollection,Findby,我有两个具有单向多对一映射的实体 以下是产品: use Doctrine\Common\Collections\ArrayCollection; /** * @Entity * @Table(name="Product") * @gedmo:TranslationEntity(class="GPos_Model_Translation_ProductTranslation") */ class GPos_Model_Product extends GPos_Doctrine_Activ
产品
:
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="Product")
* @gedmo:TranslationEntity(class="GPos_Model_Translation_ProductTranslation")
*/
class GPos_Model_Product extends GPos_Doctrine_ActiveEntity {
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
protected $id;
/**
* @ManyToMany(targetEntity="GPos_Model_Category")
* @JoinTable(name="products_categories",
* joinColumns={@JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="category_id", referencedColumnName="id")}
* )
*/
protected $categories;
public function __construct() {
$this->categories = new ArrayCollection();
}
public function addCategory(GPos_Model_Category $category) {
if (!$this->categories->contains($category))
$this->categories->add($category);
}
}
如您所见,$categories是GPO\u Model\u类别实体的数组集合
现在怎么办?
现在我想检索给定类别中的所有产品,以及不在给定类别中的所有产品
我试过$products=GPos\u Model\u Product::findByCategories($category->getId())代码>
但这只给了我
SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在第1行“=”1”附近使用的正确语法,并且“$category
”的ID为1,因此我想这不是正确的方法。有人知道怎么处理吗
谢谢大家! 多亏了您,我终于找到了如何选择某个类别中的所有产品
我稍微调整了一下他的解决方案,这样我就可以传递一组类别实体,它将找到这些类别中的所有产品。如果您提供多个实体,它将返回至少属于一个给定类别的任何产品
这是我的调整(我在我的产品实体中找到了这个函数):
你可以这样称呼它:
$products_cat = GPos_Model_Product::findByCategories(array($category));
在本例中,$category是一个单独的实体,这就是为什么我在将其提供给函数之前将其放入数组中
以下是您查找不在给定类别或类别列表中的产品的方法:
/**
*
* Takes an array of GPos_Model_Category entities as parameter and returns all products not in these categories
* @param array $categories
*/
public static function findByNotCategories($categories) {
$categoryArray = array();
foreach ($categories as $category) {
array_push($categoryArray, $category->getId());
}
$qb = Zend_Registry::get('entityManager')->createQueryBuilder();
$qb2 = Zend_Registry::get('entityManager')->createQueryBuilder();
$qb->select('p')
->from('GPos_Model_Product', 'p')
->where($qb->expr()->notIn('p.id',
$qb2->select('p2.id')
->from('GPos_Model_Product', 'p2')
->leftJoin('p2.categories', 'c')
->andWhere($qb->expr()->in('c.id', $categoryArray))
->getDQL()
));
return $qb->getQuery()->execute();
}
这实际上是使用一个子选择。我选择给定类别(即子选择)中的所有产品id,然后选择不在子选择结果中的所有产品。我在这里的工作完成了
/**
*
* Takes an array of GPos_Model_Category entities as parameter and returns all products not in these categories
* @param array $categories
*/
public static function findByNotCategories($categories) {
$categoryArray = array();
foreach ($categories as $category) {
array_push($categoryArray, $category->getId());
}
$qb = Zend_Registry::get('entityManager')->createQueryBuilder();
$qb2 = Zend_Registry::get('entityManager')->createQueryBuilder();
$qb->select('p')
->from('GPos_Model_Product', 'p')
->where($qb->expr()->notIn('p.id',
$qb2->select('p2.id')
->from('GPos_Model_Product', 'p2')
->leftJoin('p2.categories', 'c')
->andWhere($qb->expr()->in('c.id', $categoryArray))
->getDQL()
));
return $qb->getQuery()->execute();
}