0

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.

2
  • The other commenters are right that the goals() function should go into the Player model. That way you can call it easily from any controller. But if necessary, it is possible to call actions from other controllers using requestAction. But in almost all cases this can, and should, be avoided. book.cakephp.org/view/991/requestAction Commented Sep 1, 2010 at 14:51
  • Hi, I konw about requestAction, and my exemple avoids it. I've already put the goals() function into Player, but still I cannot reach it from my Team controller. That is actually exactly the issue I describe above. Commented Sep 1, 2010 at 15:59

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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,you should encapsulate player's logic about database in player's model.Then call it from other controllers.
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.
0

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.

4 Comments

Oh, I've never heard about this possibility. Is it a recommended way to deal with this kind of situation? Cheers, 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.
This link might make it a bit clearer: book.cakephp.org/view/72/Additional-Methods-and-Properties
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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.