Order By on Joined Entity - Doctrine Query Builder - php

I have the following query:
$qb = $this->_em->createQueryBuilder();
$qb->select('a')
->from('CatalogueBundle\Entity\Avantage','a')
->leftJoin('a.avantagegrilles', 'g')
...
->orderBy('a.avantageOrdre', 'ASC');
The result of the query is as follows:
An avantage can contain several avantagegrilles. Now there is a variable in entity avantagegrille called avantagegrilleMini and I want to do an order by asc on it; that is, when an avantage is returned, its avantagegrilles are ordered according to their avantagegrilleMini.
I added ->orderBy('g.avantagegrilleMini', 'ASC') to the query but it didn't have any effect. Is what I want to implement possible in Doctrine?

Let's try this line: ->orderBy('g.avantageOrdre', 'ASC');

Related

Symfony Doctrine if statement in querybuilder

Is it possible to insert IF statement inside doctrine query builder? For example: I have User and Group entities with OneToMany relationship. Group has boolean field hidden. How to create query builder which would select groups that are hidden = false if Group owner is not current user. So that only group owner can see hidden=true groups. and other users can only see hidden=false groups
$qb = $this->createQueryBuilder('group')
->where('group.owner = :userId')
->setParameter('userId', $user->getId())
->orderBy('group.created', 'DESC');
This should fit your needs
$qb = $this->createQueryBuilder('group');
$qb
->where(
$qb->expr()->andX(
$qb->expr()->eq('group.owner', ':userId'),
$qb->expr()->eq('group.hidden', true)
)
)
->orWhere($qb->expr()->eq('group.hidden', false))
->setParameter('userId', $user->getId())
->orderBy('group.created', 'DESC');
First part of query will keep a row only if current user is the owner and group is hidden.
Second part will include all non-hidden groups.

how to select from subquery using query builder in laravel 5

please help me,
i have query:
SELECT * from (select * from products ORDER BY id DESC LIMIT 20) AS result ORDER BY discount DESC LIMIT 14
so, how to convert into query builder in laravel 5.
tks!
Using the query builder and importing (use) DB, allows you to build and fetch the result you want.
use DB;
$result = DB::table(
DB::raw("(" .
DB::table('products')
->select('*')
->orderBy('id', 'desc')
->limit(20)
->toSql()
. ") as result"
)
)
->select('*')
->orderBy('discount', 'desc')
->limit(14)
->get();
table() selects the table you want to query. In this case we are building up a separate SQL statement to fetch the table.
select() the columns you would like to see.
orderBy($column, $direction) where $column is the name of the column you would like to order and $direction is the order, desc or asc.
limit($limit) only return $limit items to the result set.
toSql() returns the current QueryBuilder SQL statement as a string.
get() returns the data in a Collection.
Also added in the missing discount ordering.
Try something like:
Products::orderBy('id', 'desc')->take(20)->orderBy('discount', 'desc')->take(14)->get();

How to set a where clause filter with doctrine2 query buider in a many-to-many relation

I've been trying with no success to user doctrine2 query builder to fetch records in a related many-to-many table using a where clause.
I would like to reproduce the following statement:
SELECT [...] FROM Company
JOIN CompanyAddress ON CompanyAddress.CompanyId = Company.Id
JOIN Address ON Address.Id = CompanyAddress.AddressId
WHERE Address.State = ?
following some ideias found on google, stackoverfow and doctrine docs:
$qb = $this->_em->createQueryBuilder();
$qb->select('c')
->from('Company', 'c')
->where(':State MEMBER OF c.Address')
->setParameter('State', $arguments);
but the results are not the desired one. Any help? Thanks..

How to write a Doctrine Query with multiple conditions?

I am trying to write a doctrine query that meets the following conditions:
published = 1
AND
subsection = "gui" OR "lvo" OR "tnn" OR "wer"
This is what I have:
$getPrograms = Doctrine::getTable('Program')
->createQuery()
->where('published=?', '1')
->andWhere('subsection=?', 'gui')
->orWhere('subsection=?', 'lvo')
->orWhere('subsection=?', 'tnn')
->orWhere('subsection=?', 'wer')
->orderBy('title ASC')
->execute();
This is pulling the correct subsection records, but is pulling records all published records, instead of only ones set to 1.
I feel I need to isolate the two conditions ( this AND (this OR this OR this)) but I do not know how.
Put your OR in the andWhere :
$getPrograms = Doctrine::getTable('Program')
->createQuery()
->where('published=?', '1')
->andWhere(
'subsection=? OR subsection=? OR subsection=? OR subsection=?',
array('gui', 'lvo', 'tnn', 'wer')
)
->orderBy('title ASC')
->execute();

PHP: Doctrine: order joined records

I'm new to doctrine: I have a problem with the sorting of joined records.
A sample.
I've got an Article model which is associated with a Source model in 1 <-> n. The source model has a property called 'position' with an integer value.
Now I want to fetch an article with it's sources orderes by the position. My DQL looks like this:
$q = Doctrine_Query::create()
->select('a.title, s.content')
->from('Article a')
->leftJoin('a.Source s')
->where('a.id = ?')
->orderBy('s.position');
The result doesn't change if I edit the position.
Best regards,
Sebastian
Hmm... it should do. Maybe try either of these:
->orderBy('s.position DESC')
->orderBy('s.position ASC')
Yes, it looks OK. Try to generate the SQL from the DQL with getSqlQuery() and query the database with the result. If there is still the wrong output, it might by a problem with the data or more likely, with the DQL.
Perhaps you should include the column that you are using for the ordering (s.position), so try this:
$q = Doctrine_Query::create()
->select('a.title, s.content, s.position')
->from('Article a')
->leftJoin('a.Source s')
->where('a.id = ?')
->orderBy('s.position');

Categories