Zend_Db - union - zend-framework

I have code:
$balanceModelCredit = new Model_BalanceCredit();
$balanceModelDebt = new Model_BalanceDebt();
$selectQueryDebt = $balanceModelDebt->select()
->from('balance_debt', array('date',
'amount',
'description',
new Zend_Db_Expr('"koszta"')
));
$selectQueryDebt->where('balance_id=?', $balance_id);
$selectQueryCredit = $balanceModelCredit->select()
->from('balance_credit', array('date',
'amount',
'description',
new Zend_Db_Expr('"przychod"')
));
$selectQueryCredit->where('balance_id=?', $balance_id);
How can I do UNION statement?

You only need to do :
$selectQueryCredit->where('balance_id = ?', $balance_id);
$selectQueryCredit->union(array($selectQueryDebt));
Hope it helps.

Related

Symfony get query from form with some empty text areas

I'd like to know if there is anyway to get a query result from the text areas that have been filled while some have not.
Example:
$form = $this->get('form.factory')->create(new UsersType(), new Users())
->add('firstname', 'text', array('required' => false, 'label' => 'First name '))
->add('lastname', 'text', array('required' => false, 'label' => 'Last name '))
;
$idm = $em->getRepository('MyBundle:Users')->findBy(
array('number' => $form->get('number')->getData(),
'firstname' => $form->get('firstname')->getData(),
'lastname' => $form->get('lastname')->getData()
));
I would like that it ignores the texts which have not been filled and execute a query only with the filled texts.
Thank you!
$lookups = array('number', 'firstname', 'lastname');
$queryArray = array();
foreach($lookups as $lookup){
$fieldData = $form->get($lookup)->getData();
if(!empty($fieldData)){
$queryArray[$lookup] = $fieldData;
}
}
if(sizeof($queryArray) > 0){
$idm = $em->getRepository('MyBundle:Users')->findBy($queryArray);
}

Question mark(?) is not working with Zend's where clause

Previously our zend 1.1 application was working with PHP 5.3 and Apache 2.2 versions and now we have upgraded our PHP to version 5.4 and Apache to 2.4
After this upgrade Zend's WHERE clause is not working with question mark (?) even it is not showing any error or warning. Example is given below:
$query = $db->select()
->from(array('users' => 'users'),
array('id' => 'id', 'email', 'fname'))
->where('email = ?', $email);
However it is working with without ? mark.
What is missing can anyone suggest because it is very critical for us.
Thanks
Like #TimFountain noted, turn on php errors.
Code should be:
$query = $db->select()
->from('users', array('id', 'email', 'fname'))
->where('email = ?', $email);
Or
$query = $db->select()
->from('users', array('id', 'email', 'fname'))
->where('users.email = ?', $email);
Or
$query = $db->select()
->from('users')
->columns(array('id', 'email', 'fname'))
->where('email = ?', $email);
Or
$query = $db->select()
->from(array('u' => 'users'), array('id', 'email', 'fname'))
->where('u.email = ?', $email);
Or :)
$query = $db->select()
->from(array('u' => 'users'))
->columns(array('id', 'email', 'fname'))
->where('u.email = ?', $email);
Resources
Zend Framework Manual

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

Zend relationship - get the parent row

I know there are some examples in the web, but doesn't work in my case.
I have a Category table with subcategories. One table 'category' that has id_father that's the id of the Parent Category.
My configuration is the following:
Application_Model_DbTable_Category:
protected $_referenceMap = array(
'Application_Model_DbTable_Category' => array(
'columns' => 'id_father',
'refColumns' => 'id',
'refTableClass' => 'Application_Model_DbTable_Category'
));
CategoryMapper (I don't write the top code, anyway I retrieve a Category that has a parent in the db)
$row = $result->current();
echo $row->name;
$father = 'Non trovato';
$father = $row->findParentRow('Application_Model_DbTable_Category');
print_r($father);
This doesn't print anything... Is something wrong on my code? Thank you
you need to create instance of class Application_Model_DbTable_Category
$row = $result->current();
echo $row->name;
$father = 'Non trovato';
$father = $row->findParentRow(new Application_Model_DbTable_Category());
print_r($father);

Zend_Auth multiple credentials?

Login Form:
$authAdapter = Zend_Registry::get('authAdapter');
$authAdapter
->setIdentity($formData['email'])
->setCredential($password)
->setCredential(1);
Bootstrap:
protected function _initAuth(){
$this->bootstrap('db');
$this->bootstrap('session');
$db = $this->getPluginResource('db')->getDbAdapter();
$auth = Zend_Auth::getInstance();
$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'User', 'email', 'password', 'enabled');
Zend_Registry::set('authAdapter', $authAdapter);
return $authAdapter;
}
Obviously since adding 'enabled' its stopped working, if I remove:
->setCredential(1);
and 'enabled' from there:
($db, 'User', 'email', 'password', 'enabled');
it works just fine...
I would like to only enable users who have an enabled account to login though.
EDIT:
$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'User', 'email', 'password', '(?) AND `enabled` = 1');
works :)
You need to modify your adapter like so:
$authAdapter = new Zend_Auth_Adapter_DbTable(
$db,
'user',
'email',
'password',
'MD5(?) AND `enabled` = "true"');
please try:
$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'User', 'email', 'password', 'AND enabled > 1');
and remove:
->setCredential(1);
You can set additional conditions in the auth adapter i.e.
$select = $authAdapter->getDbSelect();
$select->where("isActive = ?","Active");