how to use joins in Zend_Paginator_Adapter_DbSelect() - zend-framework

how can i replace below mysql query to ZF Zend_Paginator_Adapter_DbSelect()
query
$sql = "SELECT ps.phone_service_id,ps.phone_service_name,ps.phone_service_Duration,ps.phone_service_type,us.user_preferences_value,ps.user_id FROM phone_service ps,user_preferences us
WHERE us.phone_service_id = ps.phone_service_id
AND us.user_preferences_name = 'is_user_package_active'
AND ps.user_id =".$user_id;
i write this one but error occur could you please replace my query to equailent Zend_Paginator_Adapter_DbSelect()
$select = $DB->select()
->from(array('ps' => 'phone_service'
'us' => 'user_preferences'),
array('ps.phone_service_id', 'ps.phone_service_name','ps.phone_service_Duration','ps.phone_service_type','us.user_preferences_value')),
->where('us.phone_service_id = ?', 'ps.phone_service_id')
->where( 'us.user_preferences_name = ?', 'is_user_package_active')
->where( 'us.user_id = ?', $user_id)
;

I guess you want something like this:
$sel = $db->select();
$sel->from(array('p' => 'phone_service'))
->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id')
->where('u.user_preferences_name = ?', 'is_user_package_active')
->where('p.user_id = ?', $user_id);

Related

Zend\Db\Sql\Select multiple where (combination between AND and OR)

I'm a little stuck at a query with multiple where. I want to 'translate' the query bellow in my Zend\Db\Sql\Select query:
SELECT `users`.*, `users_metas`.*
FROM `users` INNER JOIN `users_metas` ON `users`.`ID` = `users_metas`.`parent_id`
WHERE `role` = 'admin'
AND (
`users`.`username` like '%q_word%'
OR
(`users_metas`.`meta_key` = 'name'
AND
`users_metas`.`meta_value` like '%q_word%')
)
ORDER BY `date_added` ASC
I tried something like this:
$select = new Select();
$select->where(array('role' => 'admin'));
$select->join('users_metas', 'users.ID = users_metas.parent_id');
but I'm stuck to this where:
AND (
`users`.`username` like '%q_word%'
OR
(`users_metas`.`meta_key` = 'name'
AND
`users_metas`.`meta_value` like '%q_word%')
)
Thank you!
$where = new Where();
$where
->equalTo('role', 'admin')
->nest()
->like('users.username', '%q_word%')
->or
->like('users_metas.meta_key', 'name')
->or
->like('users_metas.meta_value', '%q_word%')
->unnest();
$select = new Select();
$select
->from('users')
->join('users_metas', 'users.ID = users_metas.parent_id')
->where($where)
->order('date_added');

zend1 query converted to plain mysqli?

I have this zend framework 1 query which looks like this:
function getNumberOfNewPlayer() {
global $db, $start_date, $end_date;
$data = $db->select()
->from(array('c' => 'casino_israel.Network'),array('count(*) as total'))
->where('c.Date > ?', $start_date)
->where('c.Date < ?', $end_date)
->query()->fetch();
return $data['total'];
}
How would it look line in plain mysqli?Thanks!
$data = $db->select()
->from(array('c' => 'casino_israel.Network'),array('count(*) as total'))
->where('c.Date > ?', $start_date)
->where('c.Date < ?', $end_date);
echo $data->__toString();

Zend_Db_Select : multiple from clause

Hy,
I have some problem vith Zend_Db_Select.
I have 2 variable : category and city. These 2 variables may have a value or they may be unset.
So I verify:
$status = '`p`.status = 1';
if($catID){
$catSQL = "`p`.parent = {$catID}";
}else{
$catSQL = '1=1';
}
if($city){
$citySQL = "`pm`.`meta_key` = 'oras' and `pm`.`meta_value` = {$city}";
$citySelect = array('pm' => 'postsmeta');
$condCity = "`p`.`ID` = `pm`.`parent_id`";
}else{
$citySQL = '1=1';
$citySelect = NULL;
$condCity = '1=1';
}
Now here is my query:
$select = $db->select()
->from( array('p' => 'posts'))
->from($citySelect)
->where($status)
->where($catSQL)
->where($condCity)
->where($citySQL)
;
The problem is that if city is empty I have something like
$select = $db->select()
->from( array('p' => 'posts'))
->from('')
->where(1=1)
->where(1=1)
->where(1=1)
->where(1=1)
;
The questions is how can I remove from('') from my query if city is empty.
Thank you!
Simply,
$select = $db->select()
->from( array('p' => 'posts'));
if($someConditionIsTrue) {
$select->join($table, $condition);
}
$select->where('field_value = ?', 'value1');
if($someConditionIsTrue) {
$select->where('another_field = ?', 'value 2');
}
Hope it helps.
Please use this syntax $select->where('another_field = ?', 'value 2'); for proper escaping values to prevent SQL injections.

How to Use Multiple Conditions In An Update Statement With Zend_Db And QuoteInto

Using Zend Framework, is there a way to pass multiple conditions to an update statement using the quoteInto method? I've found some references to this problem but I'm looking for a supported way without having to extend Zend_Db or without concatenation.
$db = $this->getAdapter();
$data = array('profile_value' => $form['profile_value']);
$where = $db->quoteInto('user_id = ?', $form['id'])
. $db->quoteInto(' AND profile_key = ?', $key);
$this->update($data, $where);
References
http://blog.motane.lu/2009/05/21/zend_db-quoteinto-with-multiple-arguments/
http://codeaid.net/php/multiple-parameters-in-zend_db::quoteinto%28%29
You can use an array type for your $where argument. The elements will be combined with the AND operator:
$where = array();
$where[] = $this->getAdapter()->quoteInto('user_id = ?', $form['id']);
$where[] = $this->getAdapter()->quoteInto('key = ?', $key);
$this->update(array('value' => $form['value']), $where);
Since 1.8 you can use:
$where = array(
'name = ?' => $name,
'surname = ?' => $surname
);
$db->update($data, $where);
Just refreshing the above answer
$data = array('value' => $form['value']);
$where = array();
$where[] = $this->getAdapter()->quoteInto('user_id = ?', $form['id']);
$where[] = $this->getAdapter()->quoteInto('key = ?', $key);
$this->update($data, $where);

Zend Framework: How to do a DB select with multiple params?

I'm just wondering what the syntax is to do a db select in Zend Framework where two values are true. Example: I want to find if a user is already a member of a group:
$userId = 1;
$groupId = 2;
$db = Zend_Db_Table::getDefaultAdapter();
$select = new Zend_Db_Select($db);
$select->from('group_members')
->where('user_id = ?', $userId); //Right here. What do I do about group_id?
$result = $select->query();
$resultSet = $result->fetchAll();
You can use multiple where clauses which will be ANDed together by default:
$select->from('group_members')
->where('user_id = ?', $userId)
->where('group_id = ?', $groupId);
Just In case someone wants to add an OR condition to a select with multiple params
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('price < ?', $minimumPrice)
->orWhere('price > ?', $maximumPrice);
For more view the Zend Select manual Docs: zend.db.select