views:

35

answers:

1

I have 2 objects. Player and Match. Player is a child of Match. I want to know if I can create both of these at the same time without inserting id's manually.

i.e.

$match = ORM::factory('match');

$player1 = ORM::factory('player');
$player2 = ORM::factory('player');

$player1->match = $match;
$player2->match = $match;

$match->save();
$player1->save();
$player2->save();

Similar to ActiveRecord in Ruby

A: 

Here is it:

$match = ORM::factory('match');
// fill Match with values
$match->result = MATCH_RESULT_WIN;
$match->started = time();
// save before using!
$match->save();

$player1 = ORM::factory('player')->where('name', '=', 'Federrer')->find();
$player2 = ORM::factory('player')->where('name', '=', 'Nadal')->find();
$player1->match = $match;
$player1->save();
$player2->match = $match;
$player2->save();

Note that you should use saved ORM object when setting it by relationship.

PS. Is your relationship right? One player can play a lot of matches, so I'd preffer another scheme:

// Match belongs to player1&player2
$match->player1 = $player1;
$match->player2 = $player2;
$match->save();
biakaveron