Zend Framework Between Query - zend-framework

How can I write the following query in Zend Framework?
select * from hosted_plans where hp_id=15 and curdate() between discount_start_date and discount_end_date
In the query above the discount_start_date and discount_end_date columns are date fields in the table
Thanks in advance.

$query = $database->select ()
->from ('hosted_plans')
->where ('hp_id = ?', 15)
->where ('curdate() between discount_start_date and discount_end_date');
$database being a Zend_Db_Adapter_Abstract descendant.

One possible form with Zend 2:
$select = $this->tableGateway->getSql()->select();
$select->where(array('hp_id' => 15, new \Zend\Db\Sql\Predicate\Expression('curdate() BETWEEN discount_start_date AND discount_end_date')));
$resultSet = $this->tableGateway->selectWith($select);

Alternatively:-
$query = $database->select ()
->from('hosted_plans')
->where('hp_id = ?', 15)
->where('curdate() >= discount_start_date')
->where('curdate() <= discount_end_date');
Would also work.

Related

TYPO3 extbase database query

I have an extbase database query like below.
$query = $this->createQuery();
$result = $query->statement("Select * FROM table1 WHERE hidden = 0 AND deleted = 0 AND (".$PublicationYears.") AND logo != '' ORDER BY uid ASC LIMIT 0, ".$iLimit." ")->execute();
return $result;
$PublicationYears = "ttra = '12' or ttra = '13' or ttra = '14'";
I converted this query as follows,
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(FALSE);
$query->matching( $query->logicalAnd(
$query->equals('deleted', 0),
$query->equals('hidden', 0)
));
$query->matching($query->logicalAnd($PublicationYears));
$query->matching($query->logicalNot(
$query->equals('logo', '')
));
$query->setOrderings(array('uid' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));
$query->setLimit((integer)$iLimit);
$Result = $query->execute();
return $Result;
But the resultant query does not contain the query part related to,
$query->matching($query->logicalAnd($PublicationYears));
I think there are also other mistakes in the above query.
Please help me to create correct query.
Thanks in advance.
first of all you dont need this
$query->matching( $query->logicalAnd(
$query->equals('deleted', 0),
$query->equals('hidden', 0)
));
disable fields are included in query from default according to setting of table in ext_tables.php
multiple matching will not work
cause your third matching
$query->matching($query->logicalNot(
$query->equals('logo', '')
));
is overwriting the matching() used before it
logicalAnd on string ? it won't work either
in your case you just need to put everything in logicalAnd do it like this
$constraints = array();
$subConstraints = array();
$subConstraints[] = $query->equals('ttra', 12);
$subConstraints[] = $query->equals('ttra', 13);
$subConstraints[] = $query->equals('ttra', 14);
$constraints[] = $query->logicalOr($subConstraints);
$constraints[] = $query->logicalNot(
$query->equals('logo', '')
));
$query->matching($query->logicalAnd($constraints));
For your PublicationYears values you have to make or query in matching like below:
I think it might work only for PublicationYears variable.
$query->matching($query->logicalAnd(
$query->logicalOr(
$query->equals('ttra', 12)
),
$query->logicalOr(
$query->equals('ttra', 13)
),
$query->logicalOr(
$query->equals('ttra', 14)
),
));

zend framework subquery

I am using zend framework 1.12. I have following query to run.
"SELECT name,(select count(*) from org_quote_template_items where org_quote_template_items.quote_template_id = org_quote_templates.`id` ) as total_line_item FROM `org_quote_templates`"
In my model file , I created it like this. following is my model file.
class default_Model_DbTable_QuoteTemplates extends Zend_Db_Table_Abstract
{
/**
* Name of the original db table
*
* #var string
*/
protected $_name = 'org_quote_templates';
public function getAllTemplate($where){
$select = $this->select();
$subquery = " (SELECT COUNT(*) FROM org_quote_template_items WHERE org_quote_template_items.quote_template_id = org_quote_templates.`id` )";
$select->from(array($this), array('org_quote_templates.*','total_line_items' => new Zend_Db_Expr($subquery)));
$select = $select->where('organization_id = ?',$where['org_id']);
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(
Zend_Registry::get('config')->paginator->general);
pr($adapter);
exit;
}
}
I am getting following error when I run the code.
" exception 'Zend_Db_Table_Select_Exception' with message 'Select query cannot join with another table' "
please let me know what should I do ?
There is an error in your request. You should have:
$select = $this->select ();
$subquery = "(SELECT COUNT(*) FROM dtempls WHERE order_id = orders.id)";
$select->from ($this, array (
'id',
'total_line_items' => new Zend_Db_Expr ($subquery)
));
I think you have to use setIntegrityCheck(false) for accomplishing that. Check this link
You can try this way in zend
$this->select()
->setIntegrityCheck(false)
->from(array('oqt' => 'org_quote_templates'),array('total_line_item'))
->joinLeft(array('oqti' => 'org_quote_template_items'), 'oqti.quote_template_id = oqt.id', array(count(*) as count))

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

convert sql query to Zend DB query

I have tow table :
article,sub_categories
how to convert this query to zend Db query join
$sql="
SELECT article.*, sub_categories.name_arabic AS sub_category_name_arabic, sub_categories.name_english AS sub_category_name_english, article.sub_category_id AS sub_categories_id FROM article
LEFT JOIN sub_categories ON article.sub_category_id = sub_categories.id
WHERE article.active = 1
";
Please need help
Thanks with regard
This should do the trick:
$db->select()
->from('article', array('*', 'sub_categories_id' => 'sub_category_id')
->joinLeft(
'sub_categories',
'article.sub_category_id = sub_categories.id',
array('sub_category_name_arabic' => 'name_arabic', 'sub_category_name_english' => 'name_english'))
->where('article.active = 1');

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