@@ -57,7 +57,7 @@ public function __construct(?Model $model = null, bool $with = false)
57
57
public function initData (bool $ relation = true )
58
58
{
59
59
// 获取实体属性
60
- $ properties = $ this ->getEntityProperties ();
60
+ $ properties = $ this ->getEntityPropertiesMap ();
61
61
$ data = $ this ->model ()->getData ();
62
62
foreach ($ properties as $ key => $ field ) {
63
63
if (!$ relation ) {
@@ -126,21 +126,35 @@ private function fetchViewAttr(string $field)
126
126
* @return array
127
127
*/
128
128
private function getEntityProperties (): array
129
+ {
130
+ $ reflection = new ReflectionClass ($ this );
131
+ $ properties = [];
132
+ foreach ($ reflection ->getProperties (ReflectionProperty::IS_PUBLIC ) as $ property ) {
133
+ $ properties [] = $ property ->getName ();
134
+ }
135
+ return $ properties ;
136
+ }
137
+
138
+ /**
139
+ * 获取包含映射关系的实体属性列表.
140
+ *
141
+ * @return array
142
+ */
143
+ private function getEntityPropertiesMap (): array
129
144
{
130
145
$ properties = $ this ->getOption ('view_properties ' );
131
146
if (empty ($ properties )) {
132
- $ reflection = new ReflectionClass ( $ this );
147
+ $ fields = $ this -> getEntityProperties ( );
133
148
$ mapping = $ this ->getOption ('viewMapping ' , []);
134
149
$ properties = [];
135
-
136
- foreach ($ reflection ->getProperties (ReflectionProperty::IS_PUBLIC ) as $ property ) {
137
- $ field = $ property ->getName ();
150
+ foreach ($ fields as $ field ) {
138
151
if (isset ($ mapping [$ field ])) {
139
152
$ properties [$ field ] = $ mapping [$ field ];
140
153
} else {
141
154
$ properties [] = $ field ;
142
155
}
143
156
}
157
+
144
158
$ this ->setOption ('view_properties ' , $ properties );
145
159
}
146
160
@@ -163,17 +177,31 @@ public function toArray(): array
163
177
return $ data ;
164
178
}
165
179
180
+ /**
181
+ * 设置视图模型数据
182
+ *
183
+ * @param array $data 数据
184
+ * @return $this
185
+ */
186
+ public function data (array $ data )
187
+ {
188
+ // 数据验证
189
+ $ data = $ this ->validate ($ data );
190
+ foreach ($ this ->getEntityProperties () as $ field ) {
191
+ $ this ->$ field = $ data [$ field ] ?? null ;
192
+ }
193
+ return $ this ;
194
+ }
195
+
166
196
/**
167
197
* 获取视图模型数据
168
198
*
169
199
* @return array
170
200
*/
171
201
protected function getData (): array
172
202
{
173
- $ reflection = new ReflectionClass ($ this );
174
- $ data = [];
175
- foreach ($ reflection ->getProperties (ReflectionProperty::IS_PUBLIC ) as $ property ) {
176
- $ field = $ property ->getName ();
203
+ $ data = [];
204
+ foreach ($ this ->getEntityProperties () as $ field ) {
177
205
$ data [$ field ] = $ this ->$ field ;
178
206
}
179
207
return $ data ;
@@ -299,7 +327,7 @@ public function bindRelationAttr($model, $bind, $relation)
299
327
protected function convertData (): array
300
328
{
301
329
// 获取实体属性
302
- $ properties = $ this ->getEntityProperties ();
330
+ $ properties = $ this ->getEntityPropertiesMap ();
303
331
$ mapping = $ this ->getOption ('autoMappingAlias ' , []);
304
332
$ data = $ this ->getData ();
305
333
$ item = [];
0 commit comments