zend framework - Zend_DB subselect / subquery how to? -


i have raw sql statement trying execute through zend_db.

$sql = 'select relocationaction.id, relocationaction.vehicle, relocationaction.start,         relocationaction.end, relocationaction.return ' .             'from relocationaction,               (select vehicle, max(end) maxend               relocationaction               group vehicle) co2             co2.vehicle = relocationaction.vehicle             and(relocationaction.monitor = 1)             , (relocationaction.return null)             , (start <= ?)             , relocationaction.end = co2.maxend'; 

i have found possible solution using type of notation, rendered totally different , wrong sql statement joins , strange table names.

$tbl    = $this->getdbtable(); $select = $tbl->select()->setintegritycheck(false);  $subselect = $select->from('relocationaction', array('vehicle', 'maxend' => 'max(relocationaction.end)'))                     ->group('vehicle'); $subselectstring = '(' . $subselect->__tostring() . ')';  $select ->from(                 array('relocationaction'), array('id', 'date' => 'start', 'enddate' => 'end', 'return'),                 array('co2' => $subselectstring)             )         ->joinleft('exhibitvehicle', 'exhibitvehicle.id = relocationaction.vehicle', array())         ->where('co2.vehicle = relocationaction.vehicle')         ->where('relocationaction.monitor = 1')         ->where('relocationaction.return null')         ->where('start <= ?', $start->get('yyyy-mm-dd'))         ->where('relocationaction.end = co2.maxend'); 

can please give me hint?

thanks jesse

update

this result of second expression (total rubbish)

select `relocationaction`.`vehicle`,      max(relocationaction.end) `maxend`,      `relocationaction_2`.`id`,      `relocationaction_2`.`start` `date`,      `relocationaction_2`.`end` `enddate`,      `relocationaction_2`.`return`  `relocationaction` inner join `(     select ``relocationaction``.``vehicle``,     max(relocationaction.end) ``maxend`` ``relocationaction`` group ``vehicle``)`.`relocationaction` `relocationaction_2` left join `exhibitvehicle` on exhibitvehicle.id = relocationaction.vehicle  (col2.vehicle = relocationaction.vehicle)  , (relocationaction.monitor = 1)  , (relocationaction.return null)  , (start <= '2013-05-08')  , (relocationaction.end = col2.maxend)  group `vehicle` 

if use string in from(), zend_db_select consider table name escapes it.

the solution wrap subselect zend_db_expr.

$tbl    = $this->getdbtable(); $select = $tbl->select()->setintegritycheck(false);  $subselect = $select->from('relocationaction', array('vehicle', 'maxend' => 'max(relocationaction.end)'))                 ->group('vehicle'); $subselectstring = '(' . $subselect->__tostring() . ')';  $select ->from(             array('relocationaction'), array('id', 'date' => 'start', 'enddate' => 'end', 'return'),             array('co2' => new zend_db_expr($subselectstring))         )     ->joinleft('exhibitvehicle', 'exhibitvehicle.id = relocationaction.vehicle',     array())     ->where('co2.vehicle = relocationaction.vehicle')     ->where('relocationaction.monitor = 1')     ->where('relocationaction.return null')     ->where('start <= ?', $start->get('yyyy-mm-dd'))     ->where('relocationaction.end = co2.maxend'); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -