sql to doctrine query language - php

How I can write this sql to doctrine query language (DQL)?
SELECT *
FROM service
WHERE service.id NOT IN (SELECT id_service FROM reclamation)
Thanks.

try use not exists:
SELECT *
FROM service
WHERE NOT Exists (SELECT 1 FROM reclamation WHERE id_service = service.id)

Try to do this,
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT s
FROM YourBundle:Service s
WHERE s.id NOT IN
( SELECT s.idService FROM YourBundle:Reclamation r )'
)->getResult();
If that deserves, you can use the old school way! native-sql-with-docrtine

see the link http://doctrine.readthedocs.org/en/latest/en/manual/dql-doctrine-query-language.html#subqueries
$q = Doctrine_Query::create()
->select('s')
->from('Service s')
->where('s.id NOT IN (SELECT id_service FROM reclamation)
->getQuery()
->getResult();

Related

Doctrine 2: QueryBuilder Join with subquery

I need to convert native query to doctrine query.
Native query
select u.*, v.browser_id
from users u
left join visits v
on u.id = v.user_id
and v.id = (
select max(id) from visits v2
where v2.user_id = u.id
)
You can see right here that I need to select users with the latest browser they used to visit
So to convert this query to DQL I understand that I need to do something like this
$queryBuilder->leftJoin('u.visits', 'lastVis', Join::WITH,
// ????
);
but I don’t know what exactly. Perhaps someone will solve this quickly?
Thanks, seem I found the solution:)
$subQuery = $this->entityManager->createQueryBuilder()
->select('MAX(vis2.id)')
->from(Visit::class, 'vis2')
->where('vis2.user = u.id');
$this->entityManager->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->leftJoin(
'u.visits', 'vis',
Join::WITH, sprintf('vis.id = (%s)', $subQuery->getDQL()),
);

Doctrine - group by on count

Here is my code:
$qb = $this->_em
->createQueryBuilder();
$query = $qb->select('COUNT(food) as cnt')
->from(Food::class, 'food')
->groupBy('cnt')
->getQuery()->getSQL();
However my expectation like the following query:
SELECT COUNT(f0_.food_id) AS sclr_0 FROM foods f0_ WHERE (f0_.deleted_at IS NULL) GROUP BY sclr_0
The result looks like:
SELECT COUNT(f0_.food_id) AS sclr_0 FROM foods f0_ WHERE (f0_.deleted_at IS NULL) GROUP BY f0_.food_id
Any suggestion?
You'll need to use your query as a subquery, Id do this in raw SQL, but you can get the idea from:
$qb = $this->_em
->createQueryBuilder();
$query = $qb->select('COUNT(food) as cnt')
->from(Food::class, 'food')
->groupBy('cnt')
->getQuery()->getSQL();
$qb2 = $this->_em
->createQueryBuilder();
$query2 = $qb->select('sclr_0')
->from('('.$query.')', 'cnt')
->groupBy('sclr_0 ')
->getQuery()->getSQL();

MySQL query to Laravel Eloquent

I have the following MySQL query:
SELECT
*
FROM
news_posts
WHERE
user_id = ?
AND id NOT IN (SELECT
post_id
FROM
user_read
WHERE
user_id = ?)
I want to make it more "eloquent". I've tried the following, but its (obviously) not working:
NewsPost::where('user_id', Auth::id())
->whereNotIn(function ($query) {
$query->DB::table('user_read')
->select('post_id')
->where('user_id', Auth::id());
})->get();
I'm assuming the 'where' method is executing a simple SQL query on 'new_posts' table. Can you try to rewrite this query and let mysql do the filtering for new posts? (search for LEFT OUTER JOIN on mysql)
SELECT *
FROM news_posts a
JOIN user_read b
ON a.id = b.post_id
WHERE a.user_id = ?
AND b.post_id IS NULL;
Thanks for all the replies! Got it working with the following tweaks:
NewsPost::where('user_id', Auth::id())
->whereNotIn('id', function ($query) {
$query->select('post_id')
->from('user_read')
->where('user_id', Auth::id());
})
->get();

Symfony createNativeQuery, add count()

I have a query in a repository like :
$rsm = new ResultSetMapping;
$rsm->addEntityResult('\My\ProjectBundle\Entity\News', 't');
$rsm->addFieldResult('t', 'id', 'id');
$rsm->addMetaResult('t', 'account_id', 'account_id');
$qb = $this->_em->createNativeQuery(
'SELECT t.*
FROM news as t
LEFT JOIN
LEFT JOIN
WHERE
CONDITIONS CONDITIONS
',
$rsm
);
return $qb->getResult();
I simplified the above query which is used to retrieve the news that meet specific conditions.
I need to add a count() function to this query.
I have an other ManyToOne entity-relationship between Comment and News.
How to modify the query to get the comments number a given news has ?
I'm trying to add a left join to comment and add Count() in the select but I always get errors. How could I resolve this problem ?
Raw SQL with Doctrine is easier like this :
$em = $this->getDoctrine()->getManager()->getConnection();
$query = "
SELECT t.*
FROM news as t
LEFT JOIN
LEFT JOIN
WHERE
CONDITIONS CONDITIONS
";
$stmt = $em->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();

Doctrine Join DQL

I's like to do a join between 2 tables on a specific ID. At the moment, I have this DQL:
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_Item i ON e.itemId = i.itemId')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
The result is empty, although my eventId has a value ... Can you help me please? I there somewhere a tutorial on DQL-joins? I don't get it right with the help of the Doctrine documentation.
Thanks!
PS I have doctrine working in combination with Zend Framework.
you need add a relation to the model and join the tables using the relation
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_EventItem.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
you should change the name in the left join from Model_EventItem to e
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_EventItem.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e, e.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');

Categories