yii2. query with like condition - yii

In my yii2 application i need something like this:
SELECT * FROM customers WHERE first_name||' '||last_name ILIKE '%Nick%' ORDER BY "id";
This query gets customers with concatinated first_name and last_name like 'Nick'.
In my yii1 application I can use CDBCriteria next way:
$criteria = new CDbCriteria;
$criteria->addSearchCondition(
"first_name||' '||last_name",
'Nick',
true,
'and',
'ILIKE'
);
$criteria->order = 't.id asc';
And this works fine.
In yii2 I tried:
$a = \common\models\Customer::find()
->filterWhere(['ILIKE', "first_name||' '||last_name", 'Nick'])
->orderBy(['id' => SORT_ASC])
->all();
And got exception column does not exist, and sql was:
SELECT * FROM "customers" WHERE "first_name||' '||last_name" ILIKE '%Nick%' ORDER BY "id"
Using \yii\db\Expression doesnt change situation.

Let it be for a while:
\common\models\Customer::find()
->where("first_name||' '||last_name ilike '%'||:query||'%'", [':query' => $name])
->orderBy(['id' => SORT_ASC])
->all()

This is one of many ways to do it:
$customers = \common\models\Customer::find()
->select('*')
->where(['like','concat(first_name,\' \', last_name)','Nick N'])
->orderBy(['id' => SORT_ASC])
->all();

Related

How to pass subquery as table name to another query in yii2

I have a query which I am trying to convert into yii2 syntax. Below is the query
SELECT project_id, user_ref_id FROM
(
SELECT `project_id`, `user_ref_id`
FROM `projectsList`
WHERE user_type_ref_id = 1) AS a WHERE user_ref_id = '.yii::$app->user->id;
I am trying to convert it into yii2 format like
$subQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from('projectsList')->where(['user_type_ref_id' => 1]);
$uQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from($subQuery)->where(['user_ref_id ' => yii::$app->user->id])->all();
It is giving an error like
trim() expects parameter 1 to be string, object given
How to I pass subquery as table name to another query
Not tested, but generally this is how it goes. You need to pass the subQuery as a table. So change ->from($subQuery) in the second query to ->from(['subQuery' => $subQuery])
$subQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from('projectsList')->where(['user_type_ref_id' => 1]);
Then
$query = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from(['subQuery' => $subQuery])->where(['subQuery.user_ref_id ' => yii::$app->user->id])->all();

Codeigniter active record where array

I am using codeigniter and active record. I am selecting my data over WHERE with array. Any like this. But how can I insert tags '>' and '<'? It is possible?
$whereQuery['service.service_end_date'] = $start;
Thank you for replies.
This might be what you want:
Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
You can include your own operators using this method as well:
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);
Source:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
http://ellislab.com/codeigniter/user-guide/database/active_record.html
$whereQuery['service.service_end_date >'] = $start;
$whereQuery['service.service_end_date <'] = $start;
You can pass > < <> in CI where function
$this->db->where('field_name <', "Condition_value");
From Codeigniter page :
You can include an operator in the first parameter in order to control the comparison:
$this->db->where('name !=', $name);
$this->db->where('id <', $id);
// Produces: WHERE name != 'Joe' AND id < 45

how to get data from inner join using cdbcriteria in yii?

This is my SQL. I want to create a CDbCriteria in Yii.
select us.user_id,u.clientid from user_session us
inner join user u on u.id=us.user_id
where us.auth_token='authtoken0000000001'
I tried this, but gives the wrong result. I also defined a relation related in my user_session model for this.
$criteria = new CDbCriteria;
$criteria->select = "user_id,user.clientid as client_id";
$criteria->condition='auth_token="'.$token.'"';
$clientIdarray = UserSession::model()->with('related')->find($criteria);
$dataprovider=New CActiveDataProvider('Bla',
array(
'criteria'=>array(
'order'=>'id ASC',
'with'=>array(
'user',
),
'joinType'=>'INNER JOIN',
'condition'=>'user.auth_token = '.$token,
)
));
Just an example how to do dataprovider init.
$criteria->with do what you need.
This topic will help. http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Custom Query with Cdbcriteria

How can I execute this query with CDbCriteria and CActiveDataProvider?
'SELECT * FROM tbl_post where title LIKE %'.$title.'% ORDER BY title LIKE '.$title.' DESC , title LIKE '.$title.'% DESC'
update:
finaly I wrote this:
$criteria = new CDbCriteria;
$criteria->addCondition('title LIKE :title');
$criteria->params = array(':title'=>'%'.$title.'%',':t1'=>$title,':t2'=>$title.'%');;
$criteria->order='title LIKE :t1 DESC , title LIKE :t2 DESC';
but I got error:
CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: SELECT COUNT(*) FROM `tbl_post` `t` WHERE title LIKE :title
Try using compare clause, not sure is it you want with the 'order by x like' :
<?php
$criteria = new CDbCriteria;
$criteria->compare('title', $title, true);
$criteria->order = 'title DESC';
$dp = new CActiveDataprovider('posts', array(
'criteria' => $criteria
));
This can be achieved like this:
$dataProvider=new CActiveDataProvider('tbl_post', array(
'criteria'=>array(
'condition'=>'title LIKE % '.$title.' %',
'order'=>'title DESC',
),
));
$dataProvider->getData();
also refer : Help
Thanks.

Zend: How to use SQL query with 'like' keyword?

I am using zend framework. I am using following query in zend and it is working for me perfectly.
$table = $this->getDbTable();
$select = $table->select();
$select->where('name = ?', 'UserName');
$rows = $table->fetchAll($select);
Now I want to create another query in zend with 'like' keyword. In simple SQL it is like that.
SELECT * FROM Users WHERE name LIKE 'U%'
Now how to convert my zend code for above query?
Try:
$table = $this->getDbTable();
$select = $table->select();
$select->where('name LIKE ?', 'UserName%');
$rows = $table->fetchAll($select);
or if UserName is a variable:
$table = $this->getDbTable();
$select = $table->select();
$select->where('name LIKE ?', $userName.'%');
$rows = $table->fetchAll($select);
$user = new Application_Model_DbTable_User();
// User List
$uname=$_POST['uname'];
$query = $user
->select()
->where('firstname LIKE ?', $uname.'%')
->ORwhere('lastname LIKE ?', $_POST['lname'].'%')
->ORwhere('emailid LIKE ?', $_POST['email'].'%');
$userlist = $user->fetchAll($query);