0
votes

I am making a PHP forum with CakePHP I am troubles with getting an array of all the members then echoing them in a view, here is my code.

<?php
App::uses('AppModel', 'Model');

class Member extends AppModel {

    public $validate = array(
        'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty')
            ),
        ),
        'password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty')
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email')
            ),
        ),
    );

    public $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Topic' => array(
            'className' => 'Topic',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}

That is my member model here is my MembersController

<?php
App::uses('Controller', 'AppController');

class MembersController extends AppController {

    public $components = array('Paginator');

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('profile','login');
    }

    public function index(){
        $this->Paginator->settings['contain'] = array('Member');
        $this->set('members', $this->Paginator->paginate());
    }

    public function profile($id=null) {}

    public function login() {
        if($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}

And here is my Index view

<div class="row">
    <?php foreach ($members as $member): ?>
        <?php echo $user[name]; ?>
    <?php endforeach; ?>
</div>

When I access example.com/members I get an error saying Model "Member" is not associated with model "Member" [CORE/Cake/Model/Behavior/ContainableBehavior.php, line 342]

Before you ask I have made AppModels actas Containable

class AppModel extends Model {
    public $actsAs = array('Containable');
}
3
With lots of help from burzum I have fixed it and I have also found-out that it doesn't matter what you call the controler you can use any models you like as long as you add public $uses = array('Model1', 'Model2', 'Model3'); at the top of the controller - Jack Price-Burns
Instead of loading lots of models into the controller (even if they're lazy loaded) it is better to access other models through the associations of the primary model of that controller. - floriank

3 Answers

1
votes

Remove that line:

$this->Paginator->settings['contain'] = array('Member');

You're causing a self-join by using this and you haven't defined that in your members model and you dont want that.

This is wrong as well plus your php has a syntax error - missing ' around the name.

<?php foreach ($members as $member): ?>
        <?php echo $user[name]; ?>
    <?php endforeach; ?>

It should be:

<?php foreach ($members as $member): ?>
        <?php echo $member['Member']['name']; ?>
    <?php endforeach; ?>

You're struggling with the very basics, I would recommend you to do the blog tutorial or at least read about how data is passed to the view and how model associations work. You can find everything in the book.

0
votes

Your problem is here:

public function index(){
    $this->Paginator->settings['contain'] = array('Member');
    $this->set('members', $this->Paginator->paginate());
}

Since paginate is being called from the members controller, it will by default try to paginate Member. Your contain setting is indicating the Member data should also contain the associated model, also named Member. You have no association between Member and itself, hence the error. I'm going to assume that contain setting was a mistake, so simply remove the line $this->Paginator->settings['contain'] = array('Member');

0
votes

$this->Paginator->settings['contain'] = array('Member'); is your problem. Since the setting are going to be passed as options to find() call on Member model you are effectively asking Member to contain itself. Set 'contain' to array() to prevent any associated records from being fetched.

Though the recommended way it to set property $recursive to -1 in AppModel so that by default no associated records are fetched. Containable is then used to fetch associated records as required.