config/bootstrap.php
Plugin::load('Migrations');
$ cake bake migration Create 項目1:タイプ
$ bin/cake bake migration CreateMoview title:string content:text stars:integer created:datetime
$ bin/cake bake migration CreateMovie title:string content:text stars:integer created:datetime
Creating file /home/vagrant/dev/cake/mycakeapp/config/Migrations/20220715040907_CreateMoview.php
Wrote `/home/vagrant/dev/cake/mycakeapp/config/Migrations/20220715040907_CreateMoview.php`
$ bin/cake migrations migrate
/config/Migrations/20220715040907_CreateMoview.php
$ bin/cake bake all movies
http://192.168.56.10:8080/movies
src/Model/Entity/Movie.php
protected $_accessible = [
'title' => true,
'content' => true,
'stars' => true,
'created' => true,
];
src/Model/Table/MoviesTable.php
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('title')
->maxLength('title', 255)
->requirePresence('title', 'create')
->notEmptyString('title');
$validator
->scalar('content')
->requirePresence('content', 'create')
->notEmptyString('content');
$validator
->integer('stars')
->requirePresence('stars', 'create')
->notEmptyString('stars');
return $validator;
}
src/Controller/MoviesController.php
public function index()
{
$movies = $this->paginate($this->Movies);
$this->set(compact('movies'));
}
/**
* View method
*
* @param string|null $id Movie id.
* @return \Cake\Http\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$movie = $this->Movies->get($id, [
'contain' => [],
]);
$this->set('movie', $movie);
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$movie = $this->Movies->newEntity();
if ($this->request->is('post')) {
$movie = $this->Movies->patchEntity($movie, $this->request->getData());
if ($this->Movies->save($movie)) {
$this->Flash->success(__('The movie has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The movie could not be saved. Please, try again.'));
}
$this->set(compact('movie'));
}
/**
* Edit method
*
* @param string|null $id Movie id.
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$movie = $this->Movies->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$movie = $this->Movies->patchEntity($movie, $this->request->getData());
if ($this->Movies->save($movie)) {
$this->Flash->success(__('The movie has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The movie could not be saved. Please, try again.'));
}
$this->set(compact('movie'));
}
/**
* Delete method
*
* @param string|null $id Movie id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$movie = $this->Movies->get($id);
if ($this->Movies->delete($movie)) {
$this->Flash->success(__('The movie has been deleted.'));
} else {
$this->Flash->error(__('The movie could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}