Skip to content

Instantly share code, notes, and snippets.

@kozo002
Created June 26, 2012 16:04
Show Gist options
  • Select an option

  • Save kozo002/2996704 to your computer and use it in GitHub Desktop.

Select an option

Save kozo002/2996704 to your computer and use it in GitHub Desktop.
Three table association on Cakephp
<?php
class Post extends AppModel {
var $hasOne = 'PostUser';
}
<?php
class PostsController extends AppController {
var $uses = array('Post', 'User', 'PostUser');
function index() {
$this->data = $this->PostUser->find();
pr($this->data);
}
}
/* pr dump
Array
(
[PostUser] => Array
(
[id] => 1
[post_id] => 1
[user_id] => 1
[hoge] => 1 - 1
)
[Post] => Array
(
[id] => 1
[title] => test title
)
[User] => Array
(
[id] => 1
[name] => test name
)
)
*/
<?php
class PostUser extends AppModel {
var $belongsTo = array('Post', 'User');
var $virtualFields = array(
'hoge' => "CONCAT('Post.id', ' - ', 'User.id')" // or "CONCAT(Post.title, ' - ', User.name)"
);
}
<?php
class User extends AppModel {
var $hasOne = 'PostUser';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment