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.
Related
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 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
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.
I am trying to filter out admins that have a super admin role. Why is the following not working?
public function findAdmins()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('a')
->from('MyBundle:Admin', 'a')
->where($qb->expr()->notIn('ROLE_SUPER_ADMIN', 'a.roles'));
$result = $qb->getQuery()->execute();
return $result;
}
It will give me the following error:
[Syntax Error] line 0, col 68: Error: Expected Literal, got 'a'
The DQL-query looks like this:
SELECT a FROM MyBundle:Admin a WHERE ROLE_SUPER_ADMIN NOT IN(a.roles)
Role itself is not an entity. It is simply an array of strings.
$roles = array('ROLE_ADMIN', 'ROLE_SUPER_ADMIN)'
Try use MEMBER OF operator
$qb
->select('a')
->from('MyBundle:Admin', 'a')
->where(':role NOT MEMBER OF ad.roles')->setParamete('role', 'ROLE_SUPER_ADMIN');
Update.
The only solution found is to check serialized string in db with LIKE. Details here
Don't mix column name with table name
Try this,
$qb
->select('a')
->from('MyBundle:Admin', 'ad')
->where($qb->expr()->notIn('ROLE_SUPER_ADMIN', 'ad.roles'));
It should be like,
SELECT a FROM MyBundle:Admin ad WHERE ROLE_SUPER_ADMIN NOT IN(ad.roles)