我有以下数据库方案:
table 'products'
id
category_id
当然还有一个类别表,只有一个 id。
数据看起来像这样:
Products
--------------------
| id | category_id |
--------------------
| 0 | 1 |
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 1 |
--------------------
我想选择一个类别(例如类别 1),因此我在我的产品存储库类中选择该类别中的所有行:
return $this
->createQueryBuilder('u')
->andWhere('u.category = :category')
->setMaxResults(1)
->setParameter('category', $category->getId())
->getQuery()
->getSingleResult()
;
我现在如何选择随机产品?另外:是否可以通过关系解决这个问题?
我在实体“Category”和“Product”之间有一个 OneToMany 关系,所以我也可以通过 category->getProducts()...
任何帮助都会非常有用,谢谢