Zend Framework 2:如何从连接表中获取列?

时间:2015-07-19 03:35:38

标签: php mysql join zend-framework2

我正在使用Zend Framework 2.4和TableGateway,需要执行以下查询:

SELECT products.title, student.firstname, orders.order_id, 
  orders.profile_id, orders.status
FROM student 
INNER JOIN 
 (products 
   INNER JOIN orders ON products.product_id = orders.product_id) 
     ON student.student_id = orders.student_id 
WHERE (((orders.profile_id)='.$id.'));

以下是我正在使用的内容:

$select = $this->tableGateway->getSql()->select();
$select->columns(array('*'))
        ->join('products', 'products.product_id = orders.product_id', array('title'))
        ->join('student', 'student.student_id = orders.student_id', array('first_name'))
        ->where(array('orders.profile_id' => $id));
$items = $this->tableGateway->selectWith($select);
return $items;

问题是$items不包含列titlefirst_name。它只包含orders表中的列。 要使$items包含连接表中的列,我需要做什么?

1 个答案:

答案 0 :(得分:0)

尝试使用关联数组

$select = $this->tableGateway->getSql()->select();
$select->columns(array('*'))
    ->join('products', 'products.product_id = orders.product_id', array('title' => 'product_title'))
    ->join('student', 'student.student_id = orders.student_id', array('first_name' => 'student_first_name'))
    ->where(array('orders.profile_id' => $id));
$items = $this->tableGateway->selectWith($select);
return $items;