Created
November 14, 2013 06:26
-
-
Save derekperkins/7462343 to your computer and use it in GitHub Desktop.
CakePHP ElasticsearchBehavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ElasticsearchBehavior extends ModelBehavior { | |
public function beforeFind(Model $Model, $query) { | |
if(!empty($query['conditions']['elastic'])) { | |
$Model->switchToElastic(); | |
unset($query['conditions']['elastic']); | |
} | |
return $query; | |
} | |
public function afterFind(Model $Model, $results, $primary = false) { | |
if($Model->isElastic()) { | |
$Model->esNormalize($results); | |
$Model->switchToDatabase(); | |
} | |
return $results; | |
} | |
public function afterSave(Model $Model, $created, $options = []) { | |
$esData = $Model->esDenormalize($Model->data); | |
$esModel = new $Model->alias(); | |
$esModel->switchToElastic(); | |
$esModel->create(); | |
$esModel->save($esData, ['callbacks' => 'false']); | |
$esModel->switchToDatabase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment