Adding IF clause to zend framework 1 database table select object - php

Here is my sql:
SELECT id,`status`,IF(`status` = 'active', 'deleted','active') AS `status-use`
FROM categories
And here is my code: (ActionResourceModel is a Zend_Db_Table )
$dbTable = $this->getActionResourceModel();
$select = $dbTable->select();
$select->from($dbTable,$this->getQueryColumns());
$select->order($orderClause.' '.$orderSpec);
How do I add to my select object :
IF(`status` = 'active', 'deleted','active') AS `status-use`

Your select should look like:
$dbTable = $this->getActionResourceModel();
$select = $dbTable->select();
$select->from(
$dbTable,
array(
'status-use' => new Zend_Db_Expr("IF(`status` = 'active', 'deleted','active')"
)
);

You can extend from Zend_Db_Table and define a function that create this query. you could see Zend_Db_Table functions like join, from, etc for design your special class

Related

cake php custom query

I have use custom query in cakephp but I dont understand how to run custom join query.
I am using this code
$arrayTemp1 = $this->User->query('SELECT DISTINCT
u.id,u.hunting_association FROM ht_users as u LEFT JOIN
`ht_user_animal_prices` as uap ON uap.user_id=u.id WHERE
uap.animal_type_id='.$this->request->data['User']['animal'].' ');
User is the model for ht_users and UserAnimalPrice is the model for ht_user_animal_prices. How to combine the query?
Please help.
If you want to use custom queries and you want the data of UserAnimalPrice model, you just have to put the fields in the query. Something like:
$arrayTemp1 = $this->User->query('SELECT DISTINCT u.id,u.hunting_association, uap.* FROM ht_users as u LEFT JOIN ht_user_animal_prices as uap ON uap.user_id=u.id WHERE uap.animal_type_id='.$this->request->data['User']['animal'].' ');
If you prefer not to use custom queries:
$fields = array('User.id','User.hunting_association','UserAnimalPrice.*');
$join = array(
array(
'table' => 'ht_user_animal_prices',
'alias' => 'UserAnimalPrice',
'type' => 'LEFT',
'conditions' => array('UserAnimalPrice.user_id = User.id')
)
);
$conditions = array('UserAnimalPrice.animal_type_id' => $this->request->data['User']['animal']);
$group = array('User.id');
arrayTemp1=arrayTemp1->find('all',array('fields'=>$fields,'joins'=>$join,'conditions'=>$conditions,'group'=>$group));
This is Correct Query u used .You can also use it in User Model like
public function getCustomUsersQuery()
{
$arrayTemp1 = $this->query("
SELECT
DISTINCT u.id,u.hunting_association FROM ht_users as u
LEFT JOIN
ht_user_animal_prices as uap ON uap.user_id=u.id
WHERE
uap.animal_type_id='".$this->request->data['User']['animal']."'");
return $arrayTemp1;
}
And call inside Users Controller
$result = $this->getCustomUsersQuery();
Sorry I cannot comment on Rizwan answer. If you received the "Call to a member function..." problem, make sure you have the following code
public $uses = array('User');
this tells that you want to access the user model in that controller. Then you will be allowed to do so.

Complex ORM in DooPHP

I have use DooPhp for a small project. But i have problem with ORM query style.
select * from table_name where table ='aaa' order by table_name_id desc
To ORM style:
$vararray = Doo::db()->find('table_name', array(
'where'=>'table=?','param' => array($this->params['table_name_id']),array('desc' => 'id'));
But more complex query like:
select * from table_name where table ='aaa' and table1 like '%value%' order by table_name_id desc
I can not finish it use ORM style.
In a model extending DooSmartModel
you can create a function like this:
/** #params array of values **/ public functiong getTables($params){
return Doo::db->find(array(
"where" => "table = '$params[0]' and table1 like'%$param[1]'",
"desc" => "table_name_id"
)
)
}
after add a __construct function ( obviously )
function __construct(){
parent::$className = __CLASS__;
}

Zend Select group by Field

I have a query as below:
select status, count(id) as units from myTable group by status
I have a Zend_Db_Table object at my displosal for the table in use, i am trying the below mentioned snippet to create the query above:
$objTable= new Application_Model_DbTable_MyTable();
$select = $objTable -> select(Zend_Db_Table::SELECT_WITH_FROM_PART)
-> setIntegrityCheck(false); // i am also using joins to create the query, but this is not of concern
$select -> columns(array(
'status' => new Zend_Db_Expr('DISTINCT(STATUS)'),
'units' => new Zend_Db_Expr('COUNT(id)')
));
$select -> group('status');
the query created from the above snippet is as follows:
select myTable.*, DISTINCT(STATUS) as status, COUNT(id) as units from myTable group by status
I want to remove the myTable.* from the query generated.
$select = $db ->select()
->distinct()
->from('myTable',array('status', 'COUNT(id) as units'))
->group('status');
Try this instead.
$select = new Zend_Db_Select;
$select->from(
# table
array(
't' => 'myTable'
),
# columns
array(
'status' => 'DISTINCT(STATUS)',
'units' => 'COUNT(id)',
));
->group('status');
The problem is Zend_Db_Select will automatically added table.* to your query if there's no column specified in ->from( clause

Propel filtering based on joined tables columns?

How do I filter based on a joined table's columns in Propel?
Like:
$results = FooQuery::create()->joinBar()->filterByBarSurname('surname');
You have to use use method as described in the doc:
$results = FooQuery::create()
->useBarQuery()
->filterBySurname('surname')
->endUse()
->find();
// example Query generated for a MySQL database
$query = 'SELECT foo.* from foo
INNER JOIN bar ON foo.BAR_ID = bar.ID
WHERE bar.SURNAME = :p1'; // :p1 => 'surname'
If you have to use join(), I don't think you can use filterByXXX method but the old where:
$results = FooQuery::create()
->join('Foo.Bar')
->where('Bar.surname = ?', 'surname')
->find();

Nested Select using Zend Db

I have a query like
select empl.idemp ,(select em.firstnm from tbl_employeemaster em where em.idemp = empl.approvedby) as approvedby from tbl_empleaveapplication empl
join tbl_employeemaster emp on empl.idemp = emp.idemp
join tbl_leavemaster lvm on empl.idleavemaster = lvm.idleavemaster
I need to use zend db to build this type of query
Here is how you can convert your string query into Zend_Db_Select
class Empleaveapplication extends Zend_Db_Table_Abstract
{
protected $_name = 'tbl_empleaveapplication';
}
$table = new Empleaveapplication();
// create sub query
$subSql = $table->select()
->setIntegrityCheck(false)
->from(array('em' => 'tbl_employeemaster'), array('firstnm'))
->where('idemp = empl.approvedby', '');
// main query
$sql = $table->select()
->setIntegrityCheck(false)
->from(array('empl' => 'tbl_empleaveapplication'), array('idemp', 'approvedby' => new Zend_Db_Expr('(' . $subSql . ')')))
->joinInner(array('emp' => 'tbl_employeemaster'), 'empl.idemp = emp.idemp', array())
->joinInner(array('lvm' => 'tbl_leavemaster'), 'empl.idleavemaster = lvm.idleavemaster', array());

Categories