Symfony DQL syntax error - symfony

I'm trying to do DQL query, but having some troubles with it...
$user = $this->getUser();
$query = $em->createQuery(
'SELECT p
FROM AppBundle:User u
JOIN AppBundle:Follower f
JOIN AppBundle:Photo p
WHERE u.id = :id
AND f.follower_id = :id
AND p.user_id = f.user_id'
)->setParameter('id', $user->getId());
I am trying to get Photos (AppBundle:Photo) of those users, to whom the logged user is following.
Getting next error:
`[Syntax Error] line 0, col 128: Error: Expected =, <, <=, <>, >, >=, !=, got 'p'`
Whats wrong here with 'p' ?

I don't understand why follower are in your code, I don't see relation with photo...
After, I think you call Join but I don't see the relation you made with the photo...
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Photo p
JOIN p.user u
WHERE u.id = :id ')->setParameter('id', $user->getId());
Here is a part of the official doc :
Example:
Regular join of the address:
createQuery("SELECT u FROM User u JOIN u.address a
WHERE a.city = 'Berlin'"); $users = $query->getResult();
Fetch join of the address:
createQuery("SELECT u, a FROM User u JOIN
u.address a WHERE a.city = 'Berlin'"); $users = $query->getResult();

did yu try this?:
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Follower f
JOIN AppBundle:User u
WHERE u.id = :id
AND f.follower_id = :id
AND p.user_id = f.user_id' )->setParameter('id', $user->getId());

Related

DqlBuilder don't return values but mysql do

I have method findByTag() which should return entites with %#i% but it won't. This is sql which dql builded:
SELECT p0_.id AS id_0, p0_.content AS content_1, p0_.date AS date_2, p0_.user_id AS user_id_3 FROM post p0_ INNER JOIN user u1_ ON p0_.user_id = u1_.id LEFT JOIN points p2_ ON p0_.id = p2_.post_id LEFT JOIN comments c3_ ON p0_.id = c3_.post_id WHERE p0_.content LIKE '%#i%' ORDER BY p0_.date DESC LIMIT 20 if i enter it to mysql it return it ok.
Anybody have an idea?
$dql = $this->createQueryBuilder('p')
->innerJoin('p.user', 'c')
->leftJoin('p.points', 'pp')
->leftJoin('p.comments', 'cc')
->Where('p.content LIKE \'%#i%\'')
->setMaxResults($max)
->orderBy('p.date','DESC');
$dql->getQuery()
->getResult();
return $dql;
try it
$dql = $this->createQueryBuilder()
->from('YourBundleName:YourEntityName', 'p')
->innerJoin('YourBundleName.user', 'c')
->leftJoin('YourBundleName.points', 'pp')
->leftJoin('YourBundleName.comments', 'cc')
->Where('p.content LIKE \'%#i%\'')
->setMaxResults($max)
->orderBy('p.date','DESC');
return $dql->getQuery()->getResult();

DQL access id from object property with left join

I realized this sql which works without problems
SELECT meeting.name, meeting.date, community.name, participation.isPresent, participation.user_id
FROM meeting
INNER JOIN community
ON meeting.community_id = community.id
AND community.is_active = 1
LEFT join participation
ON meeting.id = participation.meeting_id
AND participation.user_id = 1078
WHERE meeting.date >= CURRENT_DATE()
ORDER BY meeting.date DESC
I'm trying to reproduce it with the doctrine query builder but I never got the right result. The user id part doesn't seem to be part of the leftJoin function but is applied to the request globally, which is not what I want.
public function getNextMeetings()
{
$qb = $this->createQueryBuilder('m')
->select('m.name AS meeting, m.date, c.name AS community, p.isPresent', 'IDENTITY(p.user) AS user')
->innerJoin('m.community', 'c')
->where('c.isActive = true')
->leftJoin('m.participations', 'p')
//->leftJoin('p.user', 'u')
//->where('u.id = 1078 OR u.id IS NULL')
//->where('IDENTITY(p.user) = 1078')
->andWhere('m.date >= CURRENT_DATE()')
->orderBy('m.date', 'DESC');
return $qb->getQuery()->execute();
}
My comments are what I tried to fix this issue.
Check Working with QueryBuilder: High level API methods
More precisely, the definition od leftJoin() function:
public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null);
You can place a condition on the joined Entity by:
use Doctrine\ORM\Query\Expr;
->leftJoin('m.participations', 'p', Expr\Join::WITH, 'p.user = :userId')
->setParameter('userId', 1078)
Note you do not need a condition for "meeting.id = participation.meeting_id", as this is autoapplied by the relation m.participations to the join constructed.

Semantical Error, Symfony DQL

I have a query that perform normally with MySQL :
SELECT *
FROM td_user u
JOIN td_ranking ranking ON ranking.user_id = u.id
JOIN (
SELECT x.user_id,
MAX(x.id) AS default_id
FROM td_ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id
I try to transform it in DQL for run it in Symfony :
$query = $this->_em->createQuery('
SELECT u.*,ranking.*
FROM UserBundle:User u
JOIN UserBundle:Ranking ranking
WITH ranking.user_id = u.id
JOIN (
SELECT x.user_id, MAX(x.id) AS default_id
FROM UserBundle:Ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id'
);
$results = $query->getResult();
I have this error :
[Semantical Error] line 0, col 113 near '(SELECT x.user_id,': Error: Class '(' is not defined.
Do you have any idea please ? Thanks!
Use native query
$rsm = new ResultSetMapping();
$sql = "
SELECT *
FROM td_user u
JOIN td_ranking ranking ON ranking.user_id = u.id
JOIN (
SELECT x.user_id,
MAX(x.id) AS default_id
FROM td_ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id
";
$result = $this->getEntityManager()->createNativeQuery($sql, $rsm)->getResult();

Subquery's with limit in Doctrine/Symfony using DQL

I would like to know how can I set limit on 1 query and then query based on the selected results.
For example I want to select the last 100 posts and the do some operation.
$first_query= $this->getEntityManager()
->createQuery(
'SELECT p FROM TestBundle:Post p ORDER BY p.date DESC'
)
->setMaxResults(5);
How to use this result to select from it in the next query?
return $this->getEntityManager()
->createQuery(
"SELECT u.username , max(p.date),d.points,l.name
FROM $first_query
JOIN p.location l JOIN p.user u JOIN u.deeds d
WHERE l.name = :location
GROUP BY u.id
ORDER BY d.points DESC , p.date DESC
"
)
->setParameter('location' , $location)
->getResult();
I found out that DQL doesn't support limits so I went for native sql.
$stmt = $this->getEntityManager()->getConnection()->prepare($query);
$stmt->execute();
return $stmt->fetchAll();
This way you can do whatever you want with the $query varaible you can set limits and everythign as it's a normal sql statement.

How to write the INNER JOIN in dql

This is my select and it works in workbench fine, but how did i write the INNER JOINS for doctrine?
$qb = $em->createQuery(
"SELECT oh.objektnummer, oh.idSubunternehmer, oh.datum, oh.typ, oh.outbox
FROM MBSAllgemeinBundle:ObjektHistory oh
INNER JOIN MBSAllgemeinBundle:Objekt o ON o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer
INNER JOIN MBSAllgemeinBundle:Subunternehmer s ON s.subunternehmernummer = o.id_subunternehmer
INNER JOIN MBSAllgemeinBundle:SubunternehmerUser su ON su.id_subunternehmer = s.subunternehmernummer
WHERE su.idUser = 1"
);
Try this syntax in your repository :
return $this->createQueryBuilder('oh')
->select('partial oh.{id, objektnummer, idSubunternehmer, datum, typ, outbox}')
->innerJoin('MBSAllgemeinBundle:Objekt', 'o', 'ON', 'o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer')
->addSelect('o')
// other INNER JOIN
// other INNER JOIN
->where('su.idUser = :id')
->setParameter('id', 1)
->getQuery()
->getResult()
;

Resources