Yii 将数据从一个模型复制到另一个模型 [英] Yii copying data from one model to another

查看:51
本文介绍了Yii 将数据从一个模型复制到另一个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 yii 的新手.我使用由 CFormModel 和内部控制器扩展的模型从表单中收集数据,我想将这些数据复制到从 CActiveRecord 扩展的模型中,以便保存到 DB.有没有一种方法或方法可以将数据从数据收集模型复制到数据保存模型,而不是通过属性到属性来执行此操作,因为它太丑了.提前致谢.

I'm new to yii. I collect data from a form using a model extended by CFormModel and inside controller I want to copy these data to a model which is extended from CActiveRecord in order to save to DB. Is there a method or way to copy data from data collected model to data saving model rather than doing this by attribute to attribute as it's so ugly. Thanks in advance.

推荐答案

您可以通过以下方式获取所有模型属性:

you can get all models attributes by:

$data = $model->attributes;

并将它们分配给另一个模型

and assign them to another model

$anotherModel = new AnotherActiveRecord();
$anotherModel->setAttributes($data);
$anotherModel->save();

现在另一个模型将从 $data

这篇关于Yii 将数据从一个模型复制到另一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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