php - How to fetch data from table with foreign key in zf2? -
please me!i need fetch field table 'restaurant' - restaurant_longitude.how in zf2 using tablegateway or zend\db\select when have 1 table('user_favorite') foreign key table 'restaurant'-restaurant_id.for example:return field expression
select restaurant_longitude restaurant,user_favorite user_favorite.restaurant_id = restaurant.restaurant_id my sql statement
select restaurant_longitude,restaurant_latitude user_favorite join restaurant on user_favorite.restaurant_id = restaurant.restaurant_id 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 ; in 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; } in controller
//username route array in module.config.php $display_name = (string)$this->params()->fromroute('username',''); $favorite = $this->getfavoritetable()->getfavoritebyusername($display_name); $view->setvariable('favorite',$favorite); but not works fine returns nulls( favorite.phml
<? foreach($favorite $fav): {?> <? echo var_dump($fav->restaurant_longitude); ?> <? }endforeach; ?>
edit: okay, apparently don't need make join users table, go display name. how it's done.
public function getfavoritebyusername($username){ $resultset = $this->tablegateway->select(function (select $select) use($username){ // here's how can pass params select statement! $select->where('display_name = ' . $username) ->join('restaurant', 'user_favorite.restaurant_id = restaurant.restaurant_id',array('restaurant_longitude', 'restaurant_latitude'); }); return $resultset; }
Comments
Post a Comment