Saturday, May 24, 2014

Re: 2 tables wont display

It looks like your `posts` table doesn't have a `user_id` column. CakePHP convention wants it to have one so Cake can make the association "Posts belongs to Users".

On Friday, May 23, 2014 12:56:48 AM UTC-4, Andrew Townsley wrote:
I am getting an error on a simple display of 2 tables as i am just learning it.
I know sql but cakephp is awkward with sql/

Anyway I have

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post.user_id' in 'on clause'

SQL Query: SELECT `Post`.`id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `User`.`two`, `User`.`three`, `User`.`id` FROM `jagguy`.`posts` AS `Post` LEFT JOIN `jagguy`.`users` AS `User` ON (`Post`.`user_id` = `User`.`id`) WHERE 1 = 1

models ( I have 2 tables with 1 files with id in common

class Post extends AppModel
{
/*var $name='User';*/
    public $belongsTo = array('User');
}

class User extends AppModel
{
    public $hasMany = 'Post';
}


class PostsController extends AppController
{
   
public  function home2()
{
  
    $everything=  $this->Post->find('all', array('contain' => array('User')));
} ....


view

////////

 <?php foreach ($everything as $item):
        echo '<tr><td>'. $item['Post']['id']. '</td>';
        echo '<td>'. $item['Post']['title'].'</td>';
           echo '<td>'. $item['Post']['body'].'</td>';






Connect with us: 

    

Please consider the environment before printing this email.

 

This e-mail and any attachments to it (the "Communication") is, unless otherwise stated, confidential,  may contain copyright material and is for the use only of the intended recipient. If you receive the Communication in error, please notify the sender immediately by return e-mail, delete the Communication and the return e-mail, and do not read, copy, retransmit or otherwise deal with it. Any views expressed in the Communication are those of the individual sender only, unless expressly stated to be those of National Training and Solutions Provider Pty Ltd ABN 34 123 831 023, or any of its related entities. NTSP does not accept liability in connection with the integrity of or errors in the Communication, computer virus, data corruption, interference or delay arising from or in respect of the Communication.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: