1414namespace think \model ;
1515
1616use think \Collection as BaseCollection ;
17+ use think \model \concern \ModelCollection ;
1718use think \model \contract \Modelable as Model ;
1819use think \Paginator ;
1920
2728 */
2829class Collection extends BaseCollection
2930{
30- public function toView (string $ view )
31- {
32- return $ this ->map (function (Model $ model ) use ($ view ) {
33- return $ model ->toView ($ view );
34- });
35- }
36-
31+ use ModelCollection;
3732 /**
3833 * 延迟预载入关联查询.
3934 *
@@ -52,160 +47,6 @@ public function load(array $relation, $cache = false)
5247 return $ this ;
5348 }
5449
55- /**
56- * 删除数据集的数据.
57- *
58- * @return bool
59- */
60- public function delete (): bool
61- {
62- $ this ->each (function (Model $ model ) {
63- $ model ->delete ();
64- });
65-
66- return true ;
67- }
68-
69- /**
70- * 更新数据.
71- *
72- * @param array $data 数据数组
73- * @param array $allowField 允许字段
74- *
75- * @return bool
76- */
77- public function update (array $ data , array $ allowField = []): bool
78- {
79- $ this ->each (function (Model $ model ) use ($ data , $ allowField ) {
80- if (!empty ($ allowField )) {
81- $ model ->allowField ($ allowField );
82- }
83-
84- $ model ->save ($ data );
85- });
86-
87- return true ;
88- }
89-
90- /**
91- * 设置需要隐藏的输出属性.
92- *
93- * @param array $hidden 属性列表
94- * @param bool $merge 是否合并
95- *
96- * @return $this
97- */
98- public function hidden (array $ hidden , bool $ merge = false )
99- {
100- $ this ->each (function (Model $ model ) use ($ hidden , $ merge ) {
101- $ model ->hidden ($ hidden , $ merge );
102- });
103-
104- return $ this ;
105- }
106-
107- /**
108- * 设置需要输出的属性.
109- *
110- * @param array $visible
111- * @param bool $merge 是否合并
112- *
113- * @return $this
114- */
115- public function visible (array $ visible , bool $ merge = false )
116- {
117- $ this ->each (function (Model $ model ) use ($ visible , $ merge ) {
118- $ model ->visible ($ visible , $ merge );
119- });
120-
121- return $ this ;
122- }
123-
124- /**
125- * 设置需要追加的输出属性.
126- *
127- * @param array $append 属性列表
128- * @param bool $merge 是否合并
129- *
130- * @return $this
131- */
132- public function append (array $ append , bool $ merge = false )
133- {
134- $ this ->each (function (Model $ model ) use ($ append , $ merge ) {
135- $ model ->append ($ append , $ merge );
136- });
137-
138- return $ this ;
139- }
140-
141- /**
142- * 设置属性映射.
143- *
144- * @param array $mapping 属性映射
145- *
146- * @return $this
147- */
148- public function mapping (array $ mapping )
149- {
150- $ this ->each (function (Model $ model ) use ($ mapping ) {
151- $ model ->mapping ($ mapping );
152- });
153-
154- return $ this ;
155- }
156-
157- /**
158- * 设置模型输出场景.
159- *
160- * @param string $scene 场景名称
161- *
162- * @return $this
163- */
164- public function scene (string $ scene )
165- {
166- $ this ->each (function (Model $ model ) use ($ scene ) {
167- $ model ->scene ($ scene );
168- });
169-
170- return $ this ;
171- }
172-
173- /**
174- * 设置数据字段获取器.
175- *
176- * @param string|array $name 字段名
177- * @param callable $callback 闭包获取器
178- *
179- * @return $this
180- */
181- public function withAttr (string |array $ name , ?callable $ callback = null )
182- {
183- $ this ->each (function (Model $ model ) use ($ name , $ callback ) {
184- $ model ->withFieldAttr ($ name , $ callback );
185- });
186-
187- return $ this ;
188- }
189-
190- /**
191- * 绑定(一对一)关联属性到当前模型.
192- *
193- * @param string $relation 关联名称
194- * @param array $attrs 绑定属性
195- *
196- * @throws Exception
197- *
198- * @return $this
199- */
200- public function bindAttr (string $ relation , array $ attrs = [])
201- {
202- $ this ->each (function (Model $ model ) use ($ relation , $ attrs ) {
203- $ model ->bindAttr ($ relation , $ attrs );
204- });
205-
206- return $ this ;
207- }
208-
20950 /**
21051 * 按指定键整理数据.
21152 *
0 commit comments