Sent leftjoin querybuilder rsult to twig - php

i have a leftjoin with doctrine querybuilder
$registros = $this->em->createQueryBuilder();
$registros->select('u,v')
->from('Entity\RegistroCam', 'u')
->leftjoin('Entity\CmMunicipios','v','WITH','u.camMunicipio = v.idMunicipio')
->orderBy('u.camPaterno', 'DESC');
$query = $registros->getQuery();
// Execute Query
$result = $query->getResult();
My problem is when I send the result to twig , it throws the following error
Twig_Error_Runtime: Method "idRegistro" for object "Entity\CmMunicipios" does not exist in "adminPanel.twig.html" at line 87
if i write
$registros->select('u')
works perfectly

Related

How to execute DQL query in Symfony2?

I am trying to get objects from DQL query.
Here is my code :
$em = $this->getDoctrine()->getRepository(Item::class);
$items = $em->createQuery($getQuery);
$items = $query->getResult();
$getQuery = DQL query string : SELECT from Entity WHERE ...
I am receiving error : Undefined method 'createQuery'. The method name must start with either findBy or findOneBy!
I don't understand it, bcz this example is copied from official documentation.
How I can execute DQL query in queryBuilder/createQuery?
Entity manager has a createQuery method.
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery($getQuery);
$items = $query->getResult();
Repository has a createQueryBuilder method.
$em = $this->getDoctrine()->getManager();
$qb = $em->getRepository(Item::class)->createQueryBuilder();
$query = qb->select('[columns]')->from('Entity')->where('[condition]')->getQuery();
$items = $query->getResult();

Doctrine Query Builder Substring

Hi i got following line:
$query = $qb->select($qb->expr()->substring("p.website",1,$qb->expr()->length("p.website")-4))
->from("AppBundle\Entity\Image" ,"p")
->getQuery();
and got following Exception:
Notice: Object of class Doctrine\ORM\Query\Expr\Func could not be converted to int
I understand why this Exception is thrown but how is it possible to get a substring based on the string size with Query Builder?
OK got it...
The expression $qb->expr()->length("p.website") is first converted into LENGTH("p.website")
So the -4 has to concat as string.
$qb->expr()->length("p.website") **.'-4'**
$query = $qb->select($qb->expr()->substring("p.website",1,$qb->expr()->length("p.website").'-4))
->from("AppBundle\Entity\Image" ,"p")
->getQuery();
In your EntityRepository:
public function getAll()
{
$qb = $this->createQueryBuilder('p');
$query = $qb
->select($qb->expr()->substring("p.website", 1, $qb->expr()->length("p.website").'-4'))
->getQuery();
return $query->getResult();
}

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

Use PostgreSQL NOT SIMILAR TO in Symfony Doctrine query builder

I am attempting to use PostgreSQL's NOT SIMILAR TO exclude a blacklist from the results of a query,
When I run the query in my repository method below:
$qb = $this->getEntityManager()->createQueryBuilder('p');
$query = $qb
->select('p')
->from('CRMPiccoBundle:Person', 'p')
->where("lower(p.email) not similar to '(" . implode('|', $blacklist) . ")%'")
->getQuery();
return $query->getResult();
I get the following error:
[Doctrine\ORM\Query\QueryException]
SELECT p FROM CRMPiccoBundle:Person p WHERE lower(p.email) not similar to '(abuse#|admin#|billing#|compliance#|devnull#)%'
[Doctrine\ORM\Query\QueryException]
[Syntax Error] line 0, col 94: Error: Expected end of string, got 'to'
However, when I run this query against my local DB with PgAdmin it works.
How can I achieve this with Doctrine using the Symfony Doctrine Query builder (or similar)? I am using PostgreSQL 9.5.5
$qb = $this->getEntityManager()->createQueryBuilder('p');
$select = $qb
->select('p')
->from('CRMPiccoBundle:Person', 'p')
;
foreach ($blacklist as $key => $item) {
$select
->where('lower(p.email) NOT LIKE :key'.$key)
->setParameter('key'.$key, "$item%")
;
}
$query = $select->getQuery();
return $query->getResult();

order by LENGTH

In Symfony 1.4 and Doctrine 1.2 i can:
$bodies = Doctrine::getTable('Body')->createQuery('b')
->where('b.visible = 1')
->orderBy('LENGTH(b.title) ASC')
->execute();
and then working ok.
In Symfony 2 and Doctrine 2 i have:
$bodies = $this->getDoctrine()
->getRepository('MainBodyBundle:Body')
->createQueryBuilder('b')
->where('b.visible = 1')
->orderBy('LENGTH(b.title)', 'ASC')
->getQuery()
->getResults();
but this not working, i have error:
An exception has been thrown during the rendering of a template
("[Syntax Error] line 0, col 114: Error: Expected end of string, got
'('") in "MainBodyBundle::index.html.twig".
If you want to order by string length you should user LENGTH function in select:
$bodies = $this->getDoctrine()
->getRepository('MainBodyBundle:Body')
->createQueryBuilder('b')
->select('b,LENGTH(b.title) lgth')
->where('b.visible = 1')
->orderBy('lgth', 'ASC')
->getQuery()
->getResults();
The error in this case seems to be a syntax error and is coming from the twig file not the repository or controller. So i think the you should first check your index.html.twig for a syntax error rather than checking the query.

Categories