PHP code example of umn / yii2-algolia

1. Go to this page and download the library: Download umn/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/ */

    

umn / yii2-algolia example snippets


use leinonen\Yii2Algolia\AlgoliaComponent;
...
'bootstrap' => ['algolia'],
'components' => [
    'algolia' => [
        'class' => AlgoliaComponent::class,
        'applicationId' => 'test',
        'apiKey' => 'secret',
    ],
],


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]);

$contact = Contact::findOne(['name' => 'test');
$contact->removeFromIndices();

$contact = Contact::findOne(['name' => 'test']);
$manager->removeFromIndices($contact);

$contacts = Contact::find()->where(['type' => Contact::TYPE_AWESOME])->all();
$manager->removeMultipleFromIndices($contacts);

$contact = Contact::findOne(['name' => 'test']);
$contact->updateInIndices();

$contact = Contact::findOne(['name' => 'test']);
$manager->updateInIndices($contact);

$contacts = Contact::find()->where(['type' => Contact::TYPE_AWESOME])->all();
foreach($contacts as $contact) {
  $contact->type = Contact::TYPE_NOT_SO_AWESOME;
}
$manager->updateMultipleInIndices($contacts);

$manager->reindex(Contact::class);

Contact::reindex();

 $contactsQuery = Contact::find()->joinWith('company')->where(['company_name' => 'Algolia']);
 $manager->reindexByActiveQuery($contactsQuery);
 

 class Contact extends ActiveRecord implements SearchableInterface
 {
    use Searchable;
    
    /**
     * {@inheritdoc}
     */
    public function getAlgoliaRecord()
    {
        $record = $this->toArray();
        
        if($this->isRelationPopulated('company')) {
            $record['company'] = $this->company->toArray();
        }
        
        return $record;
    }
 }

 $contacts = Contact::find()->where(['type' => Contact::TYPE_AWESOME])->all();
 $manager->reindexOnly($contacts);
 

$manager->clearIndices(Contact::class);

Contact::clearIndices();

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,
        ],
    ];
}

$manager->search(Contact::class, 'John Doe');

$manager->search(Contact::class, 'John Doe', ['hitsPerPage' => 2, 'attributesToRetrieve' => 'name,address']);

Contact::search('John Doe');
Contact::search('John Doe', ['hitsPerPage' => 2, 'attributesToRetrieve' => 'name,address']);

use leinonen\Yii2Algolia\AlgoliaComponent;
...
'bootstrap' => ['algolia'],
'components' => [
    'algolia' => [
        'class' => AlgoliaComponent::class,
        'applicationId' => 'test',
        'apiKey' => 'secret',
        'env' => YII_ENV
    ],
],