3

I have a users table that has location attributes and would like to create a model function that retrieves nearby users (within a given radius). Here is my model:

    class User extends AppModel {
        public function getNearbyUsers($id,$dist=10) {
            return $this->query(...);
        }
    }

And here is my controller where I am trying to invoke the function:

    class UsersController extends AppController {
        public function getNearbyUsers($id) {
            ...
            $this->User->getNearbyUsers($id)
            ...
        }
    }

However doing so results in: PHP Fatal error: Call to a member function getNearbyUsers() on a non-object

What am I doing wrong?


EDIT: nevermind, it is not complaining about that anymore. But it is throwing an SQL error and my model function is never actually being called. Upon further inspection on the mysql query log I see this:

    Query   SHOW TABLES FROM `xxx`
    Query   getNearbyUsers
    Quit


Seems CakePHP is interpreting $this->User->getNearbyUsers as a literal query. So my question still remains: how to add custom functions to a model in Cake?

4
  • The code looks correct. Did you clean the cache in app/tmp/cache? Commented Jul 7, 2012 at 9:52
  • I have the same problem it was not available till cakePHP 1.3 it come with CakePHP 2.4.x (at least I test). Commented Mar 18, 2014 at 11:37
  • where you able to solve the query issue. I am facing same\ Commented Aug 17, 2014 at 20:51
  • @aWebDeveloper: You should have a look at my fresh answer, this solved my query issue with custom metods in models, using Cake v1.3. Commented Jul 8, 2015 at 15:01

3 Answers 3

4

See http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html:

While CakePHP’s model functions should get you where you need to go, don’t forget that model classes are just that: classes that allow you to write your own methods or define your own properties.

Any operation that handles the saving and fetching of data is best housed in your model classes. This concept is often referred to as the fat model.

Model

class Example extends AppModel {
    function getRecent() {
        $conditions = array(
            'created BETWEEN (curdate() - interval 7 day) and (curdate() - interval 0 day)'
        );

        return $this->find('all', compact('conditions'));
    }
}

This getRecent() method can now be used within the controller.

Controller

$recent = $this->Example->getRecent();
Sign up to request clarification or add additional context in comments.

2 Comments

As you can see from the code I included I am pretty much following exactly what you posted :)
The doc entry is invalid now (maybe is was deleted) because the link just redirects to models documentation, where there is no mention about additional methods.
0

There are a few additional items that needs to be in the code, or else you will get the non-object error.

In the App Model :

<?php

class Get extends AppModel {
    public function getRecent() {
        // $conditions = array(
            // 'created BETWEEN (curdate() - interval 7 day)' .
            // ' and (curdate() - interval 0 day))'
        // );
        // return $this->find('all', compact('conditions'));
    }
}

In the app controller,

?php



class GetsController extends AppController {

    public $uses = array('Get');  // Needed, or the error will appear.

    public function Example () {
       $this->Get->getRecent();
    }
}

Comments

0

Had the same issue with Cake 1.3, using Plugins (Modules), and even if we had the model name unique in the whole app (some model names are used in more then one plugin) it only worked once I requested the Model in the controller's $uses array with it's plugin to, like this: 'Module1.A'

app/plugins/plugin1/controllers/a_controller.php:

class AController extends AppController {

    // using simple array('A') worked fine for cake methods (find, query ...)
    // but did not recognized the custom method
    public $uses = array('Plugin1.A');

    public function Example () {
       $this->A->customMethod();
    }
}

app/plugins/plugin1/models/a.php:

<?php

class A extends AppModel {
    public function customMethod() {
        // ...
    }
}

Comments

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.