Reputation: 901
In my CakePHP 2.3, I'm trying to call a function from User Controller to a City Controller, which in-turn calls a function from City Model.
Function in User Controller:
function get_cities(){
$list_cities = $this->City->get_all_cities();
}
Function in City Controller:
function get_all_cities(){
return $get_cities = $this->City->get_all_active_cities();
}
Function in City Model
public function get_all_active_cities(){
$cities=$this->find('all',array('conditions'=>array('City.status'=>1)));
return $cities;
}
But I'm getting a Database Error.
Error: SQLSTATE[42000]: Syntax error or access violation: 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 'get_all_active_cities' at line 1
How can I fix this issue? TIA.
Upvotes: 2
Views: 266
Reputation: 82
$this->loadModel('City'); You can use the data from city directly where ever its required. Hopes this helps you
Upvotes: 1
Reputation: 66217
for the right syntax to use near 'get_all_active_cities' at line 1
This error message means this code was executed:
$modelClass->get_all_active_cities();
However the model class does not have that method, and is instead passed to mysql as a query because that's how unhandled methods are implemented.
Either there is a typo in the method name, or $modelClass
is not the class you think it is
If you change your controller code like so:
function get_cities(){
debug(get_class($this->City)); //
die;
}
And see "AppModel" then your model class is not being used at all - typical reasons for that are:
Having a method named get_all_active_cities
that is just a simple find isn't really helpful. It's not encapsulating complexity, or making it clearer what the code does, arguably it makes the code harder to read since it's hiding something so simple. Consider simply using equivalent find:
function get_cities(){
$params = [
'conditions'=>[
'City.status'=>1
]
];
$list_cities = $this->City->find('all',$params);
...
}
Another alternative is to create a finder method so that the find logic is available in exactly the same way as the standard finders i.e.
function get_cities(){
$list_cities = $this->City->find('allActive');
...
}
With:
class Article extends AppModel {
public $findMethods = array('allAcctive' => true);
protected function _findAllActive($state, $query, $results = array()) {
if ($state === 'before') {
$query['conditions'][$this->alias . '.status'] = 1;
return $query;
}
return $results;
}
}
The benefit here is that the above finder is used in exactly the same way as the other finder methods.
If this is a condition to be applied to all finds, also consider using a behavior with a beforeFind callback to always apply that condition for you.
Upvotes: 1
Reputation: 4469
You can also load City Model in your users/get_cities
method.
Like this below code
function get_cities(){
$this->loadModel('City');
$list_cities = $this->City->get_all_cities();
}
Upvotes: 1
Reputation: 159
Quick hint, you can register your model within any controller you don't need to call the controller if you are only interested in calling the model function.
e.g. you can register the model using within your User controller
$this->City = ClassRegistry::init('City');
then call
$this->City->get_all_active_cities()
http://api.cakephp.org/2.3/class-ClassRegistry.html#_init
On the contrary you can also use var $uses = array('User','City');
within your Users controller to achieve the same
Also check the sql query generated from the sql log generated by CakePHP the issue points more towards the query generated by the framework. You can verify with Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual
Hope that helps, Cheers.
Upvotes: 1