PHP code example of voycey / cakephp-sphinxsearch

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

    

voycey / cakephp-sphinxsearch example snippets


 
class PostsTable extends Table
{

    /**
     * Initialize method
     *
     * @param  array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('posts');
        $this->displayField('title');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');
        $this->addBehavior('Sphinx.Sphinx');
    }
}


public function testBehaviour()
{
    $paginate = [
        'order' => [
            'Posts.id asc'
        ],
        'fields' => [
            'id', 'title', 'user_id'
        ],
        'contain' => [
            'Comments' => [
                'fields' => ['id', 'post_id']
            ],
            'Categories' => [
                'fields' => ['id', 'CategoriesPosts.post_id']
            ],
            'Types' => [
                'fields' => ['id', 'name']
            ]
        ]
    ];

    $query = $this->Posts->search([
                                    'index' => 'idx_toolkit', 
                                    'term' => 'Ten', 
                                    'match_fields' => 'title', 
                                    'paginate' => $paginate,
                                    'limit' => 50
                                ]);
    
    $row = $query->first();

    $this->assertInstanceOf('Cake\ORM\Query', $query);
    $this->assertInstanceOf('Cake\ORM\Entity', $row);

}