0

I have two tables posts and users. The post.user_id=users.id.

The fields are

user: id, username, password
post: id, user_id, post_name

I would like to select post.id, post.post_name, user.username.

How to select this in CakePHP?

I have two Models named User and Post. It calls the Model from PostController.

1
  • the two models have no relation Commented Apr 26, 2013 at 9:24

2 Answers 2

3

If you have correctly defined the relationship between those two models, this should just happen when you use the Post model's find method:

// In PostsController
$posts = $this->Post->find('all');

// $posts should be an array like this
Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    // Model data
                )
            [User] => Array
                (
                    // Model data
                )
        )
)

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

the two models have no relation
2

You should properly define the relations between the two models in order to simplify your life, but if you want to use a join, here is the syntax:

find('all', array(
        'conditions' => array('name' => 'Thomas Anderson'),
        'joins' => array(
            array(
                'alias' => 'Thought',
                'table' => 'thoughts',
                'type' => 'LEFT',
                'conditions' => '`Thought`.`person_id` = `Person`.`id`'
            )
        )
 ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.