How can i get the single result using DQL in symfony2 - php

I want to get the last user profile . But i am not able to do that in DQL.
I have this code
$em = $this->getEntityManager();
$dql = "SELECT p FROM AcmeBundle:UserProfile p
WHERE p.user_id = :user_id
ORDER BY p.createdAt DESC ";
$allProfiles = $em->createQuery($dql)
->setParameter('user_id', $user_id)
->setMaxResults(5)
->getResult();
return $allProfiles;
It returns all the profiles.
If i use getSingleResult() then it says result not unique

The right method is:
$singleProfile = $em->createQuery($dql)
->setParameter('user_id',$user_id)
->getSingleResult();
To prevent error then no results try this:
$singleProfile = $em->createQuery($dql)
->setParameter('user_id',$user_id)
->getOneOrNullResult();

$allProfiles = $em->createQuery($dql)
->setParameter('user_id',$user_id)
->setMaxResults(1)
->getResult();
return $allProfiles[0];

Related

How can I generate this query in doctrine?

How can I generate this query in doctrine or query builder?
SELECT EndDate from helios.fsa_audits order by StartDate desc limit 1;
Any idea or Advice about how to make it?
Seeing your entity here, in your controller you can try this:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder()
->select(array('a'))
->from(FsaAudits::class, 'a')
->orderBy("a.StartDate","DESC")
->setMaxResults(1);
$resultset = $qb->getQuery()->getResult();
if (count($resultset) <= 0) {
$fsaobj = $resultset[0];
echo $fsaobj->getEndDate();
}
See Working with query builder

How can i resolve this PDOException for this update query? [duplicate]

Using Doctrine 2 I want to get some users that are contacts of another user. The table user contains the mapping between those users. The query in the function will return the following error:
Invalid parameter number: number of bound variables does not match number of tokens.
However to my best understanding $stris set to "b" and $ownerId is set to "2" and both are assigned by the setParameters function.
protected function getContactBySubstring($str, $ownerId) {
echo $str;
echo $ownerId;
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->add('select', 'u')
->add('from', '\Paston\VerBundle\Entity\User u, \Paston\VerBundle\Entity\Contact c')
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
->add('orderBy', 'u.firstname ASC, u.lastname ASC')
->setParameters(array (1=> $ownerId, 2=> '%'.$str.'%'));
echo $qb->getDql();
$query = $qb->getQuery();
$users = $query->getResult();
foreach($users as $user)
echo $user->getUsername();
exit;
//return $contacts;
}
Don't surround any of the parameters in your query text with quotes!
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
should be
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE ?2")

Convert sql to doctrine orm in aend framework

I have a query which looks like below:
SELECT *
FROM mydb.users
left join mydb.job on
users.id = job.userid;
Now I am using doctrine orm in querying database but I am newbie here.
What I have done so far is below but it doesn't work as expected.
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select(array('a', 'c'))
->from('Admin\Entity\User', 'a')
->leftJoin('a.id', 'c');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
you do not need the from clause and array from clause select
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder('u');
$qb->select('u', 'j')
->leftJoin('u.jobs', 'j')
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
it is mandatory that your users entity contains a "one to many" jobs for the attribute.
trick to display the SQL output :
var_dump($qb->getQuery()->getSQL());

Restriction the results for relation

I have simple tables Post and Comment and I am doing query:
$repository = $this->getDoctrine()
->getRepository('AppBundle:Post');
$query = $repository->createQueryBuilder('p')
->where('p.made = :made')
->setParameter('made', 1)
->leftJoin('p.comments', 'c')
->andWhere('c.isAdmin = :isAdmin')
->setParameter('isAdmin', 1)
->getQuery();
$results = $query->getResult();
foreach($results as $post) {
echo $post->getId(); // this clause where (->where('p.made = :made')) working ok
foreach($post->getComments() as $comments) {
echo $comment->getId(); //this clause where (->andWhere('c.isAdmin = :isAdmin')) not working. This return all results
}
}
So how can I use clause where in query with relations?
SQL:
SELECT
i0_.id AS id0,
i0_.made AS made1,
i0_.name AS name2,
FROM
post i0_
LEFT JOIN comment i1_ ON i0_.id = i1_.comment_id
WHERE
i0_.made = ?
AND i1_.isAdmin = ?
Let's add a select method in your request:
$query = $repository->createQueryBuilder('p')
->select(["c", "p"])
->where('p.made = :made')
->setParameter('made', 1)
->leftJoin('p.comments', 'c')
->andWhere('c.isAdmin = :isAdmin')
->setParameter('isAdmin', 1)
->getQuery();
Is it better ?
This works better because you specify to hydrate the whole objects rather than just Ids.

Invalid parameter number: number of bound variables does not match number of tokens in Doctrine

Using Doctrine 2 I want to get some users that are contacts of another user. The table user contains the mapping between those users. The query in the function will return the following error:
Invalid parameter number: number of bound variables does not match number of tokens.
However to my best understanding $stris set to "b" and $ownerId is set to "2" and both are assigned by the setParameters function.
protected function getContactBySubstring($str, $ownerId) {
echo $str;
echo $ownerId;
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->add('select', 'u')
->add('from', '\Paston\VerBundle\Entity\User u, \Paston\VerBundle\Entity\Contact c')
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
->add('orderBy', 'u.firstname ASC, u.lastname ASC')
->setParameters(array (1=> $ownerId, 2=> '%'.$str.'%'));
echo $qb->getDql();
$query = $qb->getQuery();
$users = $query->getResult();
foreach($users as $user)
echo $user->getUsername();
exit;
//return $contacts;
}
Don't surround any of the parameters in your query text with quotes!
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
should be
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE ?2")

Categories