PHP code example of mrssoft / yii2-link-relative

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

    

mrssoft / yii2-link-relative example snippets


    echo LinkRelativeWidget::widget([
        'model' => $model, //Базовая модель
        'relationName' => 'items', //Название отношения (конечного)
        'attributeTitle' => 'title', //Атрибут для вывода в таблице
        'viaToElementName' => 'item',
        'ajaxUrl' => ['item/search'], //Ссылка на поиск элементов
        'placeholder' => 'Поиск товара...',
        'name' => 'Товар' //Название элементов в таблице
    ]);

    public function behaviors()
    {
        return [
            [
                'class' => SaveRelationBehavior::class,
                'relationName' => 'items'
            ],
        ];
    }

    public function actionSearch($search = null)
    {
        $out = ['results' => ['id' => '', 'text' => '']];

        if ($search !== null) {
            $data = Item::find()
                        ->select(['CONCAT(`type`, " ", `name`) as text', 'id'])
                        ->andWhere(['like', 'name', $search])
                        ->orWhere(['like', 'type', $search])
                        ->orderBy('text')
                        ->limit(20)
                        ->asArray()
                        ->all();
            $out['results'] = array_values($data);
        }

        Yii::$app->response->format = Response::FORMAT_JSON;
        return $out;
    }

php composer.phar