假设我有一个实体模型,它是人员和组织模型的基础。
我有三个空集合,一个用于实体,一个用于人员,一个用于组织。
让我们假设出于这个问题的目的,人员和组织之间的关系将被忽略。
最后,我有一个视图,其中包含所有三个模型的字段。
我的问题:我是否创建一个模型类作为 DTO(数据传输对象)只是为了保存所有三个模型的数据并将它们保存到控制器中各自的集合中?
DTO 层次结构如下所示:
- 添加客户端DTO
- 实体(带字段)
- 组织(带字段)
- 人(带字段)
- 实体(带字段)
假设我有一个实体模型,它是人员和组织模型的基础。
我有三个空集合,一个用于实体,一个用于人员,一个用于组织。
让我们假设出于这个问题的目的,人员和组织之间的关系将被忽略。
最后,我有一个视图,其中包含所有三个模型的字段。
我的问题:我是否创建一个模型类作为 DTO(数据传输对象)只是为了保存所有三个模型的数据并将它们保存到控制器中各自的集合中?
DTO 层次结构如下所示:
听起来您正在尝试使用单个表单和控制器操作为 3 个不同的模型创建记录。这是一种简单的方法,至少可以帮助您入门。
首先,在 app/models 中创建实体、人员和组织模型。
然后,创建一个包含以下操作的实体控制器:
public function add() {
$entity = Entities::create();
if($this->request->data && $entity->save($this->request->data)) {
echo 'Success!';
exit;
}
return compact('entity');
}
接下来,创建 /app/views/entities/add.html.php:
<?=$this->form->create($entity); ?>
<?=$this->form->label('name', 'Entity Name');?>
<?=$this->form->text('name');?>
<?=$this->form->label('person_data[name]', 'Person Name');?>
<?=$this->form->text('person_data[name]');?>
<?=$this->form->label('organization_data[title]', 'Organization Title');?>
<?=$this->form->text('organization_data[title]');?>
<?=$this->form->submit('Save');?>
<?=$this->form->end(); ?>
最后,描述您的关系并在 app/models/Entities.php 中添加一个过滤器,以在保存数据时保存人员和组织:
<?php
namespace app\models;
use \app\models\People;
use \app\models\Organizations;
class Entities extends \lithium\data\Model {
public $belongsTo = array(
'Organizations' => array(
'class' => '\app\models\Organizations',
'key' => 'organization_id',
),
'People' => array(
'class' => '\app\models\People',
'key' => 'person_id',
),
);
public static function __init() {
parent::__init();
static::applyFilter('save', function($self, $params, $chain) {
// If data is passed to the save function, set it in the record
if ($params['data']) {
$params['entity']->set($params['data']);
$params['data'] = array();
}
$record = $params['entity'];
if(isset($record->person_data)) {
$person = People::create($record->person_data);
if($person->save()) {
$record->person_id = $person->id;
}
}
if(isset($record->organization_data)) {
$org = Organizations::create($record->organization_data);
if($org->save()) {
$record->organization_id = $org->id;
}
}
$params['entity'] = $record;
return $chain->next($self, $params, $chain);
});
}
}
?>
如果我对您的尝试做了太多假设,请发表评论,我会相应地调整代码。希望这可以帮助!
我得到了代码,可以进行一些编辑。为了简洁起见,我只显示了 People 实体,但它也适用于 Organizations 块。编辑是过滤器将人员模型保存到数据库的位置。
从:
if(isset($record->person_data)) {
$person = People::create($record->person_data);
if($person->save()) {
$record->person_id = $person->id;
}
}
到:
if(isset($record->person_data)) {
//added this to make an array out of the $record->person_data(mongo doc)
//$person_data = array();
//foreach($record->person_data as $key => $value) {
//$person_data[$key] = $value;
//}
//replaced the mongo doc with the array
//Edited: accessing the data() method on an object will return an array of the properties, doing exactly what I put up there initially.
$person = People::create($record->person_data->data());
if($person->save()) {
//replaced the "id" with "_id"
$record->person_id = $person->_id;
}
//removed the person_data embedded doc from the entity doc
unset($record->person_data);
}