Skip to content

Instantly share code, notes, and snippets.

@kaiohken1982
Created September 14, 2013 21:13
Show Gist options
  • Select an option

  • Save kaiohken1982/6565694 to your computer and use it in GitHub Desktop.

Select an option

Save kaiohken1982/6565694 to your computer and use it in GitHub Desktop.
Doctrine 2 "category" TermTaxonomy ordered resultset example -- not best practice
$qb = $this->_em->createQueryBuilder();
$qb->select(array(
'a.termTaxonomyId',
'b.name',
'a.parentId',
'COALESCE(a.parentId, a.termTaxonomyId) AS HIDDEN fld')
);
$qb->from('\My\Entity\TermTaxonomy', 'a');
$qb->leftJoin('\My\Entity\Term', 'b', 'WITH', 'b.termId = a.termId');
$qb->andWhere($qb->expr()->eq('a.taxonomy', ':taxonomy'));
$qb->addOrderBy('fld', 'ASC');
$qb->addOrderBy('a.parent', 'ASC');
$qb->addOrderBy('b.name', 'ASC');
$sql = $qb->getQuery()->getSQL();
$stmt = $this->_em->getConnection()->prepare($sql);
$stmt->bindValue(1, 'category');
$stmt->execute();
@wdalmut

wdalmut commented Sep 15, 2013

Copy link
Copy Markdown
<?php
$qb = $this->_em->createQueryBuilder();
        $qb->select(array(
                'b.id', 
            'b.name', 
            'a.parent', // Carica entità nel proxy 
            'COALESCE(c.id, b.id) AS HIDDEN fld') //custom
        );
        $qb->from('\My\Entity\TermTaxonomy', 'a');
        $qb->leftJoin('a.terms', 'b');  //Ho cambiato termId in terms
        $qb->leftJoin('a.parent', 'c'); //Join per caricare gli altri
        $qb->andWhere($qb->expr()->eq('a.taxonomy', ':taxonomy'));
        $qb->addOrderBy('fld', 'ASC');
        $qb->addOrderBy('a.parent', 'ASC');
        $qb->addOrderBy('b.name', 'ASC');

        $qb->setParameter("taxonomy", "category");
        $entities = $qb->getQuery()->getResult();

termId è un campo ManyToOne o ManyToMany Giusto?

Se ti rompe per la coalesce puoi mappare la custom function non è difficile.

Sono effettivamente molto brutti i campi di relazione come termId sarebbe molto più bello avere il campo terms così da avere una getTerms() che risponde una Collection invece di getTermid().

Ciao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment