<krogerlawoffices@gmail.com> wrote:
> I am building an app the keeps track of football games. Thought it
> would be a good project to help me learn cakePHP.
>
> My "schedules" table has fields for hometeam, awayteam, homescore,
> awayscore, and gamewinner. I created a model and controller based on
> this table that can be called to update the gamewinner.
>
> I can pull the data from the table but I am not sure how to save the
> gamewinner field once it is calculated.
>
>
> <?php
> class UpdateschedulesController extends AppController {
>
> var $name = 'Updateschedules';
>
> function index() {
> // Get all records from 'schedules' table using 'updateschedule'
> model
> $query = $this->Updateschedule->find('all');
>
> // Move through each row to find game winner
> // Ignoring the possibility of a tie for the moment
> foreach ($query as $row)
> {
> if ($row["Updateschedule"]["awayteamscore"] >
> $row["Updateschedule"]["hometeamscore"]) {
> // awayteam is winner, update gamewinner field
> with teamname
> $row["Updateschedule"]["gamewinner"] =
> $row["Updateschedule"]["awayteam"];
> } else {
> // hometeam is winner, update gamewinner field
> with teamname
> $row["Updateschedule"]["gamewinner"] =
> $row["Updateschedule"]["hometeam"];
> }
> // New data will not save, it appears in the array if I use
> print_r
> $this->Updateschedule->save($row["Updateschedule"]);
>
> }
> // Send data to view to display updated data
> // Does not display gamewinner
> $this->set('schedules', $query);
> }
> }
>
> ?>
There are a few things about this that I'd do differently. First,
though, the reason you're not seeing the updated info is that the
$query variable itself is not being updated. It's created when you do
the initial find() but isn't itself modified as you do the later
calculations.
Anyway, I don't think you require an UpdateSchedule model and
controller. Schedule should suffice. What you're doing here i using a
model to deal with an action. Better to do it all in
SchedulesController.
However, ask yourself if you really need to save the results of these
calculations. Why not do this in SchedulesController index action?
And, in that case, you should be checking the date of the match as you
run through the array. Actually, that brings up another issue: is the
match finished at the time the action is requested?
So, say you're array looked like:
'Schedule' => array(
'hometeamscore' => x,
'awayteamscore' => y,
// other fileds ...
)
As you loop through to do the calculations, simply add a new key and
value like so:
$data = $this->Schedule->find(...);
foreach ($data as $key => $row)
{
if ($row['Schedule']['awayteamscore'] > $row['Schedule']['hometeamscore'])
{
$data[$key]['Schedule']['gamewinner'] = 'away';
}
else if ($row['Schedule']['hometeamscore'] >
$row['Schedule']['awayteamscore'])
{
$data[$key]['Schedule']['gamewinner'] = 'home';
}
else
{
$data[$key]['Schedule']['gamewinner'] = 'tie';
}
}
$this->set(compact('data'));
I'm sure that even this could be improved, though. I know there are at
least a couple of other people on thelist who've created apps dealing
with sports teams so maybe one of them could chime in.
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
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:
Post a Comment