Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 how to rename name of field with join

Because my join includes a field named 'id' as well, I need to rename this field name during my sql so it won't override my id field name from the first selected tabel.

My query look likes as follow;

$select = new \Zend\Db\Sql\Select();
$select->from('websites');
$select->join(array('s' => 'websites_statistics'), 's.website_id = websites.id');
$select->where(array('websites.website' => $website));
$select->order('s.timestamp DESC')->limit(1);

$rowset = $this->tableGateway->selectWith($select);
$row = $rowset->current();

return $row;

So, 's' 'id' field should be renamed to something like 'stat_id'.

Thanks in advance!

Nick

like image 894
directory Avatar asked Jan 07 '13 11:01

directory


Video Answer


1 Answers

$select = new \Zend\Db\Sql\Select();
$select->from('websites');
       ->join(array('s' => 'websites_statistics'), 's.website_id = websites.id',
           array('stat_id' => 's.id')); // <-- here is the alias
       ->where(array('websites.website' => $website));
       ->order('s.timestamp DESC')
       ->limit(1);
like image 79
David Weinraub Avatar answered Sep 30 '22 18:09

David Weinraub