Skip to content

Commit f350136

Browse files
committed
rm boot method
1 parent e7e9231 commit f350136

File tree

2 files changed

+50
-63
lines changed

2 files changed

+50
-63
lines changed

src/Model.php

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,62 @@
22

33
namespace Basemkhirat\Elasticsearch;
44

5-
use Basemkhirat\Elasticsearch\Query;
6-
use Illuminate\Support\Collection;
5+
use ArrayAccess;
76

87
/**
98
* Elasticsearch data model
109
* Class Model
11-
*
1210
* @package Basemkhirat\Elasticsearch
1311
*/
14-
class Model
12+
class Model implements ArrayAccess
1513
{
1614

1715
/**
1816
* Model connection name
19-
*
2017
* @var string
2118
*/
2219
protected $connection;
2320

2421
/**
2522
* Model index name
26-
*
2723
* @var string
2824
*/
2925
protected $index;
3026

3127
/**
3228
* Model type name
33-
*
3429
* @var string
3530
*/
3631
protected $type;
3732

3833
/**
3934
* Attribute data type
40-
*
4135
* @available boolean, bool, integer, int, float, double, string, array, object, null
4236
* @var array
4337
*/
4438
protected $casts = [];
4539

4640
/**
4741
* Model attributes
48-
*
4942
* @var array
5043
*/
5144
protected $attributes = [];
5245

5346

5447
/**
5548
* Model flag indicates row exists in database
56-
*
5749
* @var bool
5850
*/
5951
protected $exists = false;
6052

6153
/**
6254
* Additional custom attributes
63-
*
6455
* @var array
6556
*/
6657
protected $appends = [];
6758

6859
/**
6960
* Allowed casts
70-
*
7161
* @var array
7262
*/
7363
private $castTypes = [
@@ -86,9 +76,8 @@ class Model
8676

8777
/**
8878
* Create a new Elasticsearch model instance.
89-
*
9079
* @param array $attributes
91-
* @param bool $exists
80+
* @param bool $exists
9281
*/
9382
function __construct($attributes = [], $exists = false)
9483
{
@@ -101,7 +90,6 @@ function __construct($attributes = [], $exists = false)
10190

10291
/**
10392
* Get current connection
104-
*
10593
* @return string
10694
*/
10795
public function getConnection()
@@ -111,7 +99,7 @@ public function getConnection()
11199

112100
/**
113101
* Set current connection
114-
*
102+
* @param $connection
115103
* @return void
116104
*/
117105
public function setConnection($connection)
@@ -131,7 +119,7 @@ public function getIndex()
131119

132120
/**
133121
* Set index name
134-
*
122+
* @param $index
135123
* @return void
136124
*/
137125
public function setIndex($index)
@@ -141,7 +129,6 @@ public function setIndex($index)
141129

142130
/**
143131
* Get type name
144-
*
145132
* @return string
146133
*/
147134
public function getType()
@@ -151,7 +138,7 @@ public function getType()
151138

152139
/**
153140
* Set type name
154-
*
141+
* @param $type
155142
* @return void
156143
*/
157144
public function setType($type)
@@ -161,9 +148,7 @@ public function setType($type)
161148

162149
/**
163150
* Magic getter for model properties
164-
*
165151
* @param $name
166-
*
167152
* @return null
168153
*/
169154
public function __get($name)
@@ -191,9 +176,7 @@ public function __get($name)
191176

192177
/**
193178
* Get original model attribute
194-
*
195179
* @param $name
196-
*
197180
* @return mixed
198181
*/
199182
protected function getOriginalAttribute($name)
@@ -205,9 +188,7 @@ protected function getOriginalAttribute($name)
205188

206189
/**
207190
* Get Appends model attribute
208-
*
209191
* @param $name
210-
*
211192
* @return mixed
212193
*/
213194
protected function getAppendsAttribute($name)
@@ -219,10 +200,8 @@ protected function getAppendsAttribute($name)
219200

220201
/**
221202
* Set attributes casting
222-
*
223203
* @param $name
224204
* @param $value
225-
*
226205
* @return mixed
227206
*/
228207
protected function setAttributeType($name, $value)
@@ -239,7 +218,6 @@ protected function setAttributeType($name, $value)
239218

240219
/**
241220
* Get model as array
242-
*
243221
* @return array
244222
*/
245223
public function toArray()
@@ -260,9 +238,7 @@ public function toArray()
260238

261239
/**
262240
* Get the collection of items as JSON.
263-
*
264241
* @param int $options
265-
*
266242
* @return string
267243
*/
268244
public function toJson($options = 0)
@@ -272,10 +248,8 @@ public function toJson($options = 0)
272248

273249
/**
274250
* Handle model properties setter
275-
*
276251
* @param $name
277252
* @param $value
278-
*
279253
* @return null
280254
*/
281255
public function __set($name, $value)
@@ -298,12 +272,11 @@ public function __set($name, $value)
298272

299273
/**
300274
* Create a new model query
301-
*
302-
* @return mixed
275+
* @return Query
303276
*/
304277
protected function newQuery()
305278
{
306-
$query = app("es")->setModel($this);
279+
$query = app("es")->connectionByName()->setModel($this);
307280

308281
$query->connection($this->getConnection());
309282

@@ -320,7 +293,7 @@ protected function newQuery()
320293

321294
/**
322295
* Get all model records
323-
*
296+
* @throws \Exception
324297
* @return mixed
325298
*/
326299
public static function all()
@@ -334,9 +307,8 @@ public static function all()
334307

335308
/**
336309
* Get model by key
337-
*
338310
* @param $key
339-
*
311+
* @throws \Exception
340312
* @return mixed
341313
*/
342314
public static function find($key)
@@ -354,7 +326,6 @@ public static function find($key)
354326

355327
/**
356328
* Delete model record
357-
*
358329
* @return $this|bool
359330
*/
360331
function delete()
@@ -373,7 +344,6 @@ function delete()
373344

374345
/**
375346
* Save data to model
376-
*
377347
* @return string
378348
*/
379349
public function save()
@@ -415,7 +385,6 @@ public function save()
415385

416386
/**
417387
* Check model is exists
418-
*
419388
* @return bool
420389
*/
421390
function exists()
@@ -425,7 +394,6 @@ function exists()
425394

426395
/**
427396
* Get model key
428-
*
429397
* @return mixed
430398
*/
431399
function getID()
@@ -434,20 +402,50 @@ function getID()
434402
}
435403

436404
/**
437-
* The "booting" method of the model.
438-
*
405+
* Determine if an item exists at an offset.
406+
* @param mixed $key
407+
* @return bool
408+
*/
409+
public function offsetExists($key)
410+
{
411+
return array_key_exists($key, $this->attributes) or in_array($key, $this->appends) or property_exists($this, $key);
412+
}
413+
414+
/**
415+
* Get an item at a given offset.
416+
* @param mixed $key
417+
* @return mixed
418+
*/
419+
public function offsetGet($key)
420+
{
421+
return $this->__get($key);
422+
}
423+
424+
/**
425+
* Set the item at a given offset.
426+
* @param mixed $key
427+
* @param mixed $value
428+
* @return void
429+
*/
430+
public function offsetSet($key, $value)
431+
{
432+
$this->__set($key, $value);
433+
}
434+
435+
/**
436+
* Unset the item at a given offset.
437+
* @param string $key
439438
* @return void
440439
*/
441-
public function boot($query)
440+
public function offsetUnset($key)
442441
{
442+
unset($this->attributes[$key]);
443443
}
444444

445445
/**
446446
* Handle dynamic static method calls into the method.
447-
*
448447
* @param string $method
449-
* @param array $parameters
450-
*
448+
* @param array $parameters
451449
* @return mixed
452450
*/
453451
public static function __callStatic($method, $parameters)
@@ -457,10 +455,8 @@ public static function __callStatic($method, $parameters)
457455

458456
/**
459457
* Handle dynamic method calls into the model.
460-
*
461458
* @param string $method
462-
* @param array $parameters
463-
*
459+
* @param array $parameters
464460
* @return mixed
465461
*/
466462
public function __call($method, $parameters)

src/Query.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,6 @@ public function query()
743743
$query["type"] = $this->getType();
744744
}
745745

746-
if ($this->model) {
747-
$this->model->boot($this);
748-
}
749-
750746
$query["body"] = $this->getBody();
751747

752748
$query["from"] = $this->getSkip();
@@ -917,13 +913,9 @@ protected function getAll($result = [])
917913

918914
foreach ($result["hits"]["hits"] as $row) {
919915

920-
if($this->model){
921-
$model = new $this->model($row["_source"], true);
922-
}else{
923-
$model = new Model($row["_source"], true);
924-
}
916+
$model = $this->model ? new $this->model($row["_source"], true) : new Model($row["_source"], true);
925917

926-
$model->setConnection($model->getConnection() ? $model->getConnection() : $this->connection);
918+
$model->setConnection($model->getConnection());
927919
$model->setIndex($row["_index"]);
928920
$model->setType($row["_type"]);
929921

@@ -965,12 +957,11 @@ protected function getFirst($result = [])
965957
$model = new $this->model($data[0]["_source"], true);
966958
} else {
967959
$model = new Model($data[0]["_source"], true);
960+
$model->setConnection($model->getConnection());
968961
$model->setIndex($data[0]["_index"]);
969962
$model->setType($data[0]["_type"]);
970963
}
971964

972-
$model->setConnection($model->getConnection() ? $model->getConnection() : $this->connection);
973-
974965
// match earlier version
975966

976967
$model->_index = $data[0]["_index"];

0 commit comments

Comments
 (0)