kohana - 帮助 has_many
我正在使用 kohana 3.2,我需要有关 has_many 关系的帮助。我无法让我的代码工作。 这就是我的数据库的样子
films
-id (pk)
-title
-description
sources
-id (pk)
-film_id
-code
class Model_Film extends ORM
{
protected $_has_many = array(
'sources' => array()
);
}
class Model_Source extends ORM
{
protected $_belongs_to = array(
'film' => array(),
);
}
源是电影的链接(例如 dvdrip、rmvb) 这就是我需要做的:
$film = ORM::factory('film');
$film->title = $title;
$film->description = $desc;
$film->year = $year;
$film->user_id = $uid;
$film->save();
$film->sources->film_id = $film->id; //last film id
$film->sources->name = $src_name;
$film->sources->code = $src_code;
$film->sources->save();
这只会为电影表添加值,但对于源来说,它会创建新的空记录。
I'm using kohana 3.2 and I need help with has_many relationship. I cant make my code working.
This is how my db looks
films
-id (pk)
-title
-description
sources
-id (pk)
-film_id
-code
class Model_Film extends ORM
{
protected $_has_many = array(
'sources' => array()
);
}
class Model_Source extends ORM
{
protected $_belongs_to = array(
'film' => array(),
);
}
Source is a link to movie (eg. dvdrip, rmvb)
and here is what i need to do:
$film = ORM::factory('film');
$film->title = $title;
$film->description = $desc;
$film->year = $year;
$film->user_id = $uid;
$film->save();
$film->sources->film_id = $film->id; //last film id
$film->sources->name = $src_name;
$film->sources->code = $src_code;
$film->sources->save();
This only adds values for film table, but for sources it makes new empty record.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为要添加到影片中的每一个源创建一个新源。例如:
对您想要添加到影片中的每个源重复此操作。与电影有很多关系,用于检索其来源,例如:
You'll need to create a new source for each one you want to add to the film. For example:
and repeat for each source you'd like to add to the film. The has many relationship for the film is used for retrieving it's sources, for example: