Cannot call set or create from with in a Model class in CakePHP?
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 be开发者_C百科en 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?
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.
The solution turned out to be a naming collision with my function search
and something deeper in CakePHP's call stack. Changing the name of the function from search
to something else (mySearch, for instance) fixed it.
精彩评论