Yii2 How to perform where AND or OR condition grouping? - php

I am new to Yii-2 framework. How can i achieve following query in Yii-2 framework using activeQuery and models.
SELECT * FROM users AS u WHERE u.user_id IN(1,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1)
Thanks

You can try this:
//SELECT * FROM users AS u WHERE u.user_id IN(1,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1)
$model = arname()->find()
->andWhere(['user_id'=>[1,5,8]])
->andWhere(['or',
['status'=>1],
['verified'=>1]
])
->orWhere(['and',
['social_account'=>1],
['enable_social'=>1]
])
->all();

try this -
$query = (new \yii\db\Query())
->select('*')
->from('users u')
->where(['and',['u.user_id'=>[1,5,8]],['or','u.status=1','u.verified=1']])
->orWhere(['u.social_account'=>1,'u.enable_social'=>1]);
$command = $query->createCommand();
print_r ($command->sql);die;
more info

I assume that you have already knew about database configuration in Yii 2.0, which is basically the same as in Yii 1.0 version.
If you want to use activeQuery, you need to define a ‘USERS’ class first:
<?php
namespace app\models;
use yii\db\ActiveRecord;
class USERS extends ActiveRecord {
public static function tableName()
{
return 'users';
}
}
?>
Then when you use it,you can write it as following:
<?
$usr_data = USERS::find()->
->where("user_id IN(1,5,8) AND (status = 1 OR verified = 1) OR (social_account = 1 AND enable_social = 1)")
->all();
?>
In my opinion, active query provides you a way to separate sql by sub-blocks. But it does not make any sense to apply it when you have such a complicated 'AND OR' WHERE condition..

U can also do it this way:
$data = model::find()
->andWhere('plz = :plz',[':plz' => 40])
->andWhere('plz = :plz',[':plz' => 40000])
->all();

User::find()
->select('u_id , u_unique_id, u_first_name, u_last_name, u_username, u_email, u_image,u_phone')
->andWhere("u_status != 2 AND u_id != 1 and u_role in (6,7)")
->andWhere(
['or',
['like', 'u_first_name', $searchVal],
['like', 'u_last_name', $searchVal],
['like', 'u_username', $searchVal],
['like', 'u_email', $searchVal]
]
)
->all();

With MongoDB:
$query->andWhere(['and',
['$eq', 'status', 1],
['$in', 'activity', [1, 2]],
]);
$query->orWhere(['$in', 'yourField', $yourArray]);
Tested. Similar with DBMS.

Use OR condition at first. For example:
(new \yii\db\Query())
->select(['id', 'client', 'ts', 'action'])
->from('log_client as log')
->orWhere(['action' => 'lock'])
->orWhere(['action' => 'rel'])
->andWhere(['in', 'client', $IDs])
->orderBy(['ts' => SORT_ASC])
->all();
It'll be like a "AND...(..OR..)"

where() function of ActiveQuery also accepts string as its first parameter that will be used in WHERE condition of the query. So easiest way would be to put those conditions in where()function.
Assuming you are using ActiveRecord model, you can do something like following in your model.
$this->find()
->where("user_id IN(1,5,8) AND (status = 1 OR verified = 1) OR (social_account = 1 AND enable_social = 1)")
->all();
Of course you should use prepared statements instead of hardcoding values inside query, but above code is just to give you an idea.
You can find more detail in documentation Here and Here

Related

laravel raw join query

I have a function that accepts raw where conditions and joins:
query('data',['fieldA','fieldB'], 'fieldA > 10 AND fieldB < 20', 'LEFT JOIN users ON data.user_id = users.id');
function query($table, $keys = [], $where = '', $joins = '') {
$query = DB::table($table)->select($keys);
if(!empty($where)) {
$query=$query->whereRaw($where);
}
if(!empty($joins)) {
$query=$query->?????????????
}
return $query->get();
}
How do I use the raw join with the query builder the way I can use whereRaw for the where condition?
Haven't good smell but works
\DB::table('data')->join('user', function($join){
$join->on(\DB::raw('( `data`.`user_id` = `user.id` or (`data`.`user_id` is null and `data`.`other_field` is null)) and 1 '),'=',\DB::raw('1'));
})
Don't bother with the query builder if you're just using raw expressions on everything.
Option 1: Utilize PDO
PDO is the underlying driver used by Laravel. To get the PDO object, run:
$pdo = DB::connection()->getPdo();
Option 2: Run raw queries
You can run entire selects through Laravel without "building" a query:
DB::select("SELECT * FROM table WHERE ..");
This even allows parameter binding when you need it.
https://laravel.com/docs/5.5/database
Here I have put some idea how can you make your method little bit generic. Do same thing for where clause and other part of the query.
query('data',['fieldA','fieldB'], 'fieldA > 10 AND fieldB < 20', 'LEFT', 'users', 'data.user_id','users.id');
function query($table, $keys = [], $where = '', $join_type = 'LEFT', $join_table='', $join_field1='', $join_field2='') {
$query = DB::table($table)->select($keys);
if(!empty($where)) {
$query=$query->whereRaw($where);
}
if(!empty($joins)&&!empty($join_type)&&$join_type=='LEFT') {
$query=$query->leftJoin($join_table, $join_field1, '=', $join_field2)
}
if(!empty($joins)&&!empty($join_type)&&$join_type=='INNER') {
$query=$query->join($join_table, $join_field1, '=', $join_field2)
}
///and so on
return $query->get();
}
//But you can make this method more generic, I just put some idea so that you can make this method more flexible
//I did not take a look at your other part of the method though.
Note: I did not put multiple joins only two table joins but you can make your method more generic form and more flexible. I just tried to put some idea that comes into my mind. If I am wrong then correct me please.
Reference:
Laravel - Joins
DB::table('data')->join('users','data.user_id','users.id')->where(~~~)

Implementing an or where condition in yii2 find

Am performing a find() in yii2 I understand that there can be andwhere but what of orWher
I have tried
$query = Tblpr::find()->where(['PRID'=>2])->andwhere(['new'=>1])->all();
How can i implement orWhere
Using Where OR
$query = Tblpr::find();
$query->andFilterWhere(['or',
['PRID',2],
['new',1]
])->all();
OR
$query = Tblpr::find()->select('*')
->orWhere(['PRID'=>2,'new'=>1])->all();
You can also use createCommand
$query = (new \yii\db\Query())
->select('*')
->from('Tblpr') // put your table name here
->where(['PRID'=>[2]])
->orWhere(['new'=>[1]]);
$command = $query->createCommand();
print_r ($command->sql);die;
The following should work for a query with ->where() and ->orWhere()
$query = Tblpr::find()
->where(['PRID' => 2])
->orWhere(['attribute' => 'value'])
->all();

WhereNotExists Laravel Eloquent

Little bit of trouble with the eloquent framework for laravel.
I need to replicate a query like this :
SELECT *
FROM RepairJob
WHERE NOT EXISTS (SELECT repair_job_id
FROM DismissedRequest
WHERE RepairJob.id = DismissedRequest.repair_job_id);
Right now I have
$repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')->where('active', '=', 'Y')->whereNotExists('id', [DismissedRequest::all('repair_job_id')])->get();
Anyone an idea? I need to get all the repairjobs where there is no record for in the dismissed requests table
I get this error when using the query above
Argument 1 passed to Illuminate\Database\Query\Builder::whereNotExists() must be an instance of Closure, string given
Try this:
$repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
->where('active', '=', 'Y')
->whereNotExists(function($query)
{
$query->select(DB::raw(1))
->from('DismissedRequest')
->whereRaw('RepairJob.id = DismissedRequest.id');
})->get();
Try doesntHave() method. Assuming 'dismissedRequests' as relation name in RepairJob model.
$jobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
->where('active', 'Y')->doesntHave('dismissedRequests')->get();

Laravel Eloquent orWhere Query

Can someone show me how to write this query in Eloquent?
SELECT * FROM `projects` WHERE `id`='17' OR `id`='19'
I am thinking
Project::where('id','=','17')
->orWhere('id','=','19')
->get();
Also my variables (17 and 19) in this case are coming from a multi select box, so basically in an array. Any clues on how to cycle through that and add these where/orWhere clauses dynamically?
Thanks.
You could do in three ways. Assume you've an array in the form
['myselect' => [11, 15, 17, 19], 'otherfield' => 'test', '_token' => 'jahduwlsbw91ihp'] which could be a dump of \Input::all();
Project::where(function ($query) {
foreach(\Input::get('myselect') as $select) {
$query->orWhere('id', '=', $select);
}
})->get();
Project::whereIn('id', \Input::get('myselect'))->get();
$sql = \DB::table('projects');
foreach (\Input::get('myselect') as $select) {
$sql->orWhere('id', '=', $select);
}
$result = $sql->get();
The best approach for this case is using Laravel's equivalent for SQL's IN().
Project::whereIn('id', [17, 19])->get();
Will be the same as:
SELECT * FROM projects WHERE id IN (17, 19)
This approach is nicer and also more efficient - according to the Mysql Manual, if all values are constants, IN sorts the list and then uses a binary search.
In laravel 5 you could do it this way.
$projects = Projects::query();
foreach ($selects as $select) {
$projects->orWhere('id', '=', $select);
}
$result = $projects->get();
This is very useful specially if you have custom methods on your Projects model and you need to query from variable. You cannot pass $selects inside the orWhere method.
public function getSearchProducts($searchInput)
{
$products = Cache::rememberForever('getSearchProductsWithDiscountCalculationproducts', function () {
return DB::table('products_view')->get();
});
$searchProducts = $products->filter(function ($item) use($searchInput) {
return preg_match('/'.$searchInput.'/i', $item->productName) || preg_match('/'.$searchInput.'/i', $item->searchTags) ;
});
$response = ["status" => "Success", "data" => $searchProducts ];
return response(json_encode($response), 200, ["Content-Type" => "application/json"]);
}
use filter functionality for any customize situations.

How can I query raw via Eloquent?

I am trying to do a query in my Laravel app and I want to use a normal structure for my query. This class either does use Eloquent so I need to find something to do a query totally raw.
Might be something like Model::query($query);. Only that doesn't work.
You may try this:
// query can't be select * from table where
Model::select(DB::raw('query'))->get();
An Example:
Model::select(DB::raw('query'))
->whereNull('deleted_at')
->orderBy('id')
->get();
Also, you may use something like this (Using Query Builder):
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
Also, you may try something like this (Using Query Builder):
$users = DB::select('select * from users where id = ?', array(1));
$users = DB::select( DB::raw("select * from users where username = :username"), array('username' => Input::get("username")));
Check more about Raw-Expressions on Laravel website.
You can use hydrate() function to convert your array to the Eloquent models, which Laravel itself internally uses to convert the query results to the models. It's not mentioned in the docs as far as I know.
Below code is equviolent to $userModels = User::where('id', '>', $userId)->get();:
$userData = DB::select('SELECT * FROM users WHERE id > ?', [$userId]);
$userModels = User::hydrate($userData);
hydrate() function is defined in \Illuminate\Database\Eloquent\Builder as:
/**
* Create a collection of models from plain arrays.
*
* #param array $items
* #return \Illuminate\Database\Eloquent\Collection
*/
public function hydrate(array $items) {}
use DB::statement('your raw query here'). Hope this helps.
I don't think you can by default. I've extended Eloquent and added the following method.
/**
* Creates models from the raw results (it does not check the fillable attributes and so on)
* #param array $rawResult
* #return Collection
*/
public static function modelsFromRawResults($rawResult = [])
{
$objects = [];
foreach($rawResult as $result)
{
$object = new static();
$object->setRawAttributes((array)$result, true);
$objects[] = $object;
}
return new Collection($objects);
}
You can then do something like this:
class User extends Elegant { // Elegant is my extension of Eloquent
public static function getWithSuperFancyQuery()
{
$result = DB::raw('super fancy query here, make sure you have the correct columns');
return static::modelsFromRawResults($result);
}
}
Old question, already answered, I know.
However, nobody seems to mention the Expression class.
Granted, this might not fix your problem because your question leaves it ambiguous as to where in the SQL the Raw condition needs to be included (is it in the SELECT statement or in the WHERE statement?). However, this piece of information you might find useful regardless.
Include the following class in your Model file:
use Illuminate\Database\Query\Expression;
Then inside the Model class define a new variable
protected $select_cols = [
'id', 'name', 'foo', 'bar',
Expression ('(select count(1) from sub_table where sub_table.x = top_table.x) as my_raw_col'), 'blah'
]
And add a scope:
public function scopeMyFind ($builder, $id) {
return parent::find ($id, $this->select_cols);
}
Then from your controller or logic-file, you simply call:
$rec = MyModel::myFind(1);
dd ($rec->id, $rec->blah, $rec->my_raw_col);
Happy days.
(Works in Laravel framework 5.5)
use Eloquent Model related to the query you're working on.
and do something like this:
$contactus = ContactUS::select('*')
->whereRaw('id IN (SELECT min(id) FROM users GROUP BY email)')
->orderByDesc('created_at')
->get();
You could shorten your result handling by writing
$objects = new Collection(array_map(function($entry) {
return (new static())->setRawAttributes((array) $entry, true);
}, $result));
if you want to select info it is DB::select(Statement goes here) just remember that some queries wont work unless you go to Config/Database.php and set connections = mysql make sure 'strict' = false
Just know that it can cause some security concerns
if ever you might also need this.
orderByRaw() function for your order by.
Like
WodSection::orderBy('score_type')
->orderByRaw('FIELD(score_type,"score_type") DESC')
->get();

Categories