Home » Php » php – Nested Select using Zend Db

php – Nested Select using Zend Db

Posted by: admin July 12, 2020 Leave a comment


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

How to&Answers:

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()
                ->from(array('em' => 'tbl_employeemaster'), array('firstnm'))
                ->where('idemp = empl.approvedby', '');

// main query
$sql = $table->select()
             ->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());