1. Go to this page and download the library: Download leinonen/yii2-algolia library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use leinonen\Yii2Algolia\AlgoliaManager;
class MyController
{
private $algoliaManager;
public function __construct($id, $module, AlgoliaManager $algoliaManager, $config = [])
{
$this->algoliaManager = $algoliaManager;
parent::__construct($id, $module, $config);
}
public function actionExample()
{
$index = $this->algoliaManager->initIndex("contacts");
$results = $index->search("query string");
}
}
use Yii;
$index = Yii::$app->algolia->initIndex("contacts");
use leinonen\Yii2Algolia\ActiveRecord\Searchable;
use leinonen\Yii2Algolia\SearchableInterface;
use yii\db\ActiveRecord;
class Contact extends ActiveRecord implements SearchableInterface
{
use Searchable;
}
class Contact extends ActiveRecord implements SearchableInterface
{
use Searchable;
/**
* {@inheritdoc}
*/
public function indices()
{
return ['first_index', 'second_index'];
}
}
class Contact extends ActiveRecord implements SearchableInterface
{
use Searchable;
/**
* {@inheritdoc}
*/
public function getAlgoliaRecord()
{
return array_merge($this->toArray(), ['someStaticValue' => "It's easy"]);
}
}
class Contact extends ActiveRecord implements SearchableInterface
{
use Searchable;
/**
* {@inheritdoc}
*/
public function getObjectID()
{
return $this->getUuid();
}
}
$contact = new Contact();
$contact->name = 'test';
$contact->index();
$contact = new Contact();
$contact->name = 'test';
$manager->pushToIndices($contact);
$contact1 = new Contact();
$contact1->name = 'test';
$contact2 = new Contact();
$contact2->name = 'anotherTest';
$manager->pushMultipleToIndices([$contact1, $contact2]);
use leinonen\Yii2Algolia\ActiveRecord\Searchable;
use leinonen\Yii2Algolia\SearchableInterface;
use leinonen\Yii2Algolia\ActiveRecord\SynchronousAutoIndexBehavior;
use yii\db\ActiveRecord;
class Contact extends ActiveRecord implements SearchableInterface
{
use Searchable;
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
SynchronousAutoIndexBehavior::class,
];
}
}
public function behaviors()
{
return [
[
'class' => SynchronousAutoIndexBehavior::class,
'afterInsert' => false,
'afterUpdate' => false,
],
];
}