<commit_before><?php

namespace kato\behaviors;

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

class DefaultTitle extends Behavior {

    /**
     * @var string attribute
     */
    public $attribute = "title";
    public $defaultPrefix = 'New';

    /**
     * @inheritdoc
     */
    public function events()
    {
        return [ActiveRecord::EVENT_BEFORE_INSERT => 'setDefaultValue'];
    }

    public function setDefaultValue()
    {
        $id = $this->getLastRow()->id + 1;
        $this->owner->{$this->attribute} = $this->defaultPrefix . '-' . $id;
    }

    /**
     * Return last row inserted
     * @return mixed
     */
    public function getLastRow()
    {
        return $this->owner->find()
            ->orderBy('id DESC')
            ->one();
    }
}<commit_msg>Set title only if data is empty<commit_after><?php

namespace kato\behaviors;

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

class DefaultTitle extends Behavior {

    /**
     * @var string attribute
     */
    public $attribute = "title";
    public $defaultPrefix = 'New';

    /**
     * @inheritdoc
     */
    public function events()
    {
        return [ActiveRecord::EVENT_BEFORE_INSERT => 'setDefaultValue'];
    }

    public function setDefaultValue()
    {
        if (!empty($this->owner->{$this->attribute})) {
            return true;
        }

        $id = $this->getLastRow()->id + 1;
        $this->owner->{$this->attribute} = $this->defaultPrefix . '-' . $id;
    }

    /**
     * Return last row inserted
     * @return mixed
     */
    public function getLastRow()
    {
        return $this->owner->find()
            ->orderBy('id DESC')
            ->one();
    }
}
{'fraction_non_alphanumeric': 0.1504739336492891, 'fraction_numerical': 0.001184834123222749, 'mean_word_length': 2.054249547920434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '4522388', 'n_tokens_mistral': 518, 'n_tokens_neox': 505, 'n_words': 110}