PHP code example of fduch2k / yii-flagged-activerecord

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

    

fduch2k / yii-flagged-activerecord example snippets


class Article extends TSFlaggedActiveRecord 
{
    //...
    // By default flag field has name 'flags', you can override it 
    // if your prefer other name
    
    // public $flagsField = 'flags';

    // By default flags values without specified bit computed automatically 
    // (draft => 1, published => 2, deleted => 128)
    public function flags() 
    {
        return array(
            'draft',
            'published',
            'deleted' => 128
        );
    }
    
    // Flag labels uses in interface messages
    // By default an flag label is generated using
    // CModel::generateAttributeLabel
    public function flagLabels()
    {
        return array(
            'deleted'=>'Removed'
        );
    }

}


// Find all published articles
$articles = Article::model()->published()->findAll();

// or all drafts
$articles = Article::model()->withFlag('draft')->findAll();

// or deleted drafts
$articles = Article::model()->withFlag('draft, deleted')->findAll();

// or not deleted
$articles = Article::model()->withoutFlag('deleted')->findAll();

$article = Article::model()->findByPk(10);
// Check if article is not deleted...
if ($article->isDeleted === false) {
    //...then publish it
    $article->isPublished = true;
}
$article->save();

echo Article::model()->getFlag('deleted'); // outputs 128

// get criteria to find not deleted article drafts
$criteria = Article::model()->applyFlags(new CDbCriteria(), array('draft', '!deleted'));

php composer.phar