【发布时间】:2012-05-18 12:51:32
【问题描述】:
我有两个具有单向多对一映射的实体。
这里是Product:
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 是 GPos_Model_Category 实体的 ArrayCollection。
现在呢? 好吧,现在我想检索给定类别中的所有产品以及给定类别中不的所有产品。
我试过$products = GPos_Model_Product::findByCategories($category->getId());
但这只给了我SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 1 和 $category 的 ID 是 1,所以我想这不是要走的路。有人知道如何处理吗?
谢谢!
【问题讨论】:
标签: doctrine-orm many-to-one arraycollection findby