3
votes

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?

3
The code looks correct. Did you clean the cache in app/tmp/cache? - dhofstet
I have the same problem it was not available till cakePHP 1.3 it come with CakePHP 2.4.x (at least I test). - SaidbakR
where you able to solve the query issue. I am facing same\ - aWebDeveloper
@aWebDeveloper: You should have a look at my fresh answer, this solved my query issue with custom metods in models, using Cake v1.3. - Radu Maris

3 Answers

4
votes

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();
0
votes

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();
    }
}
0
votes

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() {
        // ...
    }
}