How to write a Doctrine Query with multiple conditions? - php

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();

Related

Order By on Joined Entity - Doctrine Query Builder

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');

Filtering out rows where two columns equal one thing

I have the following query. I'd like for it to filter out rows after the join where both type and relationship equal 'communicator'. I tried whereRaw('(type <> communicator and relationship <> communicator') but I just get a nasty error. How can I achieve the result I'm looking for?
Relationship is in titles_to_communicators and type is in communicators.
$query = \DB::table('titles_to_communicators')
->leftJoin('communicators', 'communicators.id', '=', 'titles_to_communicators.communicator_id')
->where('relationship', '<>', 'character')
->whereIn('title_id', $childIds)
->groupBy('communicators.slug')
->limit(40);
try
where (type = "communicator" and relation = "communicator")

I want to find top 10 favorite trails in laravel queries

I have two tables named 'favorites' and 'trails'. I want to show top 10 favorites trails for users. I made 'many to many' relationship between them. But, I am not sure how to make the query.What should be the right query.Would someone help me for the right one. Without relationship, I tried something like this-
$favorites = DB::table('favorites')
->join('trails', 'trails.id', '=', 'favorites.trail_id')
->select('favorites.trail_id', 'trails.name')
->get();
First one is 'trails' table and another one is 'favorites' bellow -
To get the top 10 trails you need to use aggregate function to count no of users for each trail and order your results based on the result of count and then select only 10
$favorites = DB::table('trails as t')
->select('t.id', 't.name')
->join('favorites as f', 't.id', '=', 'f.trail_id')
->groupBy('t.id')
->groupBy('t.name')
->orderByRaw('COUNT(DISTINCT f.user_id) DESC')
->limit(10)
->get();

How can I make select in select on laravel eloquent?

I use laravel 5.3
My sql query is like this :
SELECT *
FROM (
SELECT *
FROM products
WHERE `status` = 1 AND `stock` > 0 AND category_id = 5
ORDER BY updated_at DESC
LIMIT 4
) AS product
GROUP BY store_id
I want to change it to be laravel eloquent
But I'm still confused
How can I do it?
In cases when your query is to complex you can laravel RAW query syntax like:
$data = DB::select(DB::raw('your query here'));
It will fire your raw query on the specified table and returns the result set, if any.
Reference
If you have Product model, you can run
$products = Product::where('status', 1)
->where('stock', '>', 0)
->where('category_id', '=', 5)
->groupBy('store_id')
->orderBy('updated_at', 'desc')
->take(4)
->get();
I think this should give you the same result since you pull everything from your derived table.

How to make the active record many to many relationship easier to develop in codeigniter?

I have the table like this:
[Note]
[id]
[Note_Tag_list]
[Note_id]
[Tag_id]
[Tag]
[id]
Is there any active record to let me ONLY join these three tables?
Because every time I based on the note id to loop back the note_tag_list record, and use the note tag list record, make me feel really, really annoying.
The flow I use:
//loop the note the user request
//get the note by id
//use the note id to get the note tag list, and return the array of note tag list
//loop the array of note tag list and use the tag id to quest the tag
How do I do make this easier?
Maybe you could try datamapper :)
I know this is an old question, but anyways i will answer it for anyone who faces the same problem in future.
you need to use a JOIN query to connect multiple tables using primary and foreign keys relation.
Code to get all records
$q = $this->db
->select()
->from('Note AS n')
->join('Note_Tag_list AS n_t_l', 'n_t_l.Note_id = n.id', 'left')
->join('Tag AS t', 't.id = n_t_l.Tag_id', 'left')
->get();
Code to get only specific records (by using WHERE clause)
$where = array(
'n.id' => **provide note id here**
);
$q = $this->db
->select()
->from('Note AS n')
->join('Note_Tag_list AS n_t_l', 'n_t_l.Note_id = n.id', 'left')
->join('Tag AS t', 't.id = n_t_l.Tag_id', 'left')
->where($where)
->get();
For more information read the codeigniter's docs.
Codeigniter Query Builder

Categories