PHP code example of macfly / yii2-taxonomy

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

    

macfly / yii2-taxonomy example snippets


'modules' => [
    ................
    'taxonomy' => [
        'class' => 'macfly\taxonomy\Module'
    ],
    ................
],

use macfly\taxonomy\behaviors\BaseTermBehavior;

/**
 * ...
 * @property array $terms
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            BaseTermBehavior::className(),
            ...
            ],
        ];
    }

}

$post = Post::findOne(['id'=>1]);
$post->terms;

$post = Post::findOne(['id'=>1]);
$terms = [
        Term::findOne(1),
        Term::findOne(12),
        ];
$post->terms = $terms;

$term = Term::findOne(['id'=>1]);
$post = Post::findOne(['id'=>1]);
$post->delTerm($term);

$term = Term::findOne(['id'=>1]);
$post = Post::findOne(['id'=>1]);
$post->hasTerm($term);

$term = Term::findOne(['id'=>1]);
$post = Post::findOne(['id'=>1]);
$post->addTerm($term);

use macfly\taxonomy\behaviors\PropertyTermBehavior;

/**
 * ...
 * @property array $terms
 * @property array $envs
 */
class Post extends \yii\db\ActiveRecord
{   
    public function behaviors()
    {   
        return [
                PropertyTermBehavior::className(),
                ...
            ],
        ];
    }

    # Define the get method of property 'env'
    public function getEnvs()
    {
        return $this->getPropertyTerms('env');
    }

    # Define the set method of property 'env'
    public function setEnvs($terms)
    {
        return $this->setPropertyTerms('env', $terms);
    }

    # Add term by name and value
    public function addEnv($name, $value)
    {
        return $this->addPropertyTerm('env', $name, $value);
    }

    # Detach term by name and value
    public function delEnv($name, $value)
    {
        return $this->delPropertyTerm('env', $name, $value);
    }

    # Has term by name and value
    public function hasEnv($name, $value)
    {
        return $this->hasPropertyTerm('env', $name, $value);
    }
}

$post = Post::findOne(['id'=>1]);
$post->envs;

$post = Post::findOne(['id'=>1]);
$terms = [
    Term::findOne(1),
    Term::findOne(12),
  ];
$post->envs = $terms;

$post = Post::findOne(['id'=>1]);
$post->delEnv('PWD', '/home/test/');

$post = Post::findOne(['id'=>1]);
$post->hasEnv('PWD', '/home/test');

$post = Post::findOne(['id'=>1]);
$post->addEnv('PWD', '/home/test');

use macfly\taxonomy\behaviors\TagTermBehavior;

/**
 * ...
 * @property array $terms
 * @property array $tags
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
                TagTermBehavior::className(),
                ...
            ],
        ];
    }

}

use macfly\taxonomy\behaviors\TagTermBehavior;

/**
 * ...
 * @property array $terms
 * @property array $tags
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'tag' => [
                'class' => TagBehavior::className(),
                     'type' => 'myposttagtype',
                     'name' => 'myposttagname',
            ],
        ];
    }
}

$post = Post::findOne(['id'=>1]);
$post->tags;

$post = Post::findOne(['id'=>1]);
$post->tags = ['tags1', 'tag2', 'test'];
// Or
$post->tags = 'tags1,tag2,test';

$post = Post::findOne(['id'=>1]);
$post->delTag('tags1');

$post = Post::findOne(['id'=>1]);
$post->hasTag('test');

$post = Post::findOne(['id'=>1]);
$post->addTag('foo');

'modules' => [
    ................
    'taxonomy' => [
        'class' => 'macfly\taxonomy\Module'
    ],
    ................
],

php composer.phar 

php yii migrate --migrationPath=@vendor/mhndev/yii2-taxonomy-term/src/migrations