Skip to content

Instantly share code, notes, and snippets.

@hgnelson83
Last active July 7, 2025 18:11
Show Gist options
  • Select an option

  • Save hgnelson83/30b47abfb62e3b52d329f0ada9fcabd0 to your computer and use it in GitHub Desktop.

Select an option

Save hgnelson83/30b47abfb62e3b52d329f0ada9fcabd0 to your computer and use it in GitHub Desktop.
Cake 3 Hotel App
<!-- \src\Template\Cell\Hotelquote\display.ctp -->
<div class="row column">
<?= $this->Form->create(null, ['url' => ['controller' => 'Hotelquotes', 'action' => 'add']]); ?>
<fieldset>
<?php
echo $this->Form->input('title');
echo $this->Form->input('email');
echo $this->Form->input('country');
echo $this->Form->input('arrival');
echo $this->Form->input('departure');
?>
<div class="small-12">
<?php
echo $this->Form->input('adult');
echo $this->Form->input('child');
echo $this->Form->input('infant');
?>
</div>
<?php echo $this->Form->input('comments');
?>
</fieldset>
<?php
$options = array(
$hotels['Hotels']['id'] => $hotels['Hotels']['title']
);
echo $this->Form->input(
'hotel_id',
[
'type' => 'select',
'multiple' => false,
'options' => $options,
#'default' => 'Hotels', 'id',
#'options' => $hotels,
'empty' => true
]
);
?>
<?= $this->Form->button(__('Submit'), array('class' => 'expanded button')); ?>
<?= $this->Form->end() ?>
</div>
<!-- src\View\Cell\HotelquoteCell.php -->
<?php
namespace App\View\Cell;
use Cake\View\Cell;
class HotelquoteCell extends Cell
{
protected $_validCellOptions = [];
public function display()
{
$this->loadModel('Hotels');
$hotels = $this->Hotels->find('list')
->hydrate(false)
->toArray();
$this->set('hotels', $hotels);
$this->data['Hotel']['hotelquote_id'] = $id;
}
}
<?php
namespace App\Controller;
use App\Controller\AppController;
class HotelquotesController extends AppController
{
public function add()
{
$hotelquote = $this->Hotelquotes->newEntity();
if ($this->request->is('post')) {
$hotelquote = $this->Hotelquotes->patchEntity($hotelquote, $this->request->data);
if ($this->Hotelquotes->save($hotelquote)) {
$this->Flash->success(__('The hotelquote has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The hotelquote could not be saved. Please, try again.'));
}
}
$hotels = $this->Hotelquotes->Hotels->find('list', ['limit' => 200]);
$this->set(compact('hotelquote', 'hotels'));
$this->set('_serialize', ['hotelquote']);
}
}
<?php
namespace App\Model\Table;
use App\Model\Entity\Hotelquote;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class HotelquotesTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('hotelquotes');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Hotels', [
'foreignKey' => 'hotel_id',
'joinType' => 'INNER'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->allowEmpty('title');
$validator
->email('email')
->requirePresence('email', 'create')
->notEmpty('email');
$validator
->requirePresence('country', 'create')
->notEmpty('country');
$validator
->requirePresence('arrival', 'create')
->notEmpty('arrival');
$validator
->requirePresence('departure', 'create')
->notEmpty('departure');
$validator
->requirePresence('adult', 'create')
->notEmpty('adult');
$validator
->requirePresence('child', 'create')
->notEmpty('child');
$validator
->requirePresence('infant', 'create')
->notEmpty('infant');
$validator
->requirePresence('comments', 'create')
->notEmpty('comments');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
$rules->add($rules->existsIn(['hotel_id'], 'Hotels'));
return $rules;
}
}
<!-- src\Template\Hotels\view.ctp -->
<!-- View file, where we would also load the Hotelquote form add -->
<section class="row">
<h1><?= h($hotel->title) ?></h1>
<div class="large-3 columns" style="background-color: #e6e6e6;">
<?= $this->cell('Hotelquote') ?> <!-- LOAD THE CELL FORM -->
</div>
</div>
<p>Other info about hotels...</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment