0

请帮帮我!我需要从表'restaurant'-restaurant_longitude中获取字段。当我有一个表('user_favorite')和表'restaurant'-restaurant_id的外键时,如何使用tableGateway或Zend\Db\Select在zf2中执行此操作.例如:用表达式返回字段

 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`);

餐厅.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);

但它不能正常工作只返回空值(喜爱的.phml

 <? foreach($favorite as $fav): {?>
                            <? echo var_dump($fav->restaurant_longitude); ?>
                        <? }endforeach; ?>
4

1 回答 1

1

编辑:好的,显然您不需要与用户表进行连接,您只需按显示名称即可。所以这就是它的完成方式。

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;
}
于 2013-08-01T07:53:37.337 回答