Cakephp call a function from another controller returns "Warning (512): SQL Error: 1064"
I'm sure this issue can be solved rapidly, but I don't find any answer on the web, so here I am. I want to call a function from another controller, but CakePHP does not recognize it as a function but as a query, returning a warning:
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 'goals' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 673]
Here's the logic of what I am trying to do:
//from the TeamController
$this->Team->Player->goals()
//in the PlayerController
function goals() {
//code
}
As you can guess by the names, a Team hasMany Players and a Player belongsTo a Team. I thought this was the way to deal with it, but it's obviously not working because the ca开发者_JAVA百科ke wants to launch an SQL query starting by "goals".
Cheers,
Nicolas.Well, relationships are between models.So you cannot call a controller's function --- action, via them. That means if you want to make your code work fine ,goal()
should be a function in player's model instead of in the controller.
BTW,calling a function from another controller is properly a bad idea.
Update:
to get the score of some player in team controller
/*in player's model*/
function goal($player_id)
{
return $the_score_of_player_id;
}
/*in team controller*/
$score = $this->Team->Player->goal($player_id);
Write the method goals()
on the Player model (app/models/player.php
). It is a data function as opposed to a data manipulation function and, therefore, is more properly located on the model.
None of the solutions here worked and it looked like the function had to be slightly different from the one on the other controller, so I decided to rewrite a new one in the controller.
精彩评论