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

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

Related

yii2. query with like condition

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

construct select query from two tables in zend

I'm new to zend 1.12 and I want to construct the following query, please advice:
SELECT tbl_user.first_name, tbl_user.last_name, tbl_user.email, tbl_user_group.group_id
FROM `tbl_user`, `tbl_user_group`
WHERE `organization_id` = 5
AND tbl_user.user_id = tbl_user_group.user_id
AND tbl_user_group.group_id = 11
Try this (not tested):
$select = $db->select()
->from(array('tu' => 'tbl_user'),
array('first_name', 'last_name', 'email'))
->join(array('tug' => 'tbl_user_group'),
'tu.user_id = tug.user_id',
array('group_id'))
->where('tu.organization_id = ?', 5)
->where('tug.group_id = ?', 11);

want to add one more condition in findAll

the query is
$model = Recipe::model()->findAll(array("condition" => "title like '$request%'"));
i want to add one more condition , status = Approved
is it good to add second condition in this findAll or use findallbyattributes and how to add second condition in this findAll
You could just add AND status = "Approved", but I think the best approach would be to use a CDbCriteria, like this:
$criteria = new CDbCriteria();
$criteria->addSearchCondition($request);
$criteria->compare('status', 'Approved');
$model = Recipe::model()->findAll($criteria);
What do you think?
This should simply work -
$model = Recipe::model()->findAll(array("condition" => "title like '$request%' and status = 'Approved'"));

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 Framework SQL update query

How would I write this SQL the Zend Framework way?
UPDATE register
SET balance = (balance + 10)
WHERE added_date > 1259944184 ;
I can't find any examples of this on Zend's website or the web.
Do I need to use "Zend_Db_Expr"?
This worked for me:
$data = array(balance => new Zend_DB_Expr('balance + 10'));
$db->update('register ', $data, 'added_date > 1259944184');
acording to zend framwork documentation
use this
$data = array(
'balance' => 'balance + 10'
);
$n = $db->update('register ', $data, 'added_date > 1259944184');
Try this... make sure your model is ready.
$table = new register();
This is the model class
balance=balance+10;
$data = array(
'balance' => 'balance'
);
$where = $table->getAdapter()->quoteInto('added_date > '. 1259944184 );
You can use $where[] for multiple conditions
$table->update($data, $where);
For more details follow link
I used this to modify element order to top.
Code as follow from the table class extending Zend_Db_Table_Abstract:
$data = array('i_order' => new Zend_DB_Expr('i_order + 1'));
return $this->getAdapter()->update($this->_name, $data, "i_id != {$oCurrent->i_id} AND i_order < {$i_order}");