I'm having a problem with a very simple query in Doctrine 2 ORM. I'm sure I've followed the docs to the letter, but it just won't work. I have this:
$qb = $this->em->createQueryBuilder()
->select('p')
->from('Property', 'p')
->where('type = :type');
$properties = $qb->getQuery()->setParameters(array(
'type' => 'house',
))->getResult();
And I get:
QueryException: [Semantical Error]
line 0, col 46 near 'type = :type':
Error: 'type' is not defined.
I've also tried:
$properties = $qb->getQuery()->setParameters(array(
':type' => 'house',
))->getResult();
With no luck. I'm sure this must be so simple, but I just can't see what's wrong.
Thanks.
->where('p.type = :type');
You always have to specify an owner of a property - Property entity in this case.
I've always done setParameter() on the QueryBuilder, not on the query.
Try
$qb = $this->em->createQueryBuilder()
->select('p')
->from('Property', 'p')
->where('type = :type');
$qb->setParameters(array('type' => 'house'));
$properties = $qb->getQuery()->getResult();
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 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();
I'm having an issue with doctrine inner joins.
$request = $em->getRepository('FYPFYPDesignBundle:SessionDesign');
$qb = $request->createQueryBuilder('SessionDesign');
$query = $qb
->select('SessionDesign.sessionID', 'SessionDesign.created','fos_user.username', 'fos_user.email', 'fos_user.fcid')
->from('FYPUserBundle:User', 'fos_user')
->innerJoin('fos_user.fcid', 'fos_user', 'ON', 'SessionDesign.sessionID = fos_user.fcid')
->where('fos_user.emailok = true')
->getQuery();
$result = $query->getResult();
The error it gives me is:
[Doctrine\ORM\Query\QueryException]
[Semantical Error] line 0, col 218 near 'fos_user ON SessionDesign.sessionID': Error: 'fos_user' is already defined.
You can't have an alias twice in your querybuilder.
Change:
->from('FYPUserBundle:User', 'fos_user')
->innerJoin('fos_user.fcid', 'fos_user', 'ON', 'SessionDesign.sessionID = fos_user.fcid')
To:
->from('FYPUserBundle:User', 'fos_user')
->innerJoin('fos_user.fcid', 'fcid', 'ON', 'SessionDesign.sessionID = fos_user.fcid')
Assuming fcid is an entity you want to join to.
Note I'm not sure why you're doing what you're doing, but that's what's causing the error.
Edit for more info.
If joining sessiondesign with no doctrine entity relationship (looks like that's what you're doing):
->join('YourBundle:SessionDesign', 's', 'WITH', 's.sessionID = fos_user.fcid')
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();
This is what I want to do:
$query = $repository->createQueryBuilder('c')
->where('c.categoryParentid = ?', $pcategory->getcategoryId())
->orderBy('c.categoryId', 'ASC')
->getQuery();
$childcategories = $query->getResult();
The $pcategory->getcategoryId() is an integer. The where, is this correct?
I get this error:
ContextErrorException: Warning: get_class() expects parameter 1 to be object, integer given in /Applications/mampstack-5.4.20-0/apache2/htdocs/reuzze/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Expr/Base.php line 92
I haven't used that syntax before, but the Symfony docs have examples of using the query builder, and they show something like this:
$query = $repository->createQueryBuilder('c')
->where('c.categoryParentid = :pcategory')
->setParameter('pcategory', $pcategory->getcategoryId())
->orderBy('c.categoryId', 'ASC')
->getQuery();
You can also use the EntityRepository::findBy() method (outlined in the Symfony 2.x documentation here) which allows you to specify search criteria and sorting.
e.g.
$childCategories = $repository->findBy(array(
'categoryParentId' => $pcategory->getCatagoryId()
),
array(
'categoryId' => 'ASC'
)
);