Thursday, January 29, 2015

Two tables in the same page

Hello

I am trying to understand Cakephp but i am missing something. I want to display two tables. I tried many different things but i ALWAYS end up on the same message "Call to a member function find() on a non-object"

CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

CREATE TABLE comments (
post_id INT UNSIGNED AUTO_INCREMENT,
    name VARCHAR(50),
    comment TEXT,
    created DATETIME DEFAULT NULL,
FOREIGN KEY (post_id) REFERENCES posts(id)
);

PostsController.php
<?php
class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

    public function index() {
        $this->set('posts', $this->Post->find('all'));
$this->set('comments', $this->Comment->find('all'));
    }
}
?>

Posts.php
<?php
class Posts extends AppModel {
}
?>

Comments.php
<?php
class Comments extends AppModel {
    public $hasMany = array(
        'Comments' => array(
            'className' => 'Comment',
            'foreignKey' => 'post_id',
            'dependent' => true
        )
    );
}
?>

The problems seems to be in the PostController file but i don't know what is the problem.

--
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: