How I can Iterate into SUM result from doctrine query - symfony

I need some help about iterate over result in doctrine. This is my code for query:
$consulta = $em->createQuery('
SELECT bcr, SUM(bcr.volumenAlmacenado) as vol, SUM(bcr.pesoAlmacenado) as pes
FROM ResiduoBundle:BodegaContieneResiduo bcr
WHERE bcr.fechaIngreso BETWEEN :fechaP AND :fechaA AND bcr.fechaRetiro IS NULL
GROUP BY bcr.idResiduo
');
$consulta->setParameter('fechaA', $fechaActual);
$consulta->setParameter('fechaP', $fechaPasada);
return $consulta->getResult();
When I run in mysql return without problem. in symfony also get results. Now, when I tried to loop in twig I can't do, I think could be for my agregate functions in my query. I hope you can get me a clue or something about this. Grettings

You should added extension of mysql function SUM then you can use SUM function in doctrine query
another way you can direct execute your query or native query of doctrine
Try following query you can get result
$consulta = $em->executeQuery('SELECT bcr, SUM(bcr.volumenAlmacenado) as vol, SUM(bcr.pesoAlmacenado) as pes FROM ResiduoBundle:BodegaContieneResiduo bcr WHERE bcr.fechaIngreso BETWEEN :fechaP AND :fechaA AND bcr.fechaRetiro IS NULL GROUP BY bcr.idResiduo'),
array('fechaA'=> $fechaActual, 'fechaP' => $fechaPasada));
return $consulta->fetchAll();

You can also use native query of doctrine
use Doctrine\ORM\Query\ResultSetMapping;
$rsm = new ResultSetMapping();
$rsm->addEntityResult('ResiduoBundle:BodegaContieneResiduo', 'bcr');
$rsm->addFieldResult('bcr', 'idResiduo', 'idResiduo');
$rsm->addFieldResult('bcr', 'volumenAlmacenado', 'vol');
$rsm->addFieldResult('bcr', 'pesoAlmacenado', 'pes');
$sqlQuery = "SELECT bcr.idResiduo as idResiduo, SUM(bcr.volumenAlmacenado) as vol, SUM(bcr.pesoAlmacenado) as pes
FROM ResiduoBundle:BodegaContieneResiduo bcr
WHERE bcr.fechaIngreso BETWEEN :fechaP AND :fechaA
AND bcr.fechaRetiro IS NULL
GROUP BY bcr.idResiduo";
$query = $em->createNativeQuery($sqlQuery, $rsm)
->setParameter('fechaA', $fechaActual);
->setParameter('fechaP', $fechaPasada);
$query->getResult();

Related

Doctrine Native query inside query builder join

I am working on a query on Doctrine 2 (with Symfony 2.8)
I have this query giving me relevant info:
$qb = $this->createQueryBuilder('ea');
$qb->join('ea.entity_b', 'eb')
->join('ea.entity_c', 'ec')
->join('ec.entity_d', 'ed')
->join('MainBundle:Entity_E', 'ee','WITH', 'ee.column1 = ea.id')
->join('MainBundle:Entity_F', 'ef', 'WITH', 'ea.column1 = ef.id');
Now, I need to add extra info to that query, but it comes from a native SQL, something like this:
SELECT * FROM DS ORDER BY id DESC LIMIT 1
And make sure that the id of the result from the native query is equal to ef.id
I hope I made any sense.
Thanks
Do you know DQL ?
Similar to SQL but adapted for Doctrine.
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html
You can then do queries like :
<?php
$query = $em->createQuery("SELECT u FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();

How to use a query builder object inside an IN clause of another query builder object

I need to create a query builder expression that use an IN statement where I want to put another query create by querybuilder too. In DQL, the query that i can get is:
SELECT u
FROM BundleUsuarioBundle:Usuario u
WHERE u.id IN (
SELECT u2.id
FROM BundleTramiteBundle:ExamenTeorico et
JOIN et.usuarioHabilitacion u2
WHERE u2.centro = :centro
) ORDER BY u.apellido ASC, u.nombre ASC
but i dont use that because I need put that in 'query_builder' option of an entity field of a form. So, i need a QB but I dont know how I can nest querybuilder expressions. I actually have the two queries in QueryBuilder format but separated:
$qb = $this->em->createQueryBuilder();
$qb
->select('usuario_habilitacion.id')
->from('BundleTramiteBundle:ExamenTeorico', 'examen_teorico')
->join('examen_teorico.usuarioHabilitacion', 'usuario_habilitacion');
$user = $this->getUser();
if ( !$this->esTipoASPV($user) ) {
$centro = $this->getCentro();
$qb
->andWhere($qb->expr()->eq('usuario_habilitacion.centro', ':centro'))
->setParameter(':centro', $centro->getId());
}
and:
$er
->createQueryBuilder('u')
->where($qb->expr()->in('u.id', ___???___ ))
->addOrderBy("u.apellido", "ASC")
->addOrderBy("u.nombre", "ASC");
You can get the result of your sub-query inside the first query but calling getDQL() and placing it inside a where clause in your first query. Make sure you select the id in the sub-query.
Example:
$qb->andWhere($qb->expr()->in('u.id', $er->getDQL()))

Doctrine query building select MAX

I would like to select everything + MAX value and receive only rows having max values.
$query = $this->createQueryBuilder('s');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.score');
$query->getQuery();
return $query->select('s.*, MAX(s.score) AS max_score')->getQuery()->getResult();
How could I achieve this in doctrine? I am getting an error that * property is not found. I have tried to select them all one by one but no luck either.
Goal is to achieve something like this
SELECT user, challenge, whateverelse, MAX(score) FROM users_scores_table GROUP BY user_id
Please help ;)
It's too late, but I write this for the records.
You can use "as HIDDEN" in SELECT statements to remove a field of the final result, this way you can use it for ordering or grouping without modifying result fields.
In your example:
$query = $this->createQueryBuilder('s');
$query->select('s, MAX(s.score) AS HIDDEN max_score');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.user');
$query->setMaxResults($limit);
$query->orderBy('max_score', 'DESC');
Here is a final working query
$query = $this->createQueryBuilder('s');
$query->select('s, MAX(s.score) AS max_score');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->groupBy('s.user');
$query->setMaxResults($limit);
$query->orderBy('max_score', 'DESC');
return $query->getQuery()->getResult();

symfony2 doctrine join

Okay, so i've got a query that i've researched and researched how to get this to work and for the life of me i cant!... perhaps i'm just doing this incorrectly and the minimal information ive found..
I've got a table named timeclock setup.. which has a field: noteBy_id in it which is an id to the user the record belongs to...
What I need to do now, is for the management side of things in the system.. I anticipate more than 1 company using this timeclock system, and as such I need to filter the results based on the company id.. In the user table, i have a field named parentcompany_id
So, lets see if I can express in words what I need to do..
I need to Select * from timeclock and left join user.parentcompany_id where timeclock.daydate < :start and where u.parentcompany = :pid
where start is: `date('Y-m-d 00:00:00');
I've setup this query:
$em = $this->getDoctrine()->getEntityManager();
$start = date('Y-m-d 00:00:00');
$qb = $em->getRepository('EcsCrmBundle:TimeClock');
$qb = $qb->createQueryBuilder('t');
$query = $qb->select('t, u.parentcompany_id')
->from('timeclock', 't')
->leftJoin('Ecs\AgentManagerBundle\Entity\User', 'u', 'ON' 'u.id = t.noteBy_id AND u.parentcompany_id = :pid')
->where('t.daydate < :start')
->andWhere("t.noteBy_id != ''")
->setParameter('start', $start)
->setParameter('pid', $user->getParentcompany())
->getQuery();
$entities = $query->getArrayResult();
I've looked and looked and can't find a solution to the error that I get which is:
An exception has been thrown during the rendering of a template ("[Semantical Error] line 0, col 112 near 'u ON u.id = t.noteBy_id': Error: Identification Variable Ecs\AgentManagerBundle\Entity\User used in join path expression but was not defined before.") in EcsCrmBundle:TimeClock:manager.html.twig at line 5.
and the query that gets output is:
SELECT t, u.parentcompany_id FROM Ecs\CrmBundle\Entity\TimeClock t LEFT JOIN Ecs\AgentManagerBundle\Entity\User u ON u.id = t.noteBy_id AND u.parentcompany_id = :pid, timeclock t LEFT JOIN Ecs\AgentManagerBundle\Entity\User u ON u.id = t.noteBy_id AND u.parentcompany_id = :pid WHERE t.daydate < :start AND t.noteBy_id != ''
which under normal circumstances would work perfectly... but in this, it just doesn't... Any ideas?
I've recently had to do it like this.. I'm guessing in this that your noteBy is a ManyToOne in the user table and you are wanting to have it filter the results by the company of the admin that is currently logged into your system..
So, adapting a join I had to write myself for such a task is easy enough. I personally like to use the QueryBuilder so this will be done in query builder..
Your first mistake in your query is the ->from('timeclock', 't') line. Because you have previously created your object with $qb = $em->getRepository('EcsCrmBundle:TimeClock'); $qb = $qb->createQueryBuilder('t'); you don't need the from in the query builder, as it will be generated for you.
The next issue, is the leftJoin and I'll explain why when I've shown you a working version.
And the last issue, preventing this from working how you want it - is a missing andWhere clause. So, lets take a look at a working query.
$query = $qb->select('t, u')
->leftJoin('t.noteBy', 'u', 'WITH', 'u.id = t.noteBy')
->where('t.daydate < :start')
->andWhere('u.parentcompany = :pid')
->setParameter('start', $start)
->setParameter('pid', $user->getParentcompany())
->getQuery();
So because we've already created the object by using $qb = $qb->createQueryBuilder('t') we just select t and u
For the join, we're joining the timeclock table by the noteBy column, which is the user id from the user table. So, the first argument being the "from" alias. So, since we've aliased the timeclock table with t we use t.noteBy. The next argument in the leftjoin is the alias of the 2nd table, which is u in this case but can be anything.. The third argument for a leftJoin anyway - is the way you join it.. either a WITH or ON will work here. and the 4th argument, is the match you wish it to have.. in this case u.id must equal t.noteBy
You will see that I got rid of one of the andWhere, I did this because with the properly structured query you shouldn't need it. I did however add in the andWhere for the u.parentcompany since that is afterall what you are looking to filter by you should have it in a WHERE instead of as a match in the join itself.
The documentation is very limited in this, and it took me a while to figure it all out as well.. You, undoubtedly - like me, came to using doctrine from writing your queries by hand. And So since you seem to be just starting with Symfony (i am myself as well about 2 months in now), you're still in the hand-coding mindset. But with further time, you'll start understanding the DQL way of life. Try this query out and see what happens.
Ok, first you would need to relate entity Timeclock to Company. Whenever you want to join two entities in Doctrine they need to be related by some attribute (that is, table column).
I don't see any need for User entity in this query as all info is available through Company entity and you are not filtering down results based on any user properties.
You desired query should look something like this (more or less). I took liberty and ditched _id suffixes from entity attributes as they tend to cloud what is really going on. ;)
$query = $this->getEntityManager()->createQuery("SELECT t, c.id FROM EcsCrmBundle:TimeClock t JOIN t.company c WHERE c.id = :pid AND t.daydate < :start AND t.noteBy != ''");
$query->setParameter('start', $start);
$query->setParameter('pid', $user->getParentcompany());
return $query->getArrayResult();
Also, I did inner-join (JOIN) as I think there could not be timeclock without it's company but feel free to change that to LEFT JOIN if that suits you better.
Is this what you were trying to achieve?

Magento: Filtering a Collection with grouped Clauses

I would like to filter a collection with grouped clauses. In SQL this would look something like:
SELECT * FROM `my_table` WHERE col1='x' AND (col2='y' OR col3='z')
How can I "translate" this to filtering a collection with ->addFieldToFilter(...)?
Thanks!
If your collection is an EAV type then this works well:
$collection = Mage::getResourceModel('yourmodule/model_collection')
->addAttributeToFilter('col1', 'x')
->addAttributeToFilter(array(
array('attribute'=>'col2', 'eq'=>'y'),
array('attribute'=>'col3', 'eq'=>'z'),
));
However if you're stuck with a flat table I don't think addFieldToFilter works in quite the same way. One alternative is to use the select object directly.
$collection = Mage::getResourceModel('yourmodule/model_collection')
->addFieldToFilter('col1', 'x');
$collection->getSelect()
->where('col2 = ?', 'y')
->orWhere('col3 = ?', 'z');
But the failing of this is the order of operators. You willl get a query like SELECT * FROM my_table WHERE (col1='x') AND (col2='y') OR (col3='z'). The OR doesn't take precedence here, to get around it means being more specific...
$collection = Mage::getResourceModel('yourmodule/model_collection')
->addFieldToFilter('col1', 'x');
$select = $collection->getSelect();
$adapter = $select->getAdapter();
$select->where(sprintf('(col2 = %s) OR (col3 = %s)', $adapter->quote('x'), $adapter->quote('y')));
It is unsafe to pass values unquoted, here the adapter is being used to safely quote them.
Finally, if col2 and col3 are actually the same, if you're OR-ing for values within a single column, then you can use this shorthand:
$collection = Mage::getResourceModel('yourmodule/model_collection')
->addFieldToFilter('col1', 'x')
->addFieldToFilter('col2', 'in'=>array('y', 'z'));

Resources