Thursday, January 29, 2015

Re: Two tables in the same page

Try to change like:

CREATE TABLE comments (
id INT UNSIGNED AUTO_INCREMENT,
post_id INT UNSIGNED,

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->Post-Comment->find('all'));
    }
}

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

Comment.php
<?php
class Comment extends AppModel {
    public $belongsTo = array(
        'Post'
    );
}


Andras Kende

On Jan 29, 2015, at 9:45 AM, logintech123 <logintech@gmail.com> wrote:

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: