Zend_Db_Select : multiple from clause - zend-framework

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.

Related

Postgres Query Optimization-Update Query

I have below two queries which takes time when there is lot of records in DB like 2000000.The column value in 'Where' clause of both queries are dynamic. Can anyone suggest me to to optimize these in a better way?
UPDATE demo.document_details
SET has_dc = 'Y', l3_validation = 'Y',
validation_remark = CONCAT(validation_remark,
'Deliverychallan also available against this document (Alert)<br>'),
fu_5 = CASE WHEN fu_5 = 'Success'
THEN 'Alert'
ELSE fu_5
END
WHERE document_no = '123'
AND document_date = '2018-09-05'
AND user_gstin = 'gstinid'
AND document_type != 'DELIVERY CHALLAN'
AND has_dc IS NULL;
UPDATE demo.document_details
SET is_exclude = 'Y',
l3_validation = 'Y',
validation_remark = CONCAT(validation_remark,
'Cancelled document (Alert)<br>'),
exclude_reason = 'Cancelled document',
fu_5 = CASE WHEN fu_5 = 'Success'
THEN 'Alert'
ELSE fu_5
END
WHERE id IN (1, 2, 3);

How to set custmer id for an quote in magento?

I am adding product into cart and tried to map customer id,email to that quote
using the below code
$product_id = 123;
$qty = 1;
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$superAttributeArray = array('151' => '3');
$params = array(
'product' => $product_id,
'qty' => $qty,
'super_attribute' => $superAttributeArray
);
$cart->addProduct($product, $params);
$cart->save();
$currenQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
$store = Mage::getSingleton('core/store')->load(Mage::app()->getStore()->getId());
$quote = Mage::getModel('sales/quote')->setStore($store)->load($currenQuoteId);
$quote->setCustomerId('1')->setCustomerEmail('test#gmail.com')->setCustomerFirstname('firstname')->setCustomerLastname('lastname');
$quote->save();
When I try to set customerid,email,fname,lname am getting error as "Mage registry key "controller" already exists".
Can anyone help me in fixing this issue?
Something like this might work
$customerObj = Mage::getModel('customer/customer')->load($customerId);
$quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
$storeId = Mage::app()->getStore()->getId();
$quoteObj->setStore(Mage::getSingleton('core/store')->load($storeId);
$productObj = Mage::getModel('catalog/product')->load($productId);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty('1');
$quoteItem->setStoreId($storeId);
$quoteObj->addItem($quoteItem);
$quoteObj->setStoreId($storeId);
$quoteObj->collectTotals();
$quoteObj->save();

how to use joins in Zend_Paginator_Adapter_DbSelect()

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

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