请帮助我!我需要从'restaurant'餐厅获取字段 - restaurant_longitude.How如何在zf2中使用tableGateway或Zend \ Db \选择当我有一个表('user_favorite')与外键到表'餐厅'-restaurant_id。例如:带表达式的返回字段
SELECT restaurant_longitude from restaurant,user_favorite where user_favorite.restaurant_id = restaurant.restaurant_id
我的sql语句
select restaurant_longitude,restaurant_latitude from user_favorite join restaurant on user_favorite.restaurant_id = restaurant.restaurant_id where user_favorite.display_name = 'admin'
user_favorite.sql;
CREATE TABLE IF NOT EXISTS `user_favorite` (
`user_favorite_id` int(11) NOT NULL AUTO_INCREMENT,
`display_name` varchar(50) CHARACTER SET utf8 NOT NULL,
`restaurant_id` int(11) DEFAULT NULL,
`attraction_id` int(11) DEFAULT NULL,
`user_favorite_timestamp` date NOT NULL,
PRIMARY KEY (`user_favorite_id`),
KEY `display_name` (`display_name`),
KEY `restaurant_id` (`restaurant_id`),
KEY `attraction_id` (`attraction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=42 ;
ALTER TABLE `user_favorite`
ADD CONSTRAINT `user_favorite_ibfk_1` FOREIGN KEY (`display_name`) REFERENCES `user` (`display_name`),
ADD CONSTRAINT `user_favorite_ibfk_3` FOREIGN KEY (`attraction_id`) REFERENCES `attraction` (`attraction_id`),
ADD CONSTRAINT `user_favorite_ibfk_4` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurant` (`restaurant_id`);
restaurant.sql
CREATE TABLE IF NOT EXISTS `restaurant` (
`restaurant_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Уникальный идентификатор ресторана',
`restaurant_id_name` varchar(100) NOT NULL COMMENT 'Идентификатор ресторана для маршутизации',
`restaurant_name` varchar(100) NOT NULL COMMENT 'Имя ресторана',
`restaurant_mode` varchar(100) NOT NULL COMMENT 'Тип ресторана',
`restaurant_description` varchar(1000) NOT NULL COMMENT 'Описание ресторана',
`restaurant_thumbnail` varchar(100) NOT NULL COMMENT 'Главный рисунок ресторана',
`restaurant_image_1` varchar(100) NOT NULL,
`restaurant_image_2` varchar(100) NOT NULL,
`restaurant_image_3` varchar(100) NOT NULL,
`restaurant_features` varchar(200) NOT NULL COMMENT 'Возможности ресторана',
`restaurant_dj` varchar(20) NOT NULL,
`restaurant_wifi` varchar(20) NOT NULL,
`restaurant_karaoke` int(5) NOT NULL,
`restaurant_kalian` int(5) NOT NULL,
`restaurant_chill_out` int(5) NOT NULL,
`restaurant_sigarette_room` int(5) NOT NULL,
`restaurant_live_music` int(5) NOT NULL,
`restaurant_veranda` int(5) NOT NULL,
PRIMARY KEY (`restaurant_id`),
KEY `restaurant_id_name` (`restaurant_id_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Таблица для описания ресторанов в городе Ялта' AUTO_INCREMENT=95 ;
UserFavoriteTable.php 中的
public function getFavoriteByUsername($display_name){
$resultSet = $this->tableGateway->select(function (Select $select) use($display_name){
$select->where('user_favorite.display_name = ' . $display_name)
->join('restaurant', 'user_favorite.restaurant_id = restaurant.restaurant_id',array('restaurant_longitude'));
});
return $resultSet;
}
控制器中的
//username from route array in module.config.php
$display_name = (string)$this->params()->fromRoute('username','');
$favorite = $this->getFavoriteTable()->getFavoriteByUsername($display_name);
$view->setVariable('favorite',$favorite);
但它不起作用只返回nulls( favorite.phml
<? foreach($favorite as $fav): {?>
<? echo var_dump($fav->restaurant_longitude); ?>
<? }endforeach; ?>
答案 0 :(得分:1)
编辑:好的,显然你不需要使用users表进行连接,只需按显示名称即可。所以这就是它的完成方式。
public function getFavoriteByUsername($username){
$resultSet = $this->tableGateway->select(function (Select $select) use($username){ // Here's how you can pass params to a select statement!
$select->where('display_name = ' . $username)
->join('restaurant', 'user_favorite.restaurant_id = restaurant.restaurant_id',array('restaurant_longitude', 'restaurant_latitude');
});
return $resultSet;
}