Skip to content

Instantly share code, notes, and snippets.

@markstory
Created June 8, 2017 15:27
Show Gist options
  • Save markstory/dd755d6dcac8de8af5eb99ee6bf25fd1 to your computer and use it in GitHub Desktop.
Save markstory/dd755d6dcac8de8af5eb99ee6bf25fd1 to your computer and use it in GitHub Desktop.
<?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));
}
}
@be-mohand
Copy link

It works great ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment