Skip to content

Commit 49a25f4

Browse files
author
Petr Buchyn
committed
Adding convenient methods to Update write model
1 parent dc28c11 commit 49a25f4

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

src/Write/Model/Update.php

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,208 @@ public function writeToBulk(BulkWrite $bulk)
4242
{
4343
$bulk->update($this->filter, $this->update, $this->options);
4444
}
45+
46+
/**
47+
* @param string $field
48+
* @param array $values
49+
*
50+
* @return $this
51+
*/
52+
public function addAllToSet($field, array $values)
53+
{
54+
$this->addToSet($field, ['$each' => $values]);
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* @param string $field
61+
* @param mixed $value
62+
*
63+
* @return $this
64+
*/
65+
public function addToSet($field, $value)
66+
{
67+
$this->update['$addToSet'][$field] = $value;
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* @param string $field
74+
*
75+
* @return $this
76+
*/
77+
public function popFirst($field)
78+
{
79+
$this->update['$pop'][$field] = -1;
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* @param string $field
86+
*
87+
* @return $this
88+
*/
89+
public function popLast($field)
90+
{
91+
$this->update['$pop'][$field] = 1;
92+
93+
return $this;
94+
}
95+
96+
/**
97+
* @param string $field
98+
* @param array $values
99+
*
100+
* @return $this
101+
*/
102+
public function pullAll($field, array $values)
103+
{
104+
$this->update['$pullAll'][$field] = $values;
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* @param string $field
111+
* @param array|mixed $condition - a condition to specify values to delete, or a value to delete
112+
*
113+
* @return $this
114+
*/
115+
public function pull($field, $condition)
116+
{
117+
$this->update['$pull'][$field] = $condition;
118+
119+
return $this;
120+
}
121+
122+
/**
123+
* @param string $field
124+
* @param mixed $value
125+
*/
126+
public function push($field, $value)
127+
{
128+
$this->update['$push'][$field] = $value;
129+
}
130+
131+
/**
132+
* @param string $field
133+
* @param array $values
134+
*
135+
* @return $this
136+
*/
137+
public function pushAll($field, array $values)
138+
{
139+
$this->push($field, ['$each' => $values]);
140+
141+
return $this;
142+
}
143+
144+
/**
145+
* @param string $field
146+
*/
147+
public function currentDate($field)
148+
{
149+
$this->update['$currentDate'][$field] = true;
150+
}
151+
152+
/**
153+
* @param string $field
154+
*/
155+
public function currentTimestamp($field)
156+
{
157+
$this->update['$currentDate'][$field] = ['$type' => 'timestamp'];
158+
}
159+
160+
/**
161+
* @param string $field
162+
* @param int|float $value
163+
*
164+
* @return $this
165+
*/
166+
public function increment($field, $value)
167+
{
168+
$this->update['$inc'][$field] = $value;
169+
170+
return $this;
171+
}
172+
173+
/**
174+
* @param string $field
175+
* @param int|float $value
176+
*
177+
* @return $this
178+
*/
179+
public function multiply($field, $value)
180+
{
181+
$this->update['$mul'][$field] = $value;
182+
183+
return $this;
184+
}
185+
186+
/**
187+
* @param string $field
188+
* @param int|float $value
189+
*
190+
* @return $this
191+
*/
192+
public function min($field, $value)
193+
{
194+
$this->update['$min'][$field] = $value;
195+
196+
return $this;
197+
}
198+
199+
/**
200+
* @param string $field
201+
* @param int|float $value
202+
*
203+
* @return $this
204+
*/
205+
public function max($field, $value)
206+
{
207+
$this->update['$max'][$field] = $value;
208+
209+
return $this;
210+
}
211+
212+
/**
213+
* @param string $field
214+
* @param mixed $value
215+
*
216+
* @return $this
217+
*/
218+
public function setOnInsert($field, $value)
219+
{
220+
$this->update['$setOnInsert'][$field] = $value;
221+
222+
return $this;
223+
}
224+
225+
/**
226+
* @param string $field
227+
* @param mixed $value
228+
*
229+
* @return $this
230+
*/
231+
public function set($field, $value)
232+
{
233+
$this->update['$set'][$field] = $value;
234+
235+
return $this;
236+
}
237+
238+
/**
239+
* @param string $field
240+
*
241+
* @return $this
242+
*/
243+
public function unsetField($field)
244+
{
245+
$this->update['$unset'][$field] = '';
246+
247+
return $this;
248+
}
45249
}

0 commit comments

Comments
 (0)