0

I have these tables:

table: CREATORS

**CREATORS**

obs.:I've also tried that way:

enter image description here

table: POSTS

enter image description here

CreatorModel.php

class CreatorModel extends AppModel {
    public $actsAs = array('Containable');
    public $hasOne = array('Post');
}

PostModel.php

class PostModel extends AppModel {
    public $actsAs = array('Containable');
    public $hasOne = array('Creator');
}

IndexController.php

$this->set('posts', $this->Post->find());

index.ctp

var_dump($posts);

Following the CakeBook session about Associations http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

I should receive that response in view:

Array
(
    [Post] => Array
        (
            [id] => 1
            [creator_id] => 1
            [tags] => 
            [title] => Teste
            [post] => Maromba
            [created] => 2013-04-29 19:14:32
            [modified] => 2013-04-29 19:14:32

        )
    [Creator] => Array
        (
            [creator_id] => 1
            [creator] => Alexandre Moraes
        )
)

But i receive this:

Array
(
    [Post] => Array
        (
            [id] => 1
            [creator_id] => 1
            [tags] => 
            [title] => Teste
            [post] => Maromba
            [created] => 2013-04-29 19:14:32
            [modified] => 2013-04-29 19:14:32

        )
)

So, any ideas what i'm doing wrong?

Alexandre
  • 751
  • 2
  • 11
  • 27

2 Answers2

2

In the db

CREATORS TABLE
---
id | creator

Creator.php

class Creator extends AppModel {
    public $actsAs = array('Containable');
    public $hasOne = array('Post');
}

Post.php

class Post extends AppModel {
    public $actsAs = array('Containable');
    public $belongsTo= array('Creator');
}

Please note the naming of the models and the association type in Post.php

IndexController.php

$this->set('posts', $this->Post->find('all', array('contain'=>array('Creator')));

Doing those changes, the array returned should be the one you're expecting.

Nunser
  • 4,512
  • 8
  • 25
  • 37
  • Also change the name of the model files (change it in my answer) – Nunser Apr 29 '13 at 20:03
  • @Alexandre you might want to read this chapter in the cakephp manual: [CakePHP Conventions](http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html). This chapter describes how you should name your classes, database-tables and fields so that CakePHP will 'automatically' understand them. – thaJeztah Apr 30 '13 at 07:10
0

You'll need a setup the correct relationships for your models. You're essentially looking to do a join between the two tables. Check out this answer: https://stackoverflow.com/a/5080026/2297744 I believe it's pretty similar to what you're trying to do.

The quick and dirty version would look something like this:

$this->Post->find('all', array(
    'joins' => array(
        array(
            'table' => 'creators',
            'alias' => 'CreatorJoin',
            'type' => 'inner',
            'conditions' => array(
                'CreatorJoin.id = Post.creator'
            )
        )
    )
);

But I'd recommend reading the full answer I linked to and using the first (more correct) example.

Community
  • 1
  • 1
Ryan Epp
  • 931
  • 1
  • 10
  • 21
  • I've edited my question showing what i have following the CakeBook. Even with the link you sent i can't get this working. Any idea? – Alexandre Apr 29 '13 at 19:15