Created
June 8, 2017 15:27
-
-
Save markstory/dd755d6dcac8de8af5eb99ee6bf25fd1 to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace App\Model\Behavior; | |
use Cake\ORM\Behavior; | |
class AlgoliaBehavior extends Behavior | |
{ | |
protected $_defaultConfig = [ | |
'index' => '', | |
// Other Algolia API key configuration | |
]; | |
public function afterSave($event, $entity) | |
{ | |
// use client from tutorial link assuming that the index is set. | |
$client = $this->getAlgoliaClient(); | |
// push to algolia | |
$client->saveObject($entity->toArray()); | |
} | |
public function afterDelete($event, $entity) | |
{ | |
// use client from tutorial link assuming that the index is set. | |
$client = $this->getAlgoliaClient(); | |
// Find the model's primary key, assuming a non-composite pk | |
$schema = $this->getTable()->getPrimaryKey(); | |
// Remove from algolia | |
$client->deleteObject($entity->get($primaryKey)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works great ;)