I have the following database scheme:
table 'products'
id
category_id
and of course a category table, just with an id.
The data look something like that:
Products
--------------------
| id | category_id |
--------------------
| 0 | 1 |
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 1 |
--------------------
I would like to select a category(for example Category 1), so I select all the rows from that category in my product-repository class:
return $this
->createQueryBuilder('u')
->andWhere('u.category = :category')
->setMaxResults(1)
->setParameter('category', $category->getId())
->getQuery()
->getSingleResult()
;
How I can select now a random product? Also: Is it possible to solve this via Relationships?
I have a OneToMany Relationship between the entities "Category" and "Product", so I could also get all the products via category->getProducts()...
Any help would be really useful, thanks