0

I develop a website using cakephp. But the response time is too slow. So, I want to implement fat model and skinny controller.

But i got

Call to a member function fashionpage() on a non-object

when calling fashionpage function in controller.

I have Home model(relationship: Home hasMany Cart)

My controller:

public function fashionlist() {
        $user = $this->Auth->user('id');

        $counter = $this->Home->fashionpage($user);
        $this->set(compact('user', 'counter'));
    }

My model:

public function fashionpage($user = null) {
        return $this->Home->Cart->find('count', array('conditions'=>
                                                   array('conditions'=>array('User.id'=>$user))));
      }

can anyone please help me.

7
  • fashionpage function is in Home Model or in Cart Model? Commented Feb 7, 2014 at 8:29
  • in home model @arilia Commented Feb 7, 2014 at 8:29
  • and fashionlist function is in HomesController? Commented Feb 7, 2014 at 8:32
  • yes yes. in homesCOntroller @arilia Commented Feb 7, 2014 at 8:37
  • From the error it's obvious you haven't loaded the Home model. Try via $this->loadModel('Home'); or $uses =array('Home');. And do @arilia fix Commented Feb 9, 2014 at 23:53

2 Answers 2

1

There's an error in your model. In fact when you are in your Model file $this is the Model (Home in your case), so no need to do $this->Home->Cart, but just $this->Cart

so not

public function fashionpage($user = null) {
        return $this->Home->Cart->find('count', array('conditions'=>
                                                   array('conditions'=>array('User.id'=>$user))));
      }

but

public function fashionpage($user = null) {
        return $this->Cart->find('count', array('conditions'=>
                                                   array('conditions'=>array('User.id'=>$user))));
      }
Sign up to request clarification or add additional context in comments.

3 Comments

when you ask 'fashionpage function is in Home Model or in Cart Model? ', i try put that function in cart model. and remove the model name as: 'public function fashionpage($user = null) { return $this->find('count', array('conditions'=>array('User.id'=>$user))); }' and now it run successfully. thanks for the idea..hehe
there's something wrong if it works the way you did. If you do that then in your HomesController you have to do $this->Home->Cart->fashionpage($user);
may be your association "Home hasMany Cart" is not working properly .Do check that also.
0

You must call it with persistent object I think

2 Comments

what did u means by persistent object ya?
$this->model('modelname');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.