views:

175

answers:

3

Hi,
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 cake wants to launch an SQL query starting by "goals".

Cheers,
Nicolas.

A: 

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);
SpawnCxy
Hi, I totally understand that the links are between models, it was just there to be clear about the issue. If it is a bad idea, then how could I count the number of goals a player has scored (or do whatever does the goal() function) ? There are cases where you have to call a function from another controlelr I think, or duplicate the function (which is an even worse idea in my view). Cheers, Nicolas.
Nicolas
@Nicolas,you should encapsulate player's logic about database in player's model.Then call it from other controllers.
SpawnCxy
@Spawn: the real case is actually a team of artists working on images. My function which is located in the artist controller calculate statistics about the images done by an artist. I don't see how this could be encapsulated in the database, I mean there's no point storing statistics that can be calculated with no history needed.
Nicolas
@Nicolas,see my update please.
SpawnCxy
A: 

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.

Leo
Oh, I've never heard about this possibility. Is it a recommended way to deal with this kind of situation? Cheers, Nicolas.
Nicolas
I try to keep methods that return pure or pseudo data on the model. Anything that manipulates and prepares data and chucks it out to display, I put on the controller. Anything that manipulates the way something is displayed, I put in the view. Most people will argue their own particular implementation of MVC but this one works for me and keeps me sane.
Leo
This link might make it a bit clearer: http://book.cakephp.org/view/72/Additional-Methods-and-Properties
Leo
I see what you mean. As long as it is data manipulation from the current model only to display, you put the function in the model. If I got your point, I think that's a good way to deal with it, enabling pseudo fields to be done as well.
Nicolas
A: 

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.

Nicolas