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.
Related
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
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()
I have two entities, Carros and Msg, i am looking to get Carros that have Msg messages
$query = $entityManager->createQuery("
SELECT u
FROM Auto\Entity\Carros u
JOIN Auto\Entity\Msg m WITH m.idautoad=u.idcarros
WHERE u.identidade='".$emailidentidade."'
ORDER BY u.datadoanuncio DESC
");
I'm using the paginator:
// Create the paginator itself
$paginator = new Paginator(
new DoctrinePaginator(new ORMPaginator($query))
);
and i am getting the following errors i have zend 2.3.9 and doctrine 2.4
Arquivo:
C:\websites\auto\vendor\zendframework\zendframework\library\Zend\Paginator\Paginator.php:637
Mensagem: Error producing an iterator
C:\websites\auto\vendor\doctrine\orm\lib\Doctrine\ORM\Tools\Pagination\WhereInWalker.php:85
Mensagem:
Cannot count query which selects two FROM components, cannot make distinction
its generating the error when i try to do this :
foreach ($paginator as $carro)
{}
The error disappears when getting the results like this :
$fi = $query->getResult();
and then
$paginator = new \Zend\Paginator\Paginator(new
\Zend\Paginator\Adapter\ArrayAdapter($fi)
);
In case you have a ManyToOne relationship you can do like this (be sure to see this link before to make your mapping correct : Bidirectionnal ManyToOne relation
public function getInvoices($idProformaGroup, $paginate = false)
{
$query = $this->getEntityManager()->createQueryBuilder();
$query->select('po', 'i')
->from('Invoice\Entity\Proforma', 'po')
->innerJoin('po.idInvoice', 'i')
->andWhere("po.idProformaGroup = :idProformaGroup")
->orderBy('po.idProforma', 'ASC')
->setParameter('idProformaGroup', $idProformaGroup);
if ($paginate) {
$doctrineAdapter = new DoctrineAdapter(new ORMPaginator($query, true));
return new Paginator($doctrineAdapter);
} else {
return $query->getQuery()->getResult();
}
}
Like you see, my join use the foreign key po.idInvoice to join the two tables. And because of this my Paginator don't show me your error.
EDIT : With a param I can decide to paginate or not. Don't use it if you don't need it.
EDIT 2 : From the link to the other question join before Doctrine : Pagination with left Joins The futurereal's answer is the same point I tried to explain to you.
i am not using anymore
new DoctrinePaginator(new ORMPaginator($query))
now i am using \Zend\Paginator\Paginator and its working
$fi = $query->getResult();
$paginator = new \Zend\Paginator\Paginator(new
\Zend\Paginator\Adapter\ArrayAdapter($fi)
);
Hello everyone I'm trying to use phalcon without ORM. I want to separate business logic and data access with DataMapper pattern and I can't find how I can build queries without raw SQL, but using original phalcon queryBuilder.
Is there any chance to do it?
Is this what you are looking for?
<?php
//Getting a whole set
$robots = $this->modelsManager->createBuilder()
->from('Robots')
->join('RobotsParts')
->orderBy('Robots.name')
->getQuery()
->execute();
//Getting the first row
$robots = $this->modelsManager->createBuilder()
->from('Robots')
->join('RobotsParts')
->orderBy('Robots.name')
->getQuery()
->getSingleResult();
From Phalcon docs: Phalcon Query Language (PHQL)
I figured out how to do it
I'm using phalcon with doctrine DBAL
it's working great for me
I found 2 ways solve it if i'm not mistaken.
Solution 1#
use Phalcon\Mvc\Model\Query\Builder;
$result = $this->modelsManager->createBuilder()
->columns("table1.f1, table1.f2,table2.f3,table2.f4")
->From('table1')
->innerjoin('table2', 'table1.matchfield = table2.matchfield')
->where("table1.fieldname = '$value' ")
->getQuery()
->execute();
Solution 2#
$res = $this->db->execute("UPDATE Tablename SET field1= ?,field2=?,field3=? WHERE id = ?",array($field1,$field2,$field3,$id));
So , i'm working on a news portal . I'm using KNP Paginator,and i've got an error :
Cannot count query which selects two FROM components, cannot make distinction
And now , my source code :
#block of code from controller
$rep = $this->getDoctrine()->getRepository('NickCoreBundle:News');
$user = $this->get('security.context')->getToken()->getUser();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$rep->getNewsFeedQuery($user->getId()),
$page,
10
);
and from my repository:
public function getNewsFeedQuery($userid)
{
$userid = intval($userid);
return $this->getEntityManager()->createQueryBuilder()
->select('n')
->from('NickCoreBundle:News', 'n')
->innerJoin('NickCoreBundle:Subscriptions', 's', 'WITH', 's.user = :userid AND n.source = s.source')
->orderBy("n.date","DESC")
->setParameter('userid', $userid)->getQuery();
}
how to solve it :) ? tried to get result from query , in ArrayCollection , it works (partialy,not sorted by date , and i think this method used a lot of time)
It's a little late to answer but it could be of help to other readers.
I would suggest doing away with the explicit innerJoin and let Doctrine handle the joins.
return $this->getEntityManager()->createQueryBuilder()
->select('n')
->from('NickCoreBundle:News', 'n')
->join('n.source', 's')
->where('s.user'= :userid )
->orderBy("n.date","DESC")
->setParameter('userid', $userid)->getQuery();