I have the following query I'm executing in Symfony2 Repository. The query looks like
$q = $this
->createQueryBuilder('m')
->select(array('m.reciever','m.created','m.id','m.subject'))
->where('m.reciever = ?1')
->orderBy('m.id','DESC')
->setMaxResults( '?2' )
->setFirstResult( '?3' )
->setParameter(1,$id)
->setParameter(2,$itemsPerPage)
->setParameter(3,$offset)
->getQuery();
Where reciever, created, id, and subject are fields part of my message entity. I do not need to specify which entity I am selecting from. The error I keep getting is such...
[Semantical Error] line 0, col 12 near 'reciever, m.created,': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
I'm not sure what a state field path expression is or what the syntax might be. It seems like everything should be right.
When you use the select() method, you override the default one which is in $this->createQueryBuilder('m') . That's why you lost the m alias. To avoide this, use an addSelect() or specify the alias in the from() method:
->from('Bundle:Entity', 'ALIAS')
Do you have something like this:=?
$q = $this
->createQueryBuilder()
->select('m.reciever, m.created ,m.id , m.subject')
->from('/Acme/Entity/DemoEntity', 'm')
->where('m.reciever = ?1')
->orderBy('m.id','DESC')
->setMaxResults( '?2' )
->setFirstResult( '?3' )
->setParameter(1,$id)
->setParameter(2,$itemsPerPage)
->setParameter(3,$offset)
->getQuery();
Im not sure but I think you use the array syntax only if you have multiple tables to join together: array('m.reciever, m.created', 'p.user, p.id').
i hope this will help u..
$em = $this->getDoctrine()->getManager();
$q = $em->createQueryBuilder()
->select('m.reciever,m.created,m.id,m.subject')
->from('bundle:entity','m')
->where('m.reciever = ?1')
->orderBy('m.id','DESC')
->setMaxResults( '?2' )
->setFirstResult( '?3' )
->setParameter(1,$id)
->setParameter(2,$itemsPerPage)
->setParameter(3,$offset)
->getQuery();
Related
I have below sql query running fine,
SELECT completed_by, count(*) AS Total
FROM tasks
WHERE completed_by is not null AND status = 1
GROUP BY completed_by
;
Em am doing it with doctrine query builder, but not working returning an error.
$parameters = array(
'status' => 1,
);
$qb = $repository->createQueryBuilder('log');
$query = $qb
->select(' log.completedBy, COUNT(log) AS Total')
->where('log.Status = :status')
->groupBy('log.completedBy')
->setParameters($parameters)
->getQuery();
and getting below error;
[Semantical Error] line 0, col 21 near 'completedBy,': Error: Invalid
PathExpression. Must be a StateFieldPathExpression.
I know this answer can be late, but I struggled with the exact same problem, and did not find any answer on the internet, and I believe a lot of people will struggle in this same issue.
I'm assuming your "completedBy" refers to another entity.
So, inside your repository, you can write:
$query = $this->createQueryBuilder("log")
->select("completer.id, count(completer)")
->join("log.completedBy", "completer")
->where('log.Status = :status')
->groupBy("completer")
->setParameters($parameters)
->getQuery();
This will compile to something like:
SELECT completer.id, count(completer) FROM "YOUR LOG CLASS" log INNER JOIN log.completedBy completer WHERE log.Status=:status GROUP BY completer
Now, You can do another query to get those 'completers', by their ids.
This is wrong: COUNT(log) AS Total. It should be something like COUNT(log.log) AS Total.
When you want to select a column who is a fk for another table (entity), use the IDENTITY function instead of the column name only.
Example: In your case
$parameters = array(
'status' => 1,
);
$qb = $repository->createQueryBuilder('log');
$query = $qb
->select('IDENTITY(log.completedBy), COUNT(log.something) AS Total')
->where('log.Status = :status')
->groupBy('log.completedBy')
->setParameters($parameters)
->getQuery();
i have the following code in my Entity Repository class:
$qb
->select('d')
->addOrderBy('d.dtrDate', 'DESC')
->where($qb->expr()->isNotNull('d.deletedAt'))
->leftJoin('d.user', 'u')
->where('u.id = :user_id')
->setParameter('user_id', $user->getId());
So basically, I dont want to select an entity where deletedAt property is not null. However it gives me a query like this:
>
SELECT
d0_.id AS id_0,
d0_.dtr_date AS dtr_date_1,
d0_.clock_in AS clock_in_2,
d0_.clock_out AS clock_out_3,
d0_.total_time AS total_time_4,
d0_.memo AS memo_5,
d0_.last_update_IP AS last_update_IP_6,
d0_.created_at AS created_at_7,
d0_.updated_at AS updated_at_8,
d0_.deleted_at AS deleted_at_9,
d0_.user_id AS user_id_10
FROM
daily_time_record d0_
LEFT JOIN user u1_ ON d0_.user_id = u1_.id
WHERE
u1_.id = ?
ORDER BY
d0_.dtr_date DESC
As you can see my first where is not being called. So how can i work on this?
You should read the documents, or read the ORM source to see how things work actually. TO be short, the answer to your question is:
$qb
->select('d')
->leftJoin('d.user', 'u')
->where($qb->expr()->isNotNull('d.deletedAt'))
->andWhere('u.id = :user_id')
->setParameter('user_id', $user->getId())
->orderBy('d.dtrDate', 'DESC');
I have this query below:
SELECT *
FROM mydb.users
left join mydb.jobs
on users.user_id = jobs.job_id;
And I used to convert them in orm query just like below:
return
$qb = $this->entityManager->createQueryBuilder();
$qb->select('rb', 'l')
->from('Admin\Entity\Users', 'rb')
->leftJoin(
'Admin\Entity\Jobs',
'l',
\Doctrine\ORM\Query\Expr\Join::WITH,
'rb.user_id = l.job_id'
)
->getQuery()
->getResult(AbstractQuery::HYDRATE_ARRAY);
But it doesn's still work. I get the following error:
PHP Fatal error: Call to a member function createQueryBuilder() on null
please help i don't know what to do.
Try this
$entityManager = $this->serviceLocator->get('Doctrine\ORM\EntityManager');
$qb = $entityManager->createQueryBuilder();
Or make sure $this->entityManager is assigned.
Well it simply means you don't have an EntityManager instance in $this->entityManager.
My guess would be that $this->entityManager is probably null because you did not inject an EntityManager inside your class instance.
I've got a problem with Symfony/Doctrine2 doing SQL statement with two entites:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('s')
->from('basecomProductionWorkflowBaseBundle:SubprocessData', 's')
->leftJoin('basecomProductionWorkflowBaseBundle:ReleaseDay', 'r', Expr\Join::WITH, 'r.id = s.releaseDay')
->where(
$qb->expr()->andX(
$qb->expr()->eq('r.date', ':date'),
$qb->expr()->isNotNull('r.edition')
)
)
->setParameter('date', $date);
I got the following error message:
[Semantical Error] line 0, col 124 near 'r WITH r.id =': Error: Identification Variable basecomProductionWorkflowBaseBundle:ReleaseDay used in join path expression but was not defined before.
PS: Both tables have no relation to each other (it's a workaround fixing another problem). I've tested the same statement in phpmyadmin.
It should be:
->leftJoin('s.releaseDay', 'r')
You could also simplify the conditions this way:
->where('r.date = :date')
->andWhere('r.edition IS NOT NULL')
or:
->where('r.date = :date AND r.edition IS NOT NULL')
¿What Expr class are you using?
I choose incorrectly the Expr class some days ago and throw me same exception.
Try:
use Doctrine\ORM\Query\Expr;
I have the following Doctrine2 query:
$qb = $em->createQueryBuilder()
->select('t.tag_text, COUNT(*) as num_tags')
->from('CompanyWebsiteBundle:Tag2Post', 't2p')
->innerJoin('t2p.tags', 't')
->groupBy('t.tag_text')
;
$tags = $qb->getQuery()->getResult();
When run I get the following error:
[Semantical Error] line 0, col 21 near '*) as num_tags': Error: '*' is not defined.
How would I do MySQL count(*) in Doctrine2?
You should be able to do it just like this (building the query as a string):
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
You're trying to do it in DQL not "in Doctrine 2".
You need to specify which field (note, I don't use the term column) you want to count, this is because you are using an ORM, and need to think in OOP way.
$qb = $em->createQueryBuilder()
->select('t.tag_text, COUNT(t.tag_text) as num_tags')
->from('CompanyWebsiteBundle:Tag2Post', 't2p')
->innerJoin('t2p.tags', 't')
->groupBy('t.tag_text')
;
$tags = $qb->getQuery()->getResult();
However, if you require performance, you may want to use a NativeQuery since your result is a simple scalar not an object.
As $query->getSingleScalarResult() expects at least one result hence throws a no result exception if there are not result found so use try catch block
try{
$query->getSingleScalarResult();
}
catch(\Doctrine\ORM\NoResultException $e) {
/*Your stuffs..*/
}