我有两张桌子,餐馆和餐馆,我需要在一个结果集中返回。对于餐馆餐桌中的每家餐厅,RestaurantHours餐桌上有7条记录。
我正在尝试以下列格式返回数据,以便在我的Knockout ViewModel中使用。
所需的结果格式(注意省略一些字段)
array(
'name' => 'restaurant name here',
'description' => 'restaurant description here',
'hours' => array(
'1' => array('open' => 1, 'day' => 1, 'open_time' => '8:00', 'close_time' => 10:00),
'2' => array('open' => 1, 'day' => 2, 'open_time' => '8:00', 'close_time' => 10:00),
'3' => array('open' => 1, 'day' => 3, 'open_time' => '8:00', 'close_time' => 10:00),
'4' => array('open' => 1, 'day' => 4, 'open_time' => '8:00', 'close_time' => 10:00),
'5' => array('open' => 1, 'day' => 5, 'open_time' => '8:00', 'close_time' => 10:00),
'6' => array('open' => 1, 'day' => 6, 'open_time' => '8:00', 'close_time' => 10:00),
'7' => array('open' => 1, 'day' => 7, 'open_time' => '8:00', 'close_time' => 10:00),
)
);
到目前为止,我已经能够使用下面的选择获得所有餐厅,但我不知道如何以我想要的格式返回餐厅营业时间,所以在客户端,我会有一个集合每周的每一天都有餐厅对象,每个对象都有自己的餐厅营业时间。我知道如何加入表,只是不确定如何使用Zend DB得到结果集。
餐馆
$select = $this->restaurantRepository->select();
$select->setIntegrityCheck(false)
->from('restaurants')
->join('food_types', 'restaurants.food_type = food_types.id', array('foodType' => 'name'))
->order('restaurants.name ASC');
$restaurants = $this->restaurantRepository->getAdapter()->fetchAll($select);
return Zend_Json::encode($restaurants);
餐厅营业时间
// fetch all of the restaurant hours for the specified restaurant
$select = $this->restaurantHoursRepository
->select()
->from('restaurant_hours', array('id', 'restaurant_id', 'open', 'day', 'open_time' => 'DATE_FORMAT(open_time, "%H:%i")', 'close_time' => 'DATE_FORMAT(close_time, "%H:%i")'))
->where('restaurant_id = ' . $restaurant_id);
return Zend_Json::encode($this->restaurantHoursRepository->fetchAll($select));
基本上我需要将这两个查询加在一起。现在我不得不返回所有的餐馆,然后当ViewModel中的餐馆对象被绑定时,我获取了餐厅的营业时间,这意味着很多ajax电话。我真的想把它归结为一个电话,它会加载所有的餐馆。
感谢您的帮助!
答案 0 :(得分:0)
你能试试吗
$select = $this->restaurantRepository->select();
$select->setIntegrityCheck(false)
->from('restaurants')
->join('food_types', 'restaurants.food_type = food_types.id', array('foodType' => 'name'))
->join('restaurant_hours', 'restaurants.id = restaurant_hours.restaurant_id', array('id', 'restaurant_id', 'open', 'day', 'open_time' => 'DATE_FORMAT(open_time, "%H:%i")', 'close_time' => 'DATE_FORMAT(close_time, "%H:%i")'))
->order('restaurants.name ASC');
$restaurantsAndHours = $this->restaurantRepository->getAdapter()->fetchAll($select);
return Zend_Json::encode($restaurantsAndHours);