<commit_before><?php
/**
 * Created by PhpStorm.
 * User: artemshmanovsky
 * Date: 12.03.15
 * Time: 1:52
 */

namespace v0lume\yii2\metaTags;

use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;

use v0lume\yii2\metaTags\models\MetaTag;

class MetaTagBehavior extends Behavior
{
    private $_model = null;


    public function events()
    {
        return [
            ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
            ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
            ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
        ];
    }


    public function afterSave($event)
    {
        $attributes = Yii::$app->request->post('MetaTag', Yii::$app->request->get('MetaTag', null) );

        if($attributes)
        {
            $model = $this->getModel();

            if(!isset($model))
                $model = new MetaTag();

            $attributes['model_id'] = $this->owner->id;
            $attributes['model']  = (new \ReflectionClass($this->owner))->getShortName();

            $model->attributes = $attributes;
            $model->save();
        }
    }


    public function afterDelete($event)
    {
        MetaTag::deleteAll([
            'model_id' => $this->owner->id,
            'model'  => (new \ReflectionClass($this->owner))->getShortName()
        ]);
    }


    public function getModel()
    {
        $model = MetaTag::findOne([
            'model_id' => $this->owner->id,
            'model'  => (new \ReflectionClass($this->owner))->getShortName()
        ]);

        $this->_model = $model;

        return $model;
    }

<commit_msg>fix empty model (if data added manually to db)<commit_after><?php
/**
 * Created by PhpStorm.
 * User: artemshmanovsky
 * Date: 12.03.15
 * Time: 1:52
 */

namespace v0lume\yii2\metaTags;

use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;

use v0lume\yii2\metaTags\models\MetaTag;

class MetaTagBehavior extends Behavior
{
    private $_model = null;


    public function events()
    {
        return [
            ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
            ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
            ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
        ];
    }


    public function afterSave($event)
    {
        $attributes = Yii::$app->request->post('MetaTag', Yii::$app->request->get('MetaTag', null) );

        if($attributes)
        {
            $model = $this->getModel();

            if(!isset($model))
                $model = new MetaTag();

            $attributes['model_id'] = $this->owner->id;
            $attributes['model']  = (new \ReflectionClass($this->owner))->getShortName();

            $model->attributes = $attributes;
            $model->save();
        }
    }


    public function afterDelete($event)
    {
        MetaTag::deleteAll([
            'model_id' => $this->owner->id,
            'model'  => (new \ReflectionClass($this->owner))->getShortName()
        ]);
    }


    public function getModel()
    {
        $model = MetaTag::findOne([
            'model_id' => $this->owner->id,
            'model'  => (new \ReflectionClass($this->owner))->getShortName()
        ]);

        if($model == null)
            $model = new MetaTag();

        $this->_model = $model;

        return $model;
    }


{'fraction_non_alphanumeric': 0.15631626779763708, 'fraction_numerical': 0.007876401090578612, 'mean_word_length': 2.153772683858644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5857618', 'n_tokens_mistral': 1039, 'n_tokens_neox': 995, 'n_words': 177}