Select with Inner Join with Cakephp

Asked

Viewed 1,278 times

3

I have a little problem for some time regarding a project that I am developing in cakephp, mine view can’t read a select with Inner Join table.

Controller:

           public function initialize()
       {

           $posts = $this->Posts->find("all",array(
              "joins" => array(
                  array(
                    "table" => "users",
                    "alias" => "User",
                    "type" => "INNER",
                    "conditions" => array("Post.user_id = User.id "),
                    "fields" => array('Post.*', 'User.username')
                   )
               )
            )
         );

model;

      public function initialize(array $config) {
          $this->addBehavior('Timestamp');
          $this->displayField('title');

         //join     
         $this->belongsTo('User');
}

View

<?= $post->username ?>

SQL code

SELECT posts.*,
       users.username
FROM   posts
       INNER JOIN users
               ON ( posts.user_id = users.id ) 

Explaining better, this query is seeking the "username" from table A to table B, and this table B of mine view can do the reading normally. With this select my bank brings the consultation exactly what I need, but my view does not display the result and returns null. Or it says if I try to do the view like this: $post->users->username, returns an error that does not find the object users.

  • How you are passing the data to the view?

  • <?= $post->username ? > is my view

2 answers

0

With JOIN's Cakephp places values inside their respective objects.

Probably your username is inside the object User:

$post->user->username

Give a pr() in its object $post so that it displays all the attributes.

Instead of doing sql pure, you could use the properties belongsTo(), hasMany() and hasOne() cakephp.


You need to query your controller and set the variable to exist in your view.

Controller

$posts = $this->loadModel('Posts');
$all = $posts->find('all');

$this->set('posts', $all);

Model Posts - Using belongsTo()

class PostsTable extends Table
{

    public function initialize(array $config)
    {
        $this->belongsTo('Users');
    }

}

If you haven’t created your models, check this one link

  • Yes Jeferson, this inside the obj User. Can you give me an example of how to use pr() ?

  • the function pr() of cake, is nothing more than a <pre>print_r()</pre>, you can use it as follows: pr($post), if my answer is correct, mark it so I can help other people who have the same problem.

  • Using this way, the "username" column is returning several arrays, for example: [_accessible:protected] => Array e [_properties:protected] => Array

  • Using the pr() it will display everything of the object, however calling $post->user->username it returns only the value

  • returns the error "Trying to get Property of non-object"

  • tries that way $post->User->username

  • Same error, no need to set anything in controller?

  • Put the code that is in your controller’s method

  • I posted as an answer to my question.

  • Edit your question and put in it, in your controller code you do not make any queries regarding the posts, only the part of AuthComponent

  • I edited. How could I make this query?

  • I edited my answers, so I understand you’re not using the models to query and this trying to call posts in the view without setting

  • I made the inclusions as you directed, but in the view, my "username" column keeps returning NULL, if I do, for example $post->user_id (which is a column created in the "post" table), pulls the Id right, but if I make $post->username (username which is a column of the Users table) considering the Inner Join I made, returns null.

  • Because the usename is inside the User (or user) object, try $post->user->username or $post->User->username

  • I edited the question with some changes

Show 10 more comments

0


If someone needs the resolution, you can do it by adding this code to the Postscontroller.

           public function index()
               {   
           $posts = $this->Posts->find('all', [
              'conditions' => ['Posts.online !=' => -1]])->order(['Posts.publication' => 'DESC'])->contain(['Users']);
           $this->set(compact(['posts']));
               }

Model

        public function initialize(array $config) {
            //funcao para trazer username do inner join
            $this->addBehavior('Timestamp');
            $this->displayField('title');
            $this->belongsTo('Users', [
                'foreignKey' => 'user_id',
                'joinType' => 'INNER',
            ]);
       }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.