PHP code example of kak / behaviors

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

    

kak / behaviors example snippets


    use kak\models\behaviors\MaterializedPathBehavior

    // ...

    // step 1
     public function behaviors()
     {
         return [
            [
                'class' => MaterializedPathBehavior::class
            ],
         ];
     }
     // step 2
      /**
      * Query factory
      * @return MaterializedPathQuery
      */
     public static function find()
     {
         return new MaterializedPathQuery(get_called_class());
     }

/**
Class MaterializedPathQuery
 * @return bool
 */
class MaterializedPathQuery extends ActiveQuery
{
    /**
     * Get root nodes
     * @return MaterializedPathModel
     */
    public function getChildren($path)
    {
        /** @var ActiveQuery $this */
        $this->andWhere(['path' => '.'.$path.'%']);
        return $this;
    }
} 

    $categoryModel = new Category(['parent_id' => 123 ]);
    $categoryModel->save();

use kak\models\behaviors\SluggableBehavior;

     public function behaviors()
     {
         return [
            [
                 'class' => SluggableBehavior::class,
                 'attribute' => 'title',  
                 'slugAttribute' => 'slug',           
                 // 'replacements' => [ '_' => '-']   
                 // 'limit' => '100'                  // truncate text
                 // 'delimiter' => '-'                // default delimiter
             ]
         ];
     }

use kak\models\behaviors\CallbackBehavior;

// ...

    public function behaviors()
    {
        return [
            'class' => CallbackBehavior::class,
            'params' => [
                'operators' => [
                    'attribute' => 'operator_list',
                    // implode/explode method
                    'method' => CallbackBehavior::METHOD_STRING,
                    'delimiter' => '|', // set other  delimiter only METHOD_STRING
                    // or json_encode/json_decode method
                    'method' => CallbackBehavior::METHOD_JSON,
                    // or custom callback
                    'set' => function ($value) {
                        return implode(',', $value);
                    },
                    'get' => function ($value) {
                        return explode(',', $value);
                    },
                ],
            ],
        ];
    }

use kak\models\behaviors\UUIDBehavior;

// ...

 public function behaviors()
 {
     return [
         [
             'class' => UUIDBehavior::class,
             'createViaDb' => true   // is use postgress
         ],
      ;
  }



php composer.phar