CreateQuery Doctrine Symfony 2 StateFieldPathExpression - php

I have this Query in MySql
SELECT l.id as like_id, l.spotted_id as spotted_id,count(l.spotted_id) as numero_likes
FROM sn_like_spotted l
LEFT JOIN prof_foto f
ON f.id = l.spotted_id
LEFT JOIN sn_profilo p
ON f.profilo_id = p.id
WHERE p.id = 3
GROUP BY l.spotted_id
ORDER BY numero_likes DESC LIMIT 0,10
I try to do this in Doctrine
public function getFoo($profilo_id){
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT l.id as like_id, l.spotted_id as spotted_id,count(l.spotted_id) as numero_likes
FROM SNLikeBundle:LikeSpotted l
LEFT JOIN SNFotoBundle:Foto f
WITH f.id = l.spotted_id
LEFT JOIN SNProfiloBundle:Profilo p
WITH f.profilo_id = p.id
WHERE p.id = :profilo_id
GROUP BY l.spotted_id
ORDER BY numero_likes DESC
')->setFirstResults(0)
->setMaxResults(10)
->setParameter('profilo_id', $profilo_id);
$results = $query->getResult();
return $results;
}
Then i have this error
[Semantical Error] line 0, col 36 near 'spotted_id as': Error: Class SN\LikeBundle\Entity\LikeSpotted has no field or association named spotted_id
I change SELECT in:
SELECT l.id as like_id, l.spotted as spotted_id,count(l.spotted) as numero_likes
and I have this Error:
[Semantical Error] line 0, col 36 near 'spotted as spotted_id,count(l.spotted)': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
Then i Try With IDENTITY
SELECT IDENTITY l.id as like_id, l.spotted as spotted_id,count(l.spotted) as numero_likes
But i Have This Error
[Syntax Error] line 0, col 26: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '.'
I try to create also in QueryBuilder (more criteria, same result)
$q = $this->createQueryBuilder('l');
$q->leftJoin("l.spotted", 's');
$q->leftJoin("s.profilo", 'p');
$q->leftJoin("p.utente", 'u');
$q->where('(s.foto_eliminata IS NULL OR s.foto_eliminata != 1)');
$q->andWhere('p.fase_registrazione = :fase');
$q->andWhere('u.locked = :false');
$q->andWhere('p.id = :profilo_id');
$q->setParameter(':fase', 100);
$q->setParameter('false', false);
$q->setParameter('profilo_id', $profilo_id);
$q->groupBy('l.spotted');
$q->orderBy($q->expr()->count('l.spotted'), 'desc');
$q->setMaxResults(10);
$dql = $q->getQuery();
$results = $dql->execute();
return $results;
My error is
[Syntax Error] line 0, col 285: Error: Expected end of string, got '('
The problem is in
$q->orderBy($q->expr()->count('l.spotted'), 'desc');

For CreateBuilder Case I Resolved:
$q = $this->createQueryBuilder('l');
$q->addSelect('count(l.spotted) as numero_likes');
$q->leftJoin("l.spotted", 's');
$q->leftJoin("s.profilo", 'p');
$q->leftJoin("p.utente", 'u');
$q->where('(s.foto_eliminata IS NULL OR s.foto_eliminata != 1)');
$q->andWhere('p.fase_registrazione = :fase');
$q->andWhere('u.locked = :false');
$q->andWhere('p.id = :profilo_id');
$q->setParameter(':fase', 100);
$q->setParameter('false', false);
$q->setParameter('profilo_id', $profilo_id);
$q->groupBy('l.spotted');
$q->addOrderBy('numero_likes','DESC');
$q->setMaxResults(10);
$dql = $q->getQuery();
$results = $dql->execute();
return $results;
if i don't want numero_likes in my results i can do this:
$q->addSelect('count(l.spotted) as HIDDEN numero_likes');
But i'd like how can i do this in createQuery

Related

group_concat and join codeigniter giving fatal error

this is my query in model (in my sql this query is working fine but in code -igniter it gives me fatal error(Call to a member function result() on a non-object )
$this->db->select('j.*, c.name as Cname, d.name as Dname, e.name as
Ename,GROUP_CONCAT(s.qualification SEPARATOR ",") as
Qualification,GROUP_CONCAT(s.year SEPARATOR ",") as
Year,GROUP_CONCAT(s.institute SEPARATOR ",") as Institute' );
$this->db->from('jobseekers j');
$this->db->join('city c','c.id = j.location','left');
$this->db->join('vertical e','e.id = j.vertical','left');
$this->db->join('designation d','d.id = j.designation','left');
$this->db->join('jobseekers_qul s','s.jobseekers_id = j.id','left');
$this->db->group_by('j.id');
$query = $this->db->get();
$row = $query->result();
return $row;

DQL Subquery Questions

I'm trying to SLECT * FROM a table o where the vendor is what I pass.(this is in the context of a Doctrine Repository). I then want to run a subquery and SELECT * FROM AppBundle:PriceOption where p.offer is o. I'm getting a QueryException when running this code though:
public function getVendorFeaturedDeals(Vendor $vendor){
$purchaseOptions = $this->
getEntityManager()
->createQueryBuilder()
->from('AppBundle:PriceOption', 'p')
->innerJoin('p.offer', 'o')
->getDQL();
$query = $this->createQueryBuilder('o');
return $query
->where('o.vendor = :vendor')
->addSelect(sprintf('(%s)', $purchaseOptions))
->setParameter(':vendor', $vendor)
->getQuery()
->execute();
}
Here's the error : AppBundle\Tests\Service\VendorServiceTest::testGetVendorFeaturedDeals
Doctrine\ORM\Query\QueryException: [Syntax Error] line 0, col 18: Error: Unexpected 'FROM' Caused by Doctrine\ORM\Query\QueryException: SELECT o, (SELECT FROM AppBundle:PriceOption p INNER JOIN p.offer o) FROM AppBundle\Entity\Offer o WHERE o.vendor = :vendor
Any help would be appreciated, thanks!
You should modify your query to:
$purchaseOptions = $this->
getEntityManager()
->createQueryBuilder()
->select(['p', 'o'])
->from('AppBundle:PriceOption', 'p')
->innerJoin('p.offer', 'o')
->getDQL();

Symfony Doctrine get entity through another entity

I have 3 entities: User, ImaginaryBankAccount and Recharge. One user has one ImaginaryBankAccount and ImaginaryBankAccount can have more than one Recharges. And I want to select from DB all Recharges that belongs to one user.
I have this query:
$resResults = $query->getResult();
$query = $em->createQuery('SELECT rec
FROM AppBundle:Recharge rec
WHERE rec.dateTime > :tresholdDate
AND rec.imaginaryBankAccount.user = :user
ORDER BY rec.dateTime'
)->setParameter('tresholdDate', $dateXDaysBack)
->setParameter('user', $filter->getUser());
$recResults = $query->getResult();
But it throws error:
[Semantical Error] line 0, col 223 near 'user = :user
': Error: Class AppBundle\Entity\Recharge has no field or association named imaginaryBankAccount.user
How can I achieve my goal with Doctrine2?
You have to add a JOIN clause with your imaginaryBankAccount relation like this :
SELECT rec
FROM AppBundle:Recharge rec
JOIN rec.imaginaryBankAccount i
WHERE rec.dateTime > :tresholdDate
AND i.user = :user
ORDER BY rec.dateTime

Doctrine query "where notIn" subquery issue

In Symfony2, I have a many:many relationship between users and roles.
I am trying to get a list of all the users which are not linked to the ROLE_SUPER_ADMIN role.
Before migrating to Symfony2/Doctrine, I had achieved this with a simple NOT IN sql query, but for the life of me I can't achieve the same effect with doctrine.
Here is what I am trying:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$qb2 = $qb;
$dql = $qb->select('sa.id')
->from('AcmeAdminBundle:User', 'sa')
->leftJoin('sa.roles', 'r')
->andWhere('r.role = :role')
->getDQL();
$result = $qb2->select('u')
->from('AcmeAdminBundle:User', 'u')
->where($qb2->expr()->notIn('u.id', $dql))
->setParameter('role', 'ROLE_SUPER_ADMIN')
$users = $result->getQuery()->getResult();
But this is the error:
[Semantical Error] line 0, col 140 near 'sa LEFT JOIN':
Error: 'sa' is already defined.
And this is the output:
SELECT u
FROM AcmeAdminBundle:User sa
LEFT JOIN sa.roles r, AcmeAdminBundle:User u
WHERE u.id NOT IN (
SELECT sa.id
FROM AcmeAdminBundle:User sa
LEFT JOIN sa.roles r
WHERE r.role = :role
)
No idea why it is outputting like that as it should not be performing LEFT JOIN two times, my suspicion is that it has something to do with having two QueryBuilder instances, but could be something else entirely.
You need the MEMBER OF or in your case NOT MEMBER OF option.
$qb->select('sa.id')
->from('AcmeAdminBundle:User', 'sa')
->where(":Role NOT MEMBER OF sa.roles")
->setParameter("Role", <<ROLE_ID_OR_ROLE_ENTITY>>);
I didn't test this code, but it should give you some idea.
Full documentation can be found at http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html

doctrine2 select in from

How to do this query in Doctrine 2 QueryBuilder:
SELECT AVG(x.distance) avg_distance FROM (SELECT r.* FROM result r WHERE r.place_id = ? GROUP BY r.place_id ORDER BY r.id DESC LIMIT 100
I try this:
$dql = $qb
->select('r.*')
->from('CoreBundle:Result', 'r')
->where('r.place = :place')
->orderBy('r.id', 'DESC')
->setMaxResults(100)
->setParameter('place', $place)
->getDQL()
;
$result = $qb
->select('AVG(x.distance) avg_distance')
->from($dql, 'x')
->getQuery()
->getArrayResult();
but not work
SELECT r.* FROM': Error: Class 'SELECT' is not defined.
$sql = "SELECT AVG(x.distance) avg_distance FROM (SELECT r.* FROM result r WHERE r.place_id = :place_id ORDER BY r.id DESC LIMIT 100) x ";
$stmt = $this->em->getConnection()->prepare($sql);
$stmt->bindValue(':place_id', $place->getId());
$stmt->execute();
return $stmt->fetch();

Categories