Création d'une table simple pour débuter :
CREATE TABLE articles ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, body TEXT, published BOOLEAN DEFAULT FALSE, created DATETIME, modified DATETIME ) CHARSET=utf8;
Générer le modèle, le contrôleur et les vues :
cd /var/www/mon_site sudo bin/cake bake all articles sudo chown -R www-data:www-data ./src
(Je fais un chown simplement pour que les fichiers soient éditables avec Codiad qui est un éditeur web installé sur le même serveur.)
<?php namespace App\Model\Entity; use Cake\ORM\Entity; class Article extends Entity { protected $_accessible = [ 'user_id' => true, 'title' => true, 'slug' => true, 'body' => true, 'published' => true, 'created' => true, 'modified' => true, 'user' => true ]; }
<?php namespace App\Model\Table; use Cake\ORM\Table; class ArticlesTable extends Table { public function initialize(array $config) { parent::initialize($config); $this->setTable('articles'); $this->setDisplayField('title'); $this->setPrimaryKey('id'); $this->addBehavior('Timestamp'); $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER' ]); } }