Join subquery with doctrine and Symfony 2 - php

I saw the begining of an answer in a post about Zend : Join subquery with doctrine 2 DBAL
Unfortunately I can't manage it to work. I tried aimfeld soltuion like that:
$qbaudio = $em->createQueryBuilder();
$subSelect = $qbaudio->select ('a.id_support id_support','sum(a.duration) dureeTotale','count(a) nbAudio')
->from('MyBundle:AudioObject','a')
->groupBy('a.id_support')
->where('a.type = :audio_type')
->getQuery();
$qb = $em->createQueryBuilder();
$qb->select('sp.ref1','sp.title1','count(i) nbImage','sp.nbSupportSaisi','sum(a.duration) dureeTotale','count(a) nbAudio','a.sampling')
->from('MyBundle:Storage', 'st')
->leftJoin('p.sides','si')
->leftJoin('si.support','sp')
->leftJoin('sp.images','i')
->leftJoin('sp.audioObjects', sprintf('(%s)',$subSelect->getDQL()), 'a', 'ON sp.id = a.id_support')
->groupBy('sp.id')
->setParameter('audio_type', 'MP3')
Unfortunately I got this message :
Error: Expected end of string, got 'SELECT'
If it's possible with ZEnd, Why not with Symfony?
Any idea?
Thanks

Use SQL instead of DQL:
$subSelect->getSQL()

Related

Doctrine2 QueryBuilder Join

So, I'm new to doctrine, and I'm trying to do a basic joint, but I guess I'm missing something, on my entities or I don't know for sure.
Doctrine Repository:
$queryBuilder = $this->createQueryBuilder()
->select('c.*, a.*')
->from('My\Entity\CompanyAdminNotes', 'c')
->innerJoin('Administrators','a','a.id = c.admin_id')
->where('c.admin_id = :admin_id')
->setParameter('admin_id', $id);
return $queryBuilder->getQuery()->getResult();
And I get the following error
Message: [Semantical Error] line 0, col 76 near 'a,
My\Entity\CompanyAdminNotes': Error: Identification Variable
Administrators used in join path expression but was not defined
before.
I'm not sure if my query it's wrong or something else isn't set. Can you guys give me a hint?
try to change this:
->innerJoin('Administrators','a','a.id = c.admin_id')
to this:
->innerJoin('My\Entity\Administrators','a','a.id = c.admin_id')
Because It need the path as you done into the from
UPDATE
Trying another solution like this:
$queryBuilder = $this->createQueryBuilder('c')
->select('c, a')
->from('My\Entity\CompanyAdminNotes', 'c')
->innerJoin('My:Administrators','a','a.id = c.admin_id')
->where('c.admin_id = :admin_id')
->setParameter('admin_id', $id);

Parameters not being replaced in Doctrine

I am unable to figure what is the problem here.
I have created the below query using doctrine Query Builder
$repo = $this->em->getRepository(self::STORE_TIMING);
$qb = $repo->createQueryBuilder('store_timings');
$qb->select('st')
->where('st.id = :identifier')
->setParameter('identifier', 100);
When i print the DQL :
print_r($qb->getDQL());die();
The query that is output is :
SELECT st FROM Test\BotBundle\Entity\StoreTimings store_timings WHERE st.id = :identifier
To my surprise the :identifier is not being replaced.
Request for some guidelines here.
If you are using getDQL It doesn't return the query with parameter.
I advise you to use the _profiler to view the complete query with parameter

Disable Lazy-Loading in doctrine 2 and ZF2

actually i'm totaly new in doctrine 2 , i'm using it in a project by zendframework 2 , what i'm looking for is to disable the lazy-loading in doctrine 2 , i've this way for the custom queries :
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('ed')
->from('Application\Entity\Object', 'ob')
->innerJoin('ob.fkLogin', 'lg')
->where("lg.state= 'valid'");
$query = $qb->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_FORCE_PARTIAL_LOAD, true);
$objects= $query->getResult();
it's by using the setHint for the query , i'm asking how can i do the same for : fecthAll and findBy , find , for example for this query :
$object = $this->getEntityManager()->getRepository('\Application\Entity\Object')->find($id);
Thanks.

Symfony : Pagination with inner join

I need to implement a pagination. Seemingly, Doctrine doesn't support some jointures.
Here is my query :
$query = $this->getEntityManager()
->createQueryBuilder();
$query->setFirstResult(($page - 1) * $maxperpage);
$query->setMaxResults($maxperpage);
$query->select('d')
->from('DemandeBundle:Declaration', 'd')
->orderBy('d.id', 'ASC')
->innerJoin('ContactBundle:Contact', 'c', 'WITH', 'd.contact = c')
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->getQuery()
->getResult();
return new Paginator($query, true);
It's working fine when I am not using innerJoin but I need to use it so as to display only requests regarding to my user.
Using innerJoin I got that kind of error :
"An exception has been thrown during the rendering of a template
("Cannot count query which selects two FROM components, cannot make distinction") in
DemandeBundle:Demande:listing_demande.html.twig at line 25"
How can I circumvent this problem without using another bundle or whatever.
Hope you will understand me guy.
Is your Declaration somehow related to Contact?
It's far better for you to have ManyToOne relation in Contact that points to Declaration. That way, it will work since you won't have two FROM components, but single one instead.
Then, modify the query to do:
->innerJoin('d.contant', 'c')
The full query should look like this:
$query->select('d')
->from('DemandeBundle:Declaration', 'd')
->orderBy('d.id', 'ASC')
->innerJoin('d.contact', 'c') // <-- THIS LINE IS CRITICAL
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->getQuery()
->getResult();
Finally, I found out a solution :
Instead of :
$query->select('d')
->from('DemandeBundle:Declaration', 'd')
->orderBy('d.id', 'ASC')
->innerJoin('ContactBundle:Contact', 'c', 'WITH', 'd.contact = c')
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->getQuery()
->getResult();
I used :
$query->select('d')
->add('from', 'SgaDemandeBundle:Declaration d INNER JOIN d.contact c')
->where('c.structure_id = :structure_id')
->setParameter('structure_id', $structureId)
->orderBy('d.id', 'ASC')
->getQuery();

Doctrine 2 join troubles

I try to do this query using doctrine query builder
$idAccount = $params['idAccount'];
$qb = $this->_em->createQueryBuilder()->select('t,tt')
->from($this->_entityName, 'sr')
->innerJoin('sr.account', 'a')
->innerJoin('sr.product', 'p')
->leftJoin('p.title', 't')
->leftJoin('p.set', 's')
->leftJoin('s.idTitle', 'tt');
$qb->where($qb->expr()->eq('a.idAccount',$idAccount));
end have this error:
E_WARNING
class_parents() [function.class-parents]: object or string expected
D:\Doctrine\ORM\Mapping\ClassMetadataFactory.php : 224
but when I fetch select('sr,a,p,s,t,tt') with this criteria all works fine. and when i use HYDRATE_ARRAY hydration all works fine too. But I need only t and tt fields, is there any way to do this??
For object hydration you have to fetch the element set in FROM clause currently. There might even be an open bug/enhancement report for this.

Categories