Skip to content

Instantly share code, notes, and snippets.

View cakephp-tutorial's full-sized avatar

CakePHP Italia Tutorial cakephp-tutorial

View GitHub Profile
<?php
# /app/Model/Article.php
class Article extends AppModel
{
public $hasMany = 'Comment';
}
<?php
# /app/Model/Comment.php
class Comment extends AppModel
{
public $belongsTo = 'Article';
}
CREATE TABLE `articles` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `comments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`comment` varchar(100) NOT NULL,
`article_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_COMMENTS_ARTICLE_ID` (`article_id`),
CONSTRAINT `FK_COMMENTS_ARTICLE_ID` FOREIGN KEY (`article_id`) REFERENCES `articles` (`id`) ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?php
# /app/Model/Comment.php
class Comment extends AppModel
{
public $belongsTo = array(
'Article' => array(
'foreign_key' => 'post_id'
)
);
}
<?php
# /app/Controller/ArticlesController.php
class ArticlesController extends AppController
{
public $scaffold;
}
<?php
# /app/Model/Article.php
class Article extends AppModel
{
public $hasMany = array(
'Comment'
);
public $validate = array(
'title' => array(
'rule' => array('notEmpty'),
# /app/Console/cake
cake bake all
CREATE TABLE `books` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`isbn` varchar( 13 ) NOT NULL ,
`title` varchar( 64 ) NOT NULL ,
`description` text NOT NULL ,
`author_id` int( 11 ) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `authors` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` varchar( 200 ) NOT NULL ,
`surname` varchar( 200 ) NOT NULL ,
`email` varchar( 100 ) NOT NULL ,
PRIMARY KEY (`id`)
);