我可以通过引用保存新的相关对象,而无需在 Kohana 3 的 ORM 中手动复制 id 吗?

发布于 2024-09-25 15:10:25 字数 364 浏览 4 评论 0原文

我有 2 个对象。球员和比赛。 Player 是 Match 的子级。我想知道是否可以同时创建这两个而不需要手动插入 id。

ie

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

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

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

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

类似于 Ruby 中的 ActiveRecord

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-10-02 15:10:25

如下:

$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();

请注意,按关系设置时应使用 saved ORM 对象。

附言。你们的关系好吗?一个玩家可以参加很多场比赛,所以我更喜欢另一种方案:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文