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.
Related
Am working on a symfony project with backend developed in doctrine & api-platform framework.I need to fetch some details along with checking a field in another table which will be a status. We use this status for handling something in front end.
I tried:-
$qb = $this->createQueryBuilder('contact');
$qb2=$qb;
$sub_query = $qb2->select('field')
->from('OtherTable','g')
->where("'id= '".$personId."'")
->OrderBy('updated_at', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult();
$qb->select("contact.id,
contact.title,
count (distinct person.id) as
number_of_contacts_with_email',(".$sub_query.") as status")
->leftjoin('contact.people', 'person')
->leftJoin('person.jobs', 'jobs')
->groupBy('contact.id, contact.title');
$query=$qb->getQuery();
$result = $qb->getQuery()->getArrayResult();
return $result;
Am getting this error when executing query.
[Semantical Error] line 0, col 59 near 'OtherTable g': Error: Class 'OtherTable' is not defined.
How to write this sub query here?Is there any solution for this?
You should simply use the DQL of the subquery, as example:
// Don't take the query/result instances
$sub_query = $qb2->select('field')
->from('OtherTable','g')
->where("'id= '".$personId."'")
->OrderBy('updated_at', 'DESC')
->setMaxResults(1);
and use
$qb->select("contact.id,
contact.title,
count (distinct person.id) as
number_of_contacts_with_email',(".$sub_query->getDQL().") as status")
->leftjoin('contact.people', 'person')
->leftJoin('person.jobs', 'jobs')
->groupBy('contact.id, contact.title');
Hope this help
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);
I was following the Symfony2 blog tutorial part 4
and I encountered this error when I tried to query the post's comments:
public function postAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$post = $em->getRepository('BlogBundle:Post')->find($id);
if (!$post) {
throw $this->createNotFoundException('Unable to find Blog post.');
}
$comments = $em->createQueryBuilder('c')
->select('c')
->where('c.post = :post_id')
->addOrderBy('c.created')
->setParameter('postId', $id)
->getQuery()
->getResult();
return $this->render('BlogBundle:Default:post.html.twig', array(
'post' => $post,
'comments' => $comments
));
}
I get the following error:
"[Semantical Error] line 0, col 15 near 'c.post = :post_id': Error:
Class 'c' is not defined."
The link to the tutorial you gave doesn't work for me so I can't see the context of what your are doing but...
The parameter for createQueryBuilder() is the name of the table so you have a table called 'c'. This seems an odd name for a table.
In your query your should be selecting fields from the table, not selecting the table.
Hope that helps but if you can give a link that works I can better see what you're trying to acheive.
You can not call createQueryBuilder directly on EntityManager because it does not know what c is. This is because you just copy&paste without context and code was invoked inside repository class in tutorial. Not in controller.
Try this
$comments = $em->getRepository('BlogBundle:Comment')->createQueryBuilder('c')
->select('c')
->where('c.post = :post_id')
->addOrderBy('c.created')
->setParameter('postId', $id)
->getQuery()
->getResult();
Or add method for fetching comments into PostRepository similarly as in tutorial.
You should speify the Entity you need:
$comments = $em->createQueryBuilder('c')
->from('BlogBundle:Comment', 'c') // I suppose your entity is Comment
->select('c')
->where('c.post = :post_id')
->addOrderBy('c.created')
->setParameter('postId', $id)
->getQuery()
->getResult();
I'm trying to make a query in my Symfony project, I make it with the following code:
$em = $this->getDoctrine()->getManager();
$countUnreadPm = $em->createQueryBuilder()
->select('count(*)')
->from('Privatemessage', 'pmid')
->getQuery()
->getResult();
However this gets me a semantical error:
[Semantical Error] line 0, col 21 near 'Privatemessage': Error: Class 'Privatemessage' is not defined.
The entity class is named 'Privatemessage' so no problems there. What is the issue?
count(pmid) - DQL count objects.
->from('AcmeDemoBundle:Privatemessage') - Full path to entity eg. AcmeDemoBundle
$em = $this->getDoctrine()->getManager();
$countUnreadPm = $em->createQueryBuilder()
->select('count(pmid)')
->from('AcmeDemoBundle:Privatemessage', 'pmid')
->getQuery()
->getResult(); // or ->getSingleScalarResult(); For integer value.
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