Doctrine – select cez ManyToMany väzbu
- MajklNajt
- Člen | 471
Zdravím,
v Nette začínam s Doctrine (Kdyby), namapoval som si entity a teraz sa hrám s väzbami, pričom som narazil na problém pri vyťahovaní entít pomocou väzby ManyToMany.
Mám entitu Product a Section a medzi nimi obojsmernú väzbu:
<?php
/**
* @Entity
* @Table(name="catalog_sections")
*/
class Section
{
/**
* @ManyToMany(targetEntity="Product", mappedBy="sections", fetch="LAZY")
* @var ArrayCollection
**/
protected $products;
}
/**
* @Entity
* @Table(name="catalog_products")
*/
class Product extends Shoppable
{
/**
* @ManyToMany(targetEntity="Section", inversedBy="products", fetch="LAZY")
* @JoinTable(name="catalog_products_sections")
* @var ArrayCollection
**/
protected $sections;
}
?>
Shoppable je abstraktná trieda, mám viac druhov entít, ktoré ju dedia, myslím, že v tomto prípade nie je podstatná…
K entitám mám fasádu, kde sa snažím vytiahnuť iba Product-y, ktoré majú väzbu na vybrané Section-y. Keď na to napíšem priamo DQL dotaz, funguje to podľa mojich predstáv
<?php
public function findProductsBySections(array $sections_ids = [])
{
$sections = [];
foreach($sections_ids as $section_id) {
if(is_numeric($section_id)) {
$section = $this->sectionRepository->find($section_id);
if($section instanceof Section) {
$sections[] = $section;
}
}
}
$query = $this->em->createQueryBuilder()
->select(["p"])
->from(Product::class, "p")
->leftJoin("p.sections", "s")
->where("s.id IN (:xxx)")
->setParameter("xxx", $sections)
->getQuery();
$this->resultset = new ResultSet($query);
return $this->resultset->toArray();
}
?>
Ale nejde mi to, ak sa to snažím vytiahnuť z Repository pomocou Criteria:
<?php
public function findProductsBySections(array $sections_ids = [])
{
$sections = [];
foreach($sections_ids as $section_id) {
if(is_numeric($section_id)) {
$section = $this->sectionRepository->find($section_id);
if($section instanceof Section) {
$sections[] = $section;
}
}
}
$criteria = Criteria::create();
$criteria->andWhere(Criteria::expr()->in("sections", $sections));
return $this->productRepository->matching($criteria);
}
?>
Dostávam z toho chybu:
An exception occurred while executing
'SELECT t1.id AS id_2, t1.price_amount AS price_amount_3, t1.price_currency AS price_currency_4, t0.code AS code_5, t0.ean AS ean_6, t0.name AS name_7, t0.title AS title_8, t0.description AS description_9, t0.keywords AS keywords_10, t0.content AS content_11, t0.count_views AS count_views_12, t0.count_buys AS count_buys_13, t0.created_at AS created_at_14, t0.modified_at AS modified_at_15, t0.published_at AS published_at_16, t0.hidden AS hidden_17, t0.deleted AS deleted_18, t0.flag_new AS flag_new_19, t0.flag_tip AS flag_tip_20, t0.flag_action AS flag_action_21, t0.flag_sale AS flag_sale_22, t1.entity
FROM catalog_products t0
INNER JOIN ecommerce_shoppable t1 ON t0.id = t1.id
WHERE catalog_products_sections.section_id IN (?)' with params [12]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_products_sections.section_id' in 'where clause'
Robím niečo zle, alebo to Doktrína proste nedá a musím kvôli tomu znásilňovať DQL??
Ďakujem za nakopnutie…
Editoval MajklNajt (6. 2. 2018 14:14)