Friday, December 5, 2008

Re: Custom queries/sql in models/behaviors

First of all, welcome to cakephp!

Cake's Model layer can be a big timesaver, so I suggest you get to
know it a bit better before dismissing it. However, you can execute
custom queries by using the $model->query() method. The one anomaly
with this is that cake transforms the results into a cake-like
structure. It can be good or bad depending on what you need to do, but
keep in mind you may need to massage the results a bit.

Behaviors are meant to be generic and reusable, so unless you have
more than one Team model, I would just add your methods into your Team
Model rather than create a behavior for it.

Instead of repeating your $model->query() or $model->find() calls
throughout your controller, you can just add that code into a method
in your team model.

function getStuff($id) {
return $this-find('all', array('conditions' => 'blah'));
}

Hope that helps.

Cheers,
Adam

On Dec 5, 9:23 am, gearvOsh <mileswjohn...@gmail.com> wrote:
> Ok in my current system (its a gaming league) I have packages. I will
> use a Team package as an example. In this Team package I have a method
> getTeamInfo() which calls out to external queries. I do this so I only
> have to write a query once and can access its results through a method
> call. I prefer this way instead of having to write the same query over
> and over for different instances.
>
> require('teamExt.php');
> require('userExt.php');
>
> class Team {
>
>         public static function getTeamInfo($team_id) {
>                 $team = TeamExt::getTeamInfo($team_id);
>
>                 if (!empty($team)) {
>                         $team['roster'] = TeamExt::getRoster($team_id);
>                         $team['games']  = TeamExt::getGamesPlayed($team_id);
>                         $team['stats']  = TeamExt::getStats($team_id);
>                         $team['leader'] = UserExt::getUser($team['leader_id']);
>                 }
>
>                 return $team;
>         }
>
> }
>
> Now when I call Team::getTeamInfo I should have its team info, roster,
> games played, the team leader and league statistics. Now the problem
> with CakePHP is its model system. I am not a big fan of the model
> system because its very limiting if you have advanced queries. I was
> thinking of doing it this way:
>
> Class Team = Model Team
> TeamExt = Team Behavior
> UserExt = User Behavior
>
> $team = $this->Team->getTeamInfo();
>
> But how would I do custom queries in a behavior? And the logic for
> behavior is wrong with my idea anyways. Anyone have an idea of how I
> can accomplish my setup in CakePHP, because this is the one thing
> stopping me from using CakePHP. And no I will not use joins.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: