<commit_before><?php


namespace asdfstudio\eav;


use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\Json;

/**
 * Class EavBehavior
 * @package asdfstudio\eav
 * @property ActiveRecord $owner
 */
class EavBehavior extends Behavior
{
    /**
     * EAV properties
     * @var array
     */
    public $properties = [];
    /**
     * Primary key for getting extended attributes
     * @var string
     */
    public $primaryKey = 'id';
    /**
     * Properties key for getting extended attributes
     * @var string
     */ 
    public $propertiesKey = 'id';
    /**
     * Properties field with attributes name
     * @var string
     */ 
    public $propertiesName = 'name';
    /**
     * Properties field with attributes value
     * @var string
     */ 
    public $propertiesValue = 'value';
    /**
     * Table name for storing extended attributes
     * @var string
     */
    public $tableName = null;

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

    /**
     * Load all EAV of model
     */
    public function afterFind()
    {
        $properties = (object) [];
        $query = new Query();
        $query->select('name, value');
        $query->from($this->tableName);
        $query->where([$this->primaryKey => $this->owner->{$this->primaryKey}]);

        foreach ($query->all() as $property) {
            if(!empty($property['name'])){
                $properties->{$property['name']} = $property['value'];
            }
        }
        $this->properties = $properties;
    }

    /**
     * Delete all EAV related to model
     */
    protected function deleteAll()
    {
        Yii::$app->db->createCommand()
            ->delete($this->tableName, [$this->primaryKey => $this->owner->{$this->primaryKey}])
            ->execute();
    }

    /**
     * Save all EAV
     */
    public function afterSave()
    {
        $this->deleteAll();
        $properties = [];
        if($this->properties){
            foreach ($this->properties as $name => $value) {
                $properties[] = [
                    $this->propertiesKey => $this->owner->{$this->primaryKey},
                    $this->propertiesName => $name,
                    $this->propertiesValue => $value,
                ];
            }
            Yii::$app->db->createCommand()
                ->batchInsert($this->tableName, [$this->propertiesKey => $this->propertiesKey, $this->propertiesName => $this->propertiesName, $this->propertiesValue => $this->propertiesValue], $properties)
                ->execute();
        }
    }

    /**
     * Delete all EAV
     */
    public function afterDelete()
    {
        $this->deleteAll();
    }
}<commit_msg>Fix error  when property not set<commit_after><?php


namespace asdfstudio\eav;


use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\Json;

/**
 * Class EavBehavior
 * @package asdfstudio\eav
 * @property ActiveRecord $owner
 */
class EavBehavior extends Behavior
{
    /**
     * EAV properties
     * @var array
     */
    public $properties = [];
    /**
     * Primary key for getting extended attributes
     * @var string
     */
    public $primaryKey = 'id';
    /**
     * Properties key for getting extended attributes
     * @var string
     */ 
    public $propertiesKey = 'id';
    /**
     * Properties field with attributes name
     * @var string
     */ 
    public $propertiesName = 'name';
    /**
     * Properties field with attributes value
     * @var string
     */ 
    public $propertiesValue = 'value';
    /**
     * Table name for storing extended attributes
     * @var string
     */
    public $tableName = null;

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

    /**
     * Load all EAV of model
     */
    public function afterFind()
    {
        $properties = (object) [];
        $query = new Query();
        $query->select('name, value');
        $query->from($this->tableName);
        $query->where([$this->primaryKey => $this->owner->{$this->primaryKey}]);

        foreach ($query->all() as $property) {
            if(!empty($property['name'])){
                $properties->{$property['name']} = $property['value'];
            }
        }
        $this->properties = $properties;
    }

    /**
     * Delete all EAV related to model
     */
    protected function deleteAll()
    {
        Yii::$app->db->createCommand()
            ->delete($this->tableName, [$this->primaryKey => $this->owner->{$this->primaryKey}])
            ->execute();
    }

    /**
     * Save all EAV
     */
    public function afterSave()
    {
        $this->deleteAll();
        $properties = [];
        if($this->properties){
            foreach ($this->properties as $name => $value) {
                $properties[] = [
                    $this->propertiesKey => $this->owner->{$this->primaryKey},
                    $this->propertiesName => $name,
                    $this->propertiesValue => $value,
                ];
            }
            if($properties){
                Yii::$app->db->createCommand()
                ->batchInsert($this->tableName, [$this->propertiesKey => $this->propertiesKey, $this->propertiesName => $this->propertiesName, $this->propertiesValue => $this->propertiesValue], $properties)
                ->execute(); 
            }
        }
    }

    /**
     * Delete all EAV
     */
    public function afterDelete()
    {
        $this->deleteAll();
    }
}
{'fraction_non_alphanumeric': 0.14937552039966695, 'fraction_numerical': 0.0, 'mean_word_length': 1.921206225680934, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, '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': '1148292', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1642, 'n_words': 354}