在 Yii 中保存多个子模型 [英] Save multiple child models in Yii

查看:21
本文介绍了在 Yii 中保存多个子模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意: SO 上还有其他与此相关的问题,但没有一个真正为我回答.我并不担心服务器端,因为有很多方法可以处理保存相关模型信息(以及如何执行此操作的大量示例).

我需要知道的是:如何在创建父模型的同时实现视图代码以添加多个子模型?

它肯定会像在这个页面上看到的那样:http://www.yiiframework.com/doc/guide/1.1/en/form.table.除了代码处理一堆模型的更新,而不是它们的插入.这正是我需要的.

简而言之,我有一个事件表单,用户可以在其中添加许多约会(每个约会都有一天、start_time 和 end_time).我想使用 Yii 助手在事件表单中呈现约会字段,以便获得验证和其他框架优势.

用户可以在创建活动时为同一活动添加多个约会.

解决方案

渲染模型数组(不管它们是否为子模型)的正确语法需要相关模型的实例化.所以你必须在你看来做这样的事情:

$child = new ChildModel();$form->textFieldRow($child,'[]PROPERTY',$dateAtts);

其中 PROPERTY 是您要为其呈现文本框的属性的名称.

如果你想要一个表单来创建和更新父模型,唯一的方法是实例化一个模拟对象来渲染表单.在我的例子中,事件有一个约会集合,所以在我的控制器操作中我做了:

$event->appointments = array(new Appointment);

然后,在视图中

appointments as $id => $item) : ?><div class="约会"><?php echo $form->textFieldRow($item,'[$id]day',$htmlAtts);?>

<?php endforeach;?>

我要试试这个扩展来保存相关模型:https://github.com/yiiext/with-related-behavior

更新

如果您想在 $_POST 中的父模型数组中包含子模型,则必须覆盖 name 属性.按照问题示例...

$form->textFieldRow($项目,'[$id] 天',array('name' => "Event[约会][$id][day]"));

NOTE: There are other questions related to this on SO, but none of them really answered it for me. I'm not worried about the server side as there are a lot of ways to handle saving related models info (and plenty examples of how to do this).

What I need to know is: how do I implement the view code to add many child models while creating the parent model?

It, surely, will be something like seen on this page: http://www.yiiframework.com/doc/guide/1.1/en/form.table. Except the code handles an update of a bunch of models, not the insertion of them. Which is what I need.

In short, I have an event form, where the user can add many appointments (each appointment has a day, start_time and end_time). I want to render the appointment fields inside the event form using Yii helpers so I get validation and other framework benefits.

The user will be able to add multiple appointments for the same event while creating the event.

解决方案

The correct syntax to render what an array of models (doesn't matter if they are child models or not) requires the instantiation of the model in question. So you would have to do something like this in your view:

$child = new ChildModel();
$form->textFieldRow($child,'[]PROPERTY',$dateAtts);

Where PROPERTY is the name of the property you want to render the textbox for.

If you want a single form for both creating and updating the parent model, the only way is to instantiate a mock object just to render the form. In my case, Event has a collection of Appointment, so in my controller action I did:

$event->appointments = array(new Appointment);

Then, in the view

<?php foreach($model->appointments as $id => $item) : ?>
    <div class="appointment">
        <?php echo $form->textFieldRow($item,'[$id]day',$htmlAtts); ?>
    </div>
<?php endforeach; ?>

I'm going to try this extension to save the related models: https://github.com/yiiext/with-related-behavior

Update

If you want to have the child model inside the parent model array in $_POST, you'll have to overwrite the name attribute. Following the question example...

$form->textFieldRow(
            $item,
            '[$id]day', 
            array('name' => "Event[appointments][$id][day]")
       );

这篇关于在 Yii 中保存多个子模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆