关于重写ORM的save方法的思考
我正在考虑在使用 Kohana PHP 框架开发的应用程序中重写 ORM 类的保存/创建/更新方法。
我想知道这样做是否是一个好的做法以及有什么优点和缺点。我这样做的原因是将所有重复代码从控制器中取出,并将其放在模型中重写方法内的一个位置。
例如。考虑一个简单的民意调查应用程序。有两个 ORM 类 - Model_Poll 和 Model_Choice 具有一对多关系。
现在,以下代码将被放置在控制器中,用于创建新的轮询,并保存它的选择(如果在 $_POST 中找到)
$poll = ORM::factory('poll');
$poll->name = 'some question ?';
$poll->save();
if ($this->request->post('choices')) {
foreach ($this->request->post('choices') as $choice) {
$c = ORM::factory('choice');
$c->name = $choice;
$poll->add($c);
}
}
我想将其更改为
控制器中的
$poll = ORM::factory('poll');
$poll->name = 'some question ?';
$poll->set_choices($this->request->post('choices'));
$poll->save();
Model_Poll 中的以下代码,
public function create(Validation $validation = null) {
$return = parent::create($validation);
if ($this->_choices) {
foreach ($this->_choices as $choice) {
$c = ORM::factory('choice');
$c->name = $choice;
$this->add($c);
}
}
return $return;
}
public function set_choices($choices) {
$this->_choices = $choices;
}
此创建方法将由保存方法。以后如果还有更多的事情要做,我可以自己在这里做。
感谢您的任何帮助。
I am thinking of overriding the save/create/update methods of ORM classes in an application being developed using Kohana PHP framework.
I would like to know whether it is a good practice to do so and what would be the pros and cons. My reason for doing this is to take all the duplicate code out of controller and put it in one place inside the overridden methods in model.
For eg. Consider a simple polls app. with two ORM classes - Model_Poll and Model_Choice with a one to many relationship.
right now, the following code would be placed in the controller for creating a new poll and also to save choices for it if found in $_POST
$poll = ORM::factory('poll');
$poll->name = 'some question ?';
$poll->save();
if ($this->request->post('choices')) {
foreach ($this->request->post('choices') as $choice) {
$c = ORM::factory('choice');
$c->name = $choice;
$poll->add($c);
}
}
I want to change this to following
in controller,
$poll = ORM::factory('poll');
$poll->name = 'some question ?';
$poll->set_choices($this->request->post('choices'));
$poll->save();
in Model_Poll,
public function create(Validation $validation = null) {
$return = parent::create($validation);
if ($this->_choices) {
foreach ($this->_choices as $choice) {
$c = ORM::factory('choice');
$c->name = $choice;
$this->add($c);
}
}
return $return;
}
public function set_choices($choices) {
$this->_choices = $choices;
}
This create method will get internally called by the save method. Later on, if there are more things to be done, i can do it here itself.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您选择的对象将不会被保存(需要
$c->save()
),因此$this->add($c)
也不起作用。为什么不使用这样的东西:
其中
set_choices()
方法将创建(如果需要)选择并保存当前民意调查的关系。Your choice objects will not be saved (
$c->save()
required), so$this->add($c)
will not work too.Why dont use something like this:
Where
set_choices()
method will create (if required) choices and save relations for current poll.