cakephp 2 $this->Model not set

440 Views Asked by At

I am working on Cake 2.4 and if i debug $this in my controller, then $this->Model is not set but should.

Controller: CustomersController(.php)

Model: CustomerModel(.php)

Since the naming Conventions are right, i have no clue where the issue is be located.

RELEVANT Code:

Customer.php:

<?php

class Customer extends Shop {
   public $validate = array(/* ... */);
   protected $_schema = array(/* ... */);

   public function beforeSave($options = array()) {
      parent::beforeSave($options);
   }
}


CustomersController.php:

<?php
App::uses('ShopsController', 'Controller');
class CustomersController extends ShopsController {

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

There are 2 best solutions below

9
On

Your model filename is wrong. It should be "Customer" without the "Model" suffix. Only this way it gets automatically loaded and become available as $this->Customer in your controller.

Edit: You're extending not AppModel but ShopModel for some reason (Why?), so try this in your Customer model:

public $name = 'Customer';
public $useTable = 'customers';

CakePHP does not properly merge/update all properties when you inherit controllers or models.

9
On

Model filename should be Customer.php, Don't append model to your Model name, do like this in your Customer.php

<?php

class Customer extends ShopModel {
   // ...
}