How to set the condition is null in Yii2 model search? - yii2

I want to set the condition which shows all vehicles where the title_recieved is null.
->andFilterWhere(['=', 'tr.title_recieved', null])
->andFilterWhere(['is', 'tr.title_recieved', null])
->andFilterWhere(['is', [ 'tr.title_recieved', null]])
I've tried all the available options, the is null condition works in andWhere, but not in andFilterWhere.

Use andWhere on query liek this.
->andWhere(['tr.title_recieved' => null]);

Try With This :
->andFilterWhere('tr.title_recieved is NULL');

**As per yii2 doc
andFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands.
The new condition and the existing one will be joined using the 'AND' operator.
This method is similar to andWhere(). The main difference is that this method will remove empty query operands. As a result, this method is best suited for building query conditions based on filter values entered by users.
From doc http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#andFilterWhere()-detail **

It can be done like this
$query->andFilterWhere(['IS', 'tr.title_recieved', new \yii\db\Expression('NULL')]);

Related

how to search by letter in column in database (laravel)

I want to find by letter customer's first_name on my 'customers' table.
And the only thing that I get is an empty array.
For instance, I put in the parameter 'q' value 'E' to get customer Elena from my database by I get an only empty array.
I use the following code to get first_name :
$search = Input::get('q');
if($search)
{
$customers = Customer::all()->where('full_name', 'LIKE', "%{$search}%");
return ($customers);
}
Can someone help me?
Your query don't work because you are calling the all() method before the where(). That's actually not wrong, but it have different behavior.
When you call all(), it actually does the SQL query. After that, any chained methods are being called into a Eloquent Collection class, and it also have a where method, but that's simpler since it runs on PHO instead of running on SQL.
Since the collection's where() method doesn't support LIKE operator, it's probably searching for a value that is exactly %E%.
Hope it can help you understanding why your query doesn't work as expected.
Try this
$customers = Customer::where('full_name', 'LIKE', "%{$search}%")->get();
Laravel Eloquent

Laravel Model - Want to check where (or) orWhere in DB::raw

This is my model function now its working correctly, but i want to check where (or) orwhere. I Already try that but cant get the apt answer
public static function getPlacementCountByStatus(){
$countStatus = DB::table('placements')->where('status','1')->select(DB::raw('count(joborderid) as total, joborderid'))->groupBy('joborderid')->get();
return $countStatus;
}
I want to check something like this
->where('statusid','=','3')->orWhere('statusid','=','4')->orWhere('stageid','=','4')->orWhere('stageid','=','8');
// i want to check this in my $countStatus something like and or condition in my db::raw query
Use where() closure:
$countStatus = DB::table('placements')->where(function($q){
$q->where('statusid','=','3')->orWhere('statusid','=','4')->orWhere('stageid','=','4')->orWhere('stageid','=','8');
})
->select(DB::raw('count(placementid) as total,statusid'))
->groupBy('statusid')->get();
return $countStatus;
From the docs
Sometimes you may need to create more advanced where clauses such as
"where exists" clauses or nested parameter groupings.
In your case, you need to use where closure as you are trying to combine AND,OR operation, means you are trying to group your parameters in a block. Here closure mainly does what a parenthesis does in a sql query.

how to replace multiple where clauses with signle associative array having same column and different conditions in ci

$this->db->where('column','value1');
$this->db->where('column','value12');
$this->db->where('column','value3');
I want to have something like
$where_array = array('column'=>value1,'column'=>value2,'column'=>value3);
$this->db->where($where_array);
Is that possible as we can not have same index name in associative array
Update:
Each condition differs from one another as below:
$this->db->where('column<','value1');
$this->db->where('column>','value12');
$this->db->where('column<=','value3');
Hope you are doing in condition. then try below code.
$values= array(value1,value2,value3);
$this->db->where_in('column', $values);
For between operation
https://ellislab.com/forums/viewthread/102635/
you can use this like
$this->db->where(array('column'=>value1,'column'=>value2,'column'=>value3));
Since your conditions differ from each other you cannot use where_in() or single where() clause, You can chain multiple where clauses to shorten the code as below
$this->db->where('column<','value1')->where('column>','value12')->where('column<=','value3');
However you do not need the first condition ->where('column<','value1') because third condition ->where('column<=','value3') is sufficient, so the final code would look like
$this->db->where('column>','value12')->where('column<=','value3');

Creating an OR statement using existing conditions hash

I am working on a problem where I need to add an OR clause to a set of existing conditions. The current conditions are built in a hash in a method and at the end, they are used in the where clause. Here is a simplified example:
...
conds.merge!({:users => {:archived => false}})
Model.where(conds)
I am trying to add an OR clause to the current set of conditions so it would be something like '(conditions) OR new_condition'. I'd like to add the OR statement without converting each addition to the conds hash into a string. That would be my last option. I was hoping someone has done something like this before (without using Arel). I seem to recall in Rails 2 there was a way to parse a conditions hash using a method from the model (something like Model.some_method(conds) would produce the where clause string. Maybe that would be a good option to just add the OR clause on to that string. Any ideas are appreciated. Thank you for your help!
I found a way to do what I needed. Instead of changing all of the conditions that I am building, I am parsing the conditions to SQL using sanitize_sql_for_conditions. This is a private method in ActiveRecord, so I had to put a method on the model to allow me to access it. Here is my model method:
def self.convert_conditions_hash_to_sql(conditions)
self.sanitize_sql_for_conditions(conditions)
end
So, once I convert my conditions to text, I can add my OR clause (along with the appropriate parentheses) to the end of the original conditions. So, it would go something like this:
Model.where('(?) OR (model.type = ? AND model.id IN(?))', Model.convert_conditions_hash_to_sql(conds), model_type, model_id_array)

DBIx::Class update only one row

I am using DBIx::Class and I would like to only update one row in my table. Currently this is how I do it:
my $session = my_app->model("DB::Session")->find(1);
$session->update({done_yn=>'y',end_time=>\'NOW()'});
It works, but the problem is that when it does find to find the row, it does this whole query:
SELECT me.id, me.project_id, me.user_id, me.start_time, me.end_time, me.notes, me.done_yn FROM sessions me WHERE ( me.id = ? ): '8'
Which seems a bit much when all I want to do is update a row. Is there anyway to update a row without having to pull the whole row out of the database first? Something like this is what I am looking for:
my_app->model("DB::Session")->update({done_yn=>'y',end_time=>\'NOW()'},{id=>$id});
Where $id is the WHERE id=? part of the query. Does anyone know how to do this? Thanks!
You can run update on a restricted resultset which only matches this single row:
my_app->model("DB::Session")->search_rs({ id=> 1 })->update({done_yn=>'y',end_time=>\'NOW()'});
I suggest you use a DateTime->now object instead of literal SQL for updating the end_time column because it uses the apps servers date and time instead of the database servers and makes your schema more compatible with different RDBMSes.
Do you have a check if the row was found to prevent an error in case it wasn't?
You might want to use update_or_create instead.
You could use the "columns" attribute:
my $session = my_app->model("DB::Session")->find(1, {columns => "id"});