Created
June 26, 2012 16:04
-
-
Save kozo002/2996704 to your computer and use it in GitHub Desktop.
Three table association on Cakephp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Post extends AppModel { | |
| var $hasOne = 'PostUser'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 | |
| ) | |
| ) | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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)" | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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