-1

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();
  }
}
user2610087
  • 117
  • 3
  • 13
  • just paste the how you've declared the class names for the above files – Anil kumar Sep 04 '13 at 14:27
  • $uses is not required if the model name follows the convention (singular of controller name). – floriank Sep 04 '13 at 14:44
  • Your beforeFilter() method is flawed: You don't call the parent so any other settings that are there, most likely Auth, won't be present in this controller. – floriank Sep 04 '13 at 16:16

2 Answers2

1

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.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • eh.. are you sure? the AppModel has the Model Suffix in class name – user2610087 Sep 04 '13 at 14:21
  • Define not working. If you're a programmer you should know that "not working" is an insufficient issue report. And yes I'm sure. Because the default model is loaded based on the controllers name that gets singlurized by the Inflector if not otherwise loaded. And why do you extend ShopModel and ShopController instead of AppController and AppModel? – floriank Sep 04 '13 at 14:42
  • Not working means it does not solve the described issue but has no else affect on my search for $this->Customer – user2610087 Sep 04 '13 at 14:51
  • Again: *What is the exact error message you get?* I don't care what your interpretation is, I want to know the error/notice php throws. You got it now? And show the whole relevant code, not just the part you have now. – floriank Sep 04 '13 at 15:20
  • there is no error message - if so i would have wrote it.. wow.. just read and get there is no $this->Customer, so the model is not loaded into $this.. There is NO error message, but i guess you can imagine i might need to be able to get the model callbacks called, which i'm currently not. – user2610087 Sep 04 '13 at 15:24
  • You statement can't be true: If there really would be no property then there would be this message: Undefined property: CustomersController::$Customer Maybe do some basic php tutorials first...? Whats your debug level? 0? If yes set it to 2. Also it would be brilliant if you would tell us how you debug that there is no property. – floriank Sep 04 '13 at 15:44
  • so again for you unfriendly ...: i use xdebug debug level already set to 2 there IS NO ERROR OUTPUT, how many times again? stop flaming and read what i write, if it would be that easy i'd solve it for myself.. i'd advise wheter you believe my statements or search for another thread.. thx man – user2610087 Sep 04 '13 at 15:59
  • Again, you're unwilling or not able to show the info that is needed to help you: How do you determine that there is no output? *What is the result of debug($this->Customer)*??? Null? Empty string? Model Instance? Thats a *huge* difference to "NO OUTPUT". And how many more times does somebody have to tell you to show the whole code? Also see my update to the answer. – floriank Sep 04 '13 at 16:18
  • not sure if you ever used X-Debug but the Object is not existing inside $this. – user2610087 Sep 06 '13 at 07:11
0

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 {
   // ...
}
Anil kumar
  • 4,107
  • 1
  • 21
  • 36