Skip to content

Instantly share code, notes, and snippets.

@markstory
Created October 10, 2014 00:51
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 markstory/f7d37eebeab7474f1636 to your computer and use it in GitHub Desktop.
Save markstory/f7d37eebeab7474f1636 to your computer and use it in GitHub Desktop.
<?php
App::uses('AppModel', 'Model');
/**
* Article Model
*
* @property Comment $Comment
*/
class Article extends AppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Comment' => array(
'className' => 'Management.Comment',
'foreignKey' => 'article_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
<?php
App::uses('AppController', 'Controller');
/**
* Articles Controller
*
*/
class ArticlesController extends AppController {
public function index() {
$this->autoRender = false;
$res = $this->Article->find('all', [
'contain' => ['Comment']
]);
debug($res);
debug($this->Article->getDataSource()->getLog());
}
}
<?php
/**
* In this file you set up your database connection details.
*
* @package cake
* @subpackage cake.config
*/
class DATABASE_CONFIG {
// MySQL
var $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'cake_debug',
'prefix' => 'pre_',
'encoding' => 'utf8'
);
}
CREATE TABLE `pre_articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`body` text,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `pre_comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`article_id` int(11) NOT NULL,
`body` text,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment