I use this for query not in:
$usertypes=Usertype::find()->where(['not in ','user_type_id',['2,3,4']])->all();
Error:
Database Exception – yii\db\Exception
Undefined offset: 1
Failed to prepare SQL: SELECT * FROM usertype WHERE user_type_id NOT IN :qp0
also tried the array format as ['2','3','4'] but not works?What is the issue?
Try This :
$usertypes=Usertype::find()
->where(['not in','user_type_id',[2,3,4]])
->all();
OR :
$usertypes=Usertype::find()
->where(['NOT',['user_type_id'=>[2,3,4]]])
->all();
Refer : http://www.bsourcecode.com/yiiframework2/select-query-model/#In-Condition
Maybe remove space character from 'not in '?
$usertypes=Usertype::find()->where(['not in', 'user_type_id', ['2,3,4']])->all();
you can use "not" word or "<>"
$usertypes=Usertype::find()->where(['not',['user_type_id'=>['2,3,4']]])->all();
or this
$usertypes=Usertype::find()->where(['<>',['user_type_id'=>['2,3,4']]])->all();
Try to use ->andFilterWhere instead of where ->where
Try this:
$usertypes = Usertype::find()
->andFilterWhere(['NOT IN','user_type_id',[2,3,4]])
->all();
Related
I have this rather simple query:
Category::whereRaw('hierarchy LIKE ?', [$old_hierarchy . '>%'])
->update([
'hierarchy' => DB::raw("REPLACE(hierarchy, ?, ?)", [$old_hierarchy, $new_hierarchy])
]);
And it keeps giving me this error:
SQLSTATE[HY093]: Invalid parameter number (SQL: update `categories` set `hierarchy` = REPLACE(hierarchy, 2020-05-25 22:30:55, first_param>%), `categories`.`updated_at` = ? where hierarchy LIKE ?)
Seems like no parameters are passed to the DB::raw. Any idea what's going on ?
Ok, this thing is so weird I have zero explanation for it, but here's the solution:
1) First of all you need to explicitly update updated_at yourself, because otherwise Laravel would try and do it itself and make a big mess out of everything.
2) For some unknown reason the initial setup simply failed to assign the variables to the placeholder ?: DB::raw("REPLACE(hierarchy, ?, ?)", [$old_hierarchy, $new_hierarchy])
3) I had to do it using setBindings, but on top of that couldn't assign all of them through this method because they would have gotten messed up once again, so only did it for the DB::raw statements.
Category::setBindings([$old_hierarchy, $new_hierarchy, now()])
->whereRaw("hierarchy LIKE ?", [$old_hierarchy . '>%'])
->update([
'hierarchy' => DB::raw("REPLACE(hierarchy, ?, ?)"),
'updated_at' => DB::raw("?")
]);
If anyone has any explanation of this whole crazy thing, please let me know
I want to perform following WHERE condition in yii2 ActiveDataProvider
Expected Where Condition :
$query="WHERE VoterName Like '%s%' AND (contactno != '' OR whatsapp_no!= '')";
My current where Condition:
$query->andFilterWhere(['like', 'VoterName', $this->VoterName]);
$query->orWhere(['<>', 'contactno', ''])->orWhere(['<>', 'whatsapp_no', '']);
I want to fetch only those records who have contactno or whatsapp_no.
When you need to set multi condition, you must use andWhere, for example for your question:
$query->andFilterWhere(['like', 'VoterName', $this->VoterName]);
$query->andWhere(['OR',['<>', 'contactno', ''],['<>', 'whatsapp_no', '']]);
Reference: \yii\db\QueryInterface::where()
Can the following query meet your needs?
$query->andWhere(['like', 'VoterName', $this->VoterName])
->andWhere(['or',
['!=', 'contactno', ''],
['!=', 'whatsapp_no', '']
]);
I want to add like condition with % wildcard on the one side, like:
where name like 'value%'
My code:
Table::find()->filterWhere(['like', 'name' , $_GET['q'].'%' ])
->all();
But query result is:
where name like '%value\%%'
You need set the fourth operand to false in order to use custom where like conditions:
Table::find()->where(['like', 'name', $_GET['q'] . '%', false]);
From the docs:
Sometimes, you may want to add the percentage characters to the
matching value by yourself, you may supply a third operand false to do
so. For example, ['like', 'name', '%tester', false] will generate name LIKE '%tester'.
You can use:
Table::find()->where(new \yii\db\Expression('name LIKE :term', [':term' => $_GET['q'] . '%']));
or
Table::find()->where(['like', 'name', $_GET['q'] . '%', false]);
or
$likeCondition = new \yii\db\conditions\LikeCondition('name', 'LIKE', $_GET['q'] . '%');
$likeCondition->setEscapingReplacements(false);
Table::find()->where($likeCondition);
More info at https://www.yiiframework.com/doc/api/2.0/yii-db-conditions-likecondition
I have this query :
$query->select('if(bud.posa=2,1,0) AS proprietaire,if(bud.du=1 and bud.gog=1,1,0) as pa')->from('bud');
if I execute it is giving synthax error; I try to echo the sql that is being generated I found that it is not being generated(quoting problem) well, here is what is generated:
select if(bud.posa=2, `1`, `0)` AS `proprietaire`, if(bud.du=1 and bud.gog=1, `1`, `0)` AS `pa` FROM `bud`
0) is what is causing the error ,I don't see how to solve it,Any solution?
Rewrite the query like this:
$query->select([
'proprietaire' => 'if(bud.posa=2,1,0)',
'pa' => 'if(bud.posa=2,1,0)',
]);
http://www.yiiframework.com/doc-2.0/yii-db-query.html#select()-detail
Try so:
use yii\db\Expression;
.......
.......
$expresion = new Expresion('if(bud.posa=2,1,0) AS proprietaire,if(bud.du=1 and bud.gog=1,1,0) as pa');
$query->select($expresion)->from('bud');
It seems that Active Record (Codeigniter) doesn't accept the WEEK parameter and I don't understand why?!
Whern i remove '3', my query works properly!
$this->db->select('WEEK(insere_le,3) AS semaine, COUNT(*) AS nombre')
->from($this->table_name)
->where(array(
'type_anomalie' => "Dérogation",
'YEAR(insere_le)' => $year
))
->group_by('WEEK(insere_le,3)')
->get()
->result_array();
This query, shows the following string when I execute it:
SELECT WEEK(insere_le, `3)` AS semaine, COUNT(*) AS nombre FROM (`aero_anomalie_montage`) WHERE `type_anomalie` = 'Dérogation' AND YEAR(insere_le) = '2014' GROUP BY WEEK(insere_le, `3)`
As you can see it adds an apostrophe before the number 3 and after the parenthesis )
It looks a little confused by your syntax. Why not put them in two different selects (if that's possible...)?
I don't know if this will help, but I found this, it will prevent it from adding the backticks altogether.
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names with backticks. This is useful if you need a compound select
statement.
http://ellislab.com/codeigniter/user-guide/database/active_record.html
hmm, if you look at active rec class (system/core/database/DB_active_rec.php) you will find this in function select() :
if (is_string($select))
{
$select = explode(',', $select);
}
so what you're select is doing is actually exploding your string into an array like this:
array(
WEEK(insere_le,
3) AS semaine,
COUNT(*) AS nombre
);
and then processing this.
This looks like an oversight from the developers of the active record class, and probably won't be solved by not escaping the values...
On the other hand, it actually checks its a string prior to the above.. so could try this:
$this->db->select(array('WEEK(insere_le,3) AS semaine', 'COUNT(*) AS nombre'))...
and the same with group_by(array('WEEK(insere_le,3)'))...
so end result would be:
$this->db->select(array('WEEK(insere_le,3) AS semaine', 'COUNT(*) AS nombre'))
->from($this->table_name)
->where(array(
'type_anomalie' => "Dérogation",
'YEAR(insere_le)' => $year
))
->group_by(array('WEEK(insere_le,3)'))
->get()
->result_array();
you tried this?:
$this->db->select("WEEK(insere_le,3) AS semaine, COUNT(*) AS nombre", FALSE);