Skip to content

Commit 2e6017d

Browse files
committed
Playing with iterators and results from elastic search
1 parent 0898f25 commit 2e6017d

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

Controller/AppController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class AppController extends Controller {
3838
],
3939
'Crud.Crud' => array(
4040
'actions' => array('index', 'add', 'view', 'edit', 'delete'),
41-
'listeners' => array('Api', 'ApiQueryLog')
42-
)
41+
'listeners' => array('Api', 'ApiQueryLog', 'ApiPagination')
42+
),
43+
'Paginator' => array('settings' => ['paramType' => 'querystring'])
4344
);
4445

4546
}

Controller/UsersController.php

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
App::uses('AppController', 'Controller');
33
App::uses('CakeEvent', 'Event');
4+
App::uses('FavoriteIterator', 'Iterator');
5+
App::uses('SecurityIterator', 'Iterator');
6+
App::uses('ListIterator', 'Iterator');
47

58
/**
69
* Users Controller
@@ -11,6 +14,18 @@ class UsersController extends AppController {
1114
public function index() {
1215
$this->Crud->on('beforePaginate', function() {
1316
$this->User->switchToElastic();
17+
$this->Paginator->settings['limit'] = 20;
18+
if ($this->request->query('name')) {
19+
$this->Paginator->settings['query'] = [
20+
'match' => ['_all' => $this->request->query('name')]
21+
];
22+
}
23+
});
24+
$this->Crud->on('afterPaginate', function($event) {
25+
$event->subject()->items = new ListIterator(new SecurityIterator(new FavoriteIterator(
26+
new ArrayIterator($event->subject()->items),
27+
$this->Auth->user('id')
28+
)));
1429
});
1530
return $this->Crud->executeAction();
1631
}

Iterator/FavoriteIterator.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class FavoriteIterator extends IteratorIterator {
4+
5+
protected $_user;
6+
7+
protected $_cache;
8+
9+
public function __construct($results, $userId) {
10+
$this->_user = $userId;
11+
$this->_cache = (array)Cache::read('favorites_' . $userId);
12+
parent::__construct($results);
13+
}
14+
15+
public function current() {
16+
$current = parent::current();
17+
$current['User']['favorite'] = !empty($this->_cache[$current['User']['id']]);
18+
return $current;
19+
}
20+
}

Iterator/ListIterator.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class ListIterator extends IteratorIterator {
4+
5+
public function key() {
6+
return parent::current()['User']['id'];
7+
}
8+
9+
public function current() {
10+
$current = parent::current();
11+
return $current['User']['full_name'] . ' - ' . $current['User']['companies'][0]['country'];
12+
}
13+
}
14+
15+

Iterator/SecurityIterator.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
class SecurityIterator extends IteratorIterator {
4+
5+
public function current() {
6+
$current = parent::current();
7+
unset($current['User']['last_name']);
8+
return $current;
9+
}
10+
}
11+

Model/User.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function elasticMapping() {
1717
return array(
1818
'id' => array('type' => 'string', 'index' => 'not_analyzed'),
1919
'full_name' => array('type' => 'string'),
20-
'first_name' => array('type' => 'string'),
21-
'last_name' => array('type' => 'string'),
20+
'first_name' => array('type' => 'string', 'include_in_all' => false),
21+
'last_name' => array('type' => 'string', 'include_in_all' => false),
2222
'companies' => array(
2323
'type'=> 'object',
2424
'properties' => array(
@@ -29,7 +29,7 @@ public function elasticMapping() {
2929
'state' => array('type' => 'string'),
3030
'country' => array('type' => 'string'),
3131
'zip' => array('type' => 'string'),
32-
'website' => array('type' => 'string', 'length' => 2048),
32+
'website' => array('type' => 'string', 'length' => 2048),
3333
'location' => array('type' => 'geo_point', 'lat_lon' => true),
3434
)
3535
),
@@ -103,7 +103,7 @@ function afterSave($created) {
103103
$esData = $this->esDenormalize($this->data);
104104
$this->switchToElastic();
105105
$this->create();
106-
$this->save($this->data, ['callbacks' => 'false']);
106+
$this->save($esData, ['callbacks' => 'false']);
107107
$this->switchToDatabase();
108108
}
109109

0 commit comments

Comments
 (0)