Skip to content
This repository was archived by the owner on Jul 31, 2018. It is now read-only.

Commit a89de82

Browse files
committed
Fix #26. Fix small issues + fix documentation + add browse and browseFrom + Change perEnvironment to static
1 parent 6607f3f commit a89de82

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ class Contact extends Model
188188
{
189189
use AlgoliaEloquentTrait;
190190

191-
public $perEnvironment = true; // Index name will be 'Contacts_{\App::environnement()}';
191+
public static $perEnvironment = true; // Index name will be 'contacts_{\App::environnement()}';
192192
}
193193
```
194194

195195
#### Custom `objectID`
196196

197-
By default, the `objectID` is based on your record's keyName (`id` by default). You can change this behavior specifying the `object_id_key` option (be sure to use a uniq field).
197+
By default, the `objectID` is based on your record's keyName (`id` by default). You can change this behavior specifying the `objectIdKey` option (be sure to use a uniq field).
198198

199199
```php
200200
use Illuminate\Database\Eloquent\Model;
@@ -203,7 +203,7 @@ class Contact extends Model
203203
{
204204
use AlgoliaEloquentTrait;
205205

206-
public $objectIdKey = 'new_key';
206+
public static $objectIdKey = 'new_key';
207207
}
208208
```
209209

@@ -229,7 +229,7 @@ class Contact extends Model
229229

230230
#### Manual Indexing
231231

232-
You can trigger indexing using the `pushToindex()` instance method.
232+
You can trigger indexing using the `pushToindex` instance method.
233233

234234
```php
235235
$contact = Contact::firstOrCreate(['name' => 'Jean']);
@@ -238,7 +238,7 @@ $contact->pushToindex();
238238

239239
#### Manual Removal
240240

241-
And trigger index removing using the `removeFromIndex()` instance method.
241+
And trigger index removing using the `removeFromIndex` instance method.
242242

243243
```php
244244
$contact = Contact::firstOrCreate(['name' => 'Jean']);
@@ -260,7 +260,7 @@ Contact::reindex(false);
260260

261261
#### Clearing an Index
262262

263-
To clear an index, use the `clear_index!` class method:
263+
To clear an index, use the `clearIndices` class method:
264264

265265
```ruby
266266
Contact::clearIndices();
@@ -316,7 +316,7 @@ Book.search('foo bar', ['index' => 'contacts_desc']);
316316

317317
## Target Multiple Indexes
318318

319-
You can index a record in several indexes using the <code>add_index</code> method:
319+
You can index a record in several indexes using the <code>indexOnly</code> method:
320320

321321
```php
322322
use Illuminate\Database\Eloquent\Model;

src/AlgoliaEloquentTrait.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,57 @@ public function _clearIndices()
6060
}
6161
}
6262

63+
/**
64+
* @param $query
65+
* @param array $parameters
66+
* @param $cursor
67+
*
68+
* @return mixed
69+
*/
70+
public function _browseFrom($query, $parameters = [], $cursor = null)
71+
{
72+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
73+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
74+
75+
$index = null;
76+
77+
if (isset($parameters['index'])) {
78+
$index = $modelHelper->getIndex($parameters['index']);
79+
unset($parameters['index']);
80+
} else {
81+
$index = $modelHelper->getIndices($this)[0];
82+
}
83+
84+
$result = $index->browseFrom($query, $parameters, $cursor);
85+
86+
return $result;
87+
}
88+
89+
/**
90+
* @param $query
91+
* @param array $parameters
92+
*
93+
* @return mixed
94+
*/
95+
public function _browse($query, $parameters = [])
96+
{
97+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
98+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
99+
100+
$index = null;
101+
102+
if (isset($parameters['index'])) {
103+
$index = $modelHelper->getIndex($parameters['index']);
104+
unset($parameters['index']);
105+
} else {
106+
$index = $modelHelper->getIndices($this)[0];
107+
}
108+
109+
$result = $index->browse($query, $parameters);
110+
111+
return $result;
112+
}
113+
63114
/**
64115
* @param $query
65116
* @param array $parameters
@@ -80,7 +131,7 @@ public function _search($query, $parameters = [])
80131
$index = $modelHelper->getIndices($this)[0];
81132
}
82133

83-
$result = $index->search($query, ['hitsPerPage' => 0]);
134+
$result = $index->search($query, $parameters);
84135

85136
return $result;
86137
}

src/ModelHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getObjectId(Model $model)
5151

5252
public function getObjectIdKey(Model $model)
5353
{
54-
return property_exists($model, 'objectIdKey') ? $model->objectIdKey : $model->getKeyName();
54+
return property_exists($model, 'objectIdKey') ? $model::$objectIdKey : $model->getKeyName();
5555
}
5656

5757
public function getSettings(Model $model)
@@ -77,7 +77,7 @@ public function getIndices(Model $model)
7777
$indicesName[] = $this->getIndexName($model);
7878
}
7979

80-
$env_suffix = property_exists($model, 'perEnvironment') && $model->perEnvironment === true ? '_'.\App::environment() : '';
80+
$env_suffix = property_exists($model, 'perEnvironment') && $model::$perEnvironment === true ? '_'.\App::environment() : '';
8181

8282
$indices = array_map(function ($index_name) use ($env_suffix) {
8383
return $this->algolia->initIndex($index_name.$env_suffix);
@@ -96,7 +96,7 @@ public function getIndicesTmp(Model $model)
9696
$indicesName[] = $this->getIndexName($model);
9797
}
9898

99-
$env_suffix = property_exists($model, 'perEnvironment') && $model->perEnvironment === true ? '_'.\App::environment() : '';
99+
$env_suffix = property_exists($model, 'perEnvironment') && $model::$perEnvironment === true ? '_'.\App::environment() : '';
100100

101101
$indices = array_map(function ($index_name) use ($env_suffix) {
102102
return $this->algolia->initIndex($index_name.$env_suffix.'_tmp');

tests/Models/Model4.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ class Model4 extends Model
99
{
1010
use AlgoliaEloquentTrait;
1111

12-
public $objectIdKey = 'id3';
12+
public static $objectIdKey = 'id3';
1313

1414
protected $primaryKey = 'id2';
1515

16-
public $perEnvironment = false;
16+
public static $perEnvironment = false;
1717

1818
public function __construct()
1919
{

tests/Models/Model5.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class Model5 extends Model
99
{
1010
use AlgoliaEloquentTrait;
1111

12-
public $perEnvironment = true;
12+
public static $perEnvironment = true;
1313
}

0 commit comments

Comments
 (0)