Skip to content

Instantly share code, notes, and snippets.

@residential-map-machine-user
Created May 25, 2017 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save residential-map-machine-user/0cfc9b8d38382813b72b0a28497c8982 to your computer and use it in GitHub Desktop.
Save residential-map-machine-user/0cfc9b8d38382813b72b0a28497c8982 to your computer and use it in GitHub Desktop.
<?php
namespace App\Model\Table\Restaurant;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\I18n\Time;
/**
* Lseats Model
*
* @property \Cake\ORM\Association\BelongsToMany $Lcombinations
* @property \Cake\ORM\Association\BelongsToMany $Ltimers
*
* @method \App\Model\Entity\Lseat get($primaryKey, $options = [])
* @method \App\Model\Entity\Lseat newEntity($data = null, array $options = [])
* @method \App\Model\Entity\Lseat[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Lseat|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Lseat patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Lseat[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\Lseat findOrCreate($search, callable $callback = null, $options = [])
*/
class SeatsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('seats');
$this->setEntityClass('App\Model\Entity\Restaurant\Seat');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->belongsToMany('Combinations', [
'foreignKey' => 'seat_id',
'targetForeignKey' => 'combination_id',
'joinTable' => 'combinations_seats'
]);
$this->belongsToMany('Timers', [
'foreignKey' => 'seat_id',
'targetForeignKey' => 'timer_id',
'joinTable' => 'seats_timers'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('name', 'create')
->notEmpty('name');
$validator
->integer('capacity')
->requirePresence('capacity', 'create')
->notEmpty('capacity');
return $validator;
}
/**
* 指定時間以降のその日の予約情報を渡す
*
* @param datetime|null $time
* @return array
*/
public function reserveSeat($time = null, $shop_id = null)
{
if($time == null || $shop_id == null) return null;
$before = clone $time;
$before->modify('-2 hours');
$endtime = clone $time;
$endtime->hour = 24;
$endtime->minute = 0;
//予約されている席を取得
$reserveSeats = $this->find()
->contain(['Timers' => function ($q) use ($before,$endtime) {
return $q
->order([ 'Timers.start_time' => 'ASC' ])
->where([
'Timers.start_time >=' => $before,
'Timers.start_time <=' => $endtime
]);
}])
->where(['LSeats.shop_id' => $shop_id ])
->all();
return $reserveSeats;
}
/**
* うまっていないseatを返すメソッド
*
* @param datetime|null $time
* @return array
*/
public function inquiryVacancy($time = null, $shop_id = null)
{
if($time == null || $shop_id == null) return null;
$before = clone $time;
$before->modify('-2 hours');
$after = clone $time;
$after->modify('+2 hours');
$reserveIdArray = array();
$vacancyIdArray = array();
//指定された店の予約されている席を取得
$reserveSeats = $this->find()
->matching('Timers', function ($q) use ($before,$after) {
return $q
->where([
'Timers.start_time >=' => $before,
'Timers.start_time <' => $after
]);
})
->where(['Seats.shop_id' => $shop_id ])
->select(['Seats.id', 'Seats.name'])
->all();
foreach($reserveSeats as $resSeat):
array_push($reserveIdArray, $resSeat["id"]);
endforeach;
//全席を取得
$allSeats = $this->find()
->select(['Seats.id', 'Seats.name'])
->where(['Seats.shop_id' => $shop_id ])
->all();
//全席から予約されている席を取り除く
foreach($allSeats as $allSeat):
if(!in_array($allSeat["id"],$reserveIdArray)){
array_push($vacancyIdArray, $allSeat["id"]);
}
endforeach;
return $vacancyIdArray;
}
/*
*
*/
public function fetchReservationSeatById($id = null){
if($id == null) return null;
//現在時刻の作成
$time = new Time("2017-05-22 11:00");
$before = clone $time;
$before->modify('-2 hours');
$endtime = clone $time;
$endtime->hour = 24;
$endtime->minute = 0;
//予約されている席を取得
$reserveSeat = $this->find()
->contain(['Ltimers' => function ($q) use ($before,$endtime) {
return $q
->order([ 'start_time' => 'ASC' ])
->where([
'start_time >=' => $before,
'start_time <=' => $endtime,
]);
}])->where(['id' => $id]);
return $reserveSeat;
//関連テーブルと時間を入力すればおk
}
/*
*
*/
public function createReservationForSeat($id = null, $start_time, $end_time){
$seat = $this->get($id);
$timer = $this->Ltimers->newEntity();
$timer->end_time = $end_time;
$timer->start_time = $start_time;
$seat->ltimer = $timer;
debug($timer);
return $this->save($timer);
}
}
<?php
namespace App\Model\Table\Restaurant;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\I18n\Time;
/**
* Lseats Model
*
* @property \Cake\ORM\Association\BelongsToMany $Lcombinations
* @property \Cake\ORM\Association\BelongsToMany $Ltimers
*
* @method \App\Model\Entity\Lseat get($primaryKey, $options = [])
* @method \App\Model\Entity\Lseat newEntity($data = null, array $options = [])
* @method \App\Model\Entity\Lseat[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Lseat|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Lseat patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Lseat[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\Lseat findOrCreate($search, callable $callback = null, $options = [])
*/
class SeatsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('seats');
$this->setEntityClass('App\Model\Entity\Restaurant\Seat');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->belongsToMany('Combinations', [
'foreignKey' => 'seat_id',
'targetForeignKey' => 'combination_id',
'joinTable' => 'combinations_seats'
]);
$this->belongsToMany('Timers', [
'foreignKey' => 'seat_id',
'targetForeignKey' => 'timer_id',
'joinTable' => 'seats_timers'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('name', 'create')
->notEmpty('name');
$validator
->integer('capacity')
->requirePresence('capacity', 'create')
->notEmpty('capacity');
return $validator;
}
/**
* 指定時間以降のその日の予約情報を渡す
*
* @param datetime|null $time
* @return array
*/
public function reserveSeat($time = null, $shop_id = null)
{
if($time == null || $shop_id == null) return null;
$before = clone $time;
$before->modify('-2 hours');
$endtime = clone $time;
$endtime->hour = 24;
$endtime->minute = 0;
//予約されている席を取得
$reserveSeats = $this->find()
->contain(['Timers' => function ($q) use ($before,$endtime) {
return $q
->order([ 'Timers.start_time' => 'ASC' ])
->where([
'Timers.start_time >=' => $before,
'Timers.start_time <=' => $endtime
]);
}])
->where(['LSeats.shop_id' => $shop_id ])
->all();
return $reserveSeats;
}
/**
* うまっていないseatを返すメソッド
*
* @param datetime|null $time
* @return array
*/
public function inquiryVacancy($time = null, $shop_id = null)
{
if($time == null || $shop_id == null) return null;
$before = clone $time;
$before->modify('-2 hours');
$after = clone $time;
$after->modify('+2 hours');
$reserveIdArray = array();
$vacancyIdArray = array();
//指定された店の予約されている席を取得
$reserveSeats = $this->find()
->matching('Timers', function ($q) use ($before,$after) {
return $q
->where([
'Timers.start_time >=' => $before,
'Timers.start_time <' => $after
]);
})
->where(['Seats.shop_id' => $shop_id ])
->select(['Seats.id', 'Seats.name'])
->all();
foreach($reserveSeats as $resSeat):
array_push($reserveIdArray, $resSeat["id"]);
endforeach;
//全席を取得
$allSeats = $this->find()
->select(['Seats.id', 'Seats.name'])
->where(['Seats.shop_id' => $shop_id ])
->all();
//全席から予約されている席を取り除く
foreach($allSeats as $allSeat):
if(!in_array($allSeat["id"],$reserveIdArray)){
array_push($vacancyIdArray, $allSeat["id"]);
}
endforeach;
return $vacancyIdArray;
}
/*
*
*/
public function fetchReservationSeatById($id = null){
if($id == null) return null;
//現在時刻の作成
$time = new Time("2017-05-22 11:00");
$before = clone $time;
$before->modify('-2 hours');
$endtime = clone $time;
$endtime->hour = 24;
$endtime->minute = 0;
//予約されている席を取得
$reserveSeat = $this->find()
->contain(['Ltimers' => function ($q) use ($before,$endtime) {
return $q
->order([ 'start_time' => 'ASC' ])
->where([
'start_time >=' => $before,
'start_time <=' => $endtime,
]);
}])->where(['id' => $id]);
return $reserveSeat;
//関連テーブルと時間を入力すればおk
}
/*
*
*/
public function createReservationForSeat($id = null, $start_time, $end_time){
$seat = $this->get($id);
$timer = $this->Ltimers->newEntity();
$timer->end_time = $end_time;
$timer->start_time = $start_time;
$seat->ltimer = $timer;
debug($timer);
return $this->save($timer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment