Parameters not being replaced in Doctrine - php

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

Related

Symfony2 query builder does not return inner join data

I using Symfony2 and Doctrine and I have this code in my repository.
$cb = $this->getEntityManager()
->createQueryBuilder("p")
->select($arrayColumns) //This array contains the fields
->from('MainBundle\Entity\Pedido', 'p')
->innerJoin('p.sucursal', 's');
The $arrayColumns contains this: ['p.fecha', 's.descripcion', 'p.descripcion', 'p.id'].
When this query is executed, the result contains: ['p.fecha', 'p.descripcion', 'p.id']. It is omitting the 'sucursal' field.
The relation between Sucursal and Pedido is OneToMany.
Maybe you should build your query in pure SQL and use RSM to map entities.
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/native-sql.html#resultsetmappingbuilder
You probably need to use:
$arrayColumns[] = "IDENTITY(p.sucursal)";
before the query
as described here: https://github.com/doctrine/doctrine2/issues/1955 and here: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html

How to use phalcon query builder without ORM

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));

How convert SQL query to Doctrine2?

I want to convert this in Doctrine2.
Similar to these questions:
Symfony2 / Doctrine2 - How to convert this SQL request with QueryBuilder?
How to convert a complex MySQL Query to Doctrine2
Convert SQL to Doctrine 2 Query Builder or DQL by using NOT IN?
Is it possible?
SELECT
SUM(q.amount)
FROM(
SELECT amount
FROM expense_report e0_
WHERE
(e0_.is_account_transfer = 0)
AND (e0_.remove_date IS NULL)
LIMIT 0, 2) q
Thanks
you can archive your problem with this custom repository method:
public function getAmount()
{
$qb = $this->createQueryBuilder("q");
return
$qb
->select("sum(q.amount)")
->where($qb->expr()->isNull('q.remove_date'))
->andWhere('q.is_account_transfer = 0')
->setFirstResult(0)
->setMaxResults(2)
->getQuery()
->getSingleScalarResult();
}
And simply use it, as example, in a controller method as:
public function testAction()
{
....
$totalAmount = $this->getDoctrine()
->getRepository("AcmeDemoBundle:ExpenseReport")
->getAmount();
...
}
Hope this help

How to print single value of entity without dumping the whole object?

I want to get one single value from entity.Can anyone help me here.
Here is my code.Please let me know what is missing here.
$query = $em->createQuery("SELECT e FROM AdminBundle:MailTemplates e WHERE e.keyword = '" .$keywordVal."'");
$query->execute();
$result = $query->getResult();
echo $result ->getId();
Here i want the 'id'.
This is noted in the documentation how you can do this.
So given you're code this will become:
$query = $em->createQuery("SELECT e.id FROM AdminBundle:MailTemplates e WHERE e.keyword = ?1");
$query->setParameter(1, $keywordVal);
$query->execute();
$result = $query->getResult(); // array of MailTemplates ids
Note: I also made use of setParameters instead of setting the value directly in the query.
In your controller:
$this->get('database_connection')->fetchColumn('select id from mail_templates where...');
That's much better for performance and much easier if you don't want to have a deal with query builder and other doctrine orm stuff.
Using the query builder you could do...
$queryBuilder = $em->createQueryBuilder('e');
$queryBuilder
->select('e.yourColumn')
// This will return just this column
// Alternatively you could omit any select to return the whole object
// that you could then use like $object->getYourColumn() if you so chose
->where($queryBuilder->expr()->eq('e.keyword', ':keyword'))
->setParameter('keyword', $keyword)
;
return $queryBuilder
->getQuery()
->getResult();
try this on loading Entities instead of creating own queries
Loading the entity with the Repository.
$rep = $this->getDoctrine()->getManager()->getRepository("Bundlename:Entity");
//find one by keyword -> single entity
$entity = $rep->findOneBy(array('keyword' => $keyword));
//find all by keyword - Array of entities
$result = $rep->findBy(array('keyword' => $keyword));

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