tags:

views:

26

answers:

1

I'm working in a CakePHP Model class, writing a function that is supposed to perform a whole bunch of manipulations on the model. I'm trying to follow the skinny controller, fat model idea. However, I don't seem to be able to call any of the model's functions from with in my model. Something hasn't been initialized yet, because I get an SQL error when I do:

Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 681]

Query: SHOW FULL COLUMNS FROM 

It looks as if the table name hasn't been set internally somewhere. My model looks like this:

class Search extends AppModel {
    var $name='Search';
    var $hasMany = 'SearchResult';
    var $actsAs = array('Containable');


    function search($query) {
            $this->create();
            $this->set('query', $query);
            $this->save();
        }
}

I know everything the Model needs has already been created works find, because calling that exact same sequence of functions works fine from the Model controller. Like so:

function search($query) {
            $this->Search->create();
            $this->Search->set('query', $query);
            $this->Search->save();
}

That works fine. So what gives? What's going on here?

A: 

What are you trying to do with the set() method? A method by that name exist for both Model & Controller, but I've never had a reason to use it in the Model context. Moreover, your particular use looks like it belongs in the Controller context (though that's an educated guess at best).

If you're trying to set a variable named query that can be accessed by the view, then it needs to be in the controller. create() and save(), of course, are staples of the Model context.

Rob Wilkerson
That was an extraordinarily boiled down version of it. It's a very detailed search function with multiple model manipulations (creating the parent and associated models based on multiple queries to the database and other models). In the context of the model, the `$this->set($field, $value);` is the same as `$this->data[$field] = $value;` The issue turned out to be a name collision between my `search` function and something deeper in Cake's call stack. Changing the name of the function made everything happy again.
Daniel Bingham
Glad you tracked it down. Too bad the docs for `Model::set` aren't as concise as your explanation.
Rob Wilkerson
@Rob Yeah, the documents are definitely rather lacking. Makes the learning curve for cake a lot rougher than it needs to be. Thanks for the suggestions though.
Daniel Bingham