/** * Returns all special behavior attributes as two arrays: all attributes and only blameable attributes. * @param ActiveRecord $model * @return array two arrays: all behavior attributes and blameable attributes */ public static function getModelBehaviorAttributes($model) { $behaviorAttributes = []; $blameableAttributes = []; foreach ($model->behaviors() as $behaviorName => $behaviorOptions) { if (!is_array($behaviorOptions)) { continue; } switch ($behaviorOptions['class']) { case \netis\crud\db\SortableBehavior::className(): $behaviorAttributes[] = $behaviorOptions['attribute']; break; case \netis\crud\db\ToggableBehavior::className(): if (isset($behaviorOptions['disabledAttribute'])) { $behaviorAttributes[] = $behaviorOptions['disabledAttribute']; } if (isset($behaviorOptions['enabledAttribute'])) { $behaviorAttributes[] = $behaviorOptions['enabledAttribute']; } break; case \netis\crud\db\BlameableBehavior::className(): if (isset($behaviorOptions['createdByAttribute'])) { $behaviorAttributes[] = $behaviorOptions['createdByAttribute']; $blameableAttributes[] = $behaviorOptions['createdByAttribute']; } if (isset($behaviorOptions['updatedByAttribute'])) { $behaviorAttributes[] = $behaviorOptions['updatedByAttribute']; $blameableAttributes[] = $behaviorOptions['updatedByAttribute']; } break; case \netis\crud\db\TimestampBehavior::className(): if (isset($behaviorOptions['createdAtAttribute'])) { $behaviorAttributes[] = $behaviorOptions['createdAtAttribute']; } if (isset($behaviorOptions['updatedAtAttribute'])) { $behaviorAttributes[] = $behaviorOptions['updatedAtAttribute']; } break; } } return [$behaviorAttributes, $blameableAttributes]; }
/** * Generates behaviors for the specified table, detecting special columns. * @param \yii\db\TableSchema $table the table schema * @return array the generated behaviors as name => options */ public function generateBehaviors($table) { $available = [['name' => 'blameable', 'attributes' => ['author_id', 'created_by', 'created_id'], 'class' => \netis\crud\db\BlameableBehavior::className(), 'optionName' => 'createdByAttribute'], ['name' => 'blameable', 'attributes' => ['editor_id', 'edited_by', 'updated_by', 'updated_id', 'last_editor_id'], 'class' => \netis\crud\db\BlameableBehavior::className(), 'optionName' => 'updatedByAttribute'], ['name' => 'blameable', 'attributes' => ['update_reason'], 'class' => \netis\crud\db\BlameableBehavior::className(), 'optionName' => 'updateNotesAttribute'], ['name' => 'timestamp', 'attributes' => ['created_on', 'created_at', 'create_at', 'created_date', 'date_created'], 'class' => \netis\crud\db\TimestampBehavior::className(), 'optionName' => 'createdAtAttribute'], ['name' => 'timestamp', 'attributes' => ['updated_on', 'updated_at', 'update_at', 'updated_date', 'date_updated'], 'class' => \netis\crud\db\TimestampBehavior::className(), 'optionName' => 'updatedAtAttribute'], ['name' => 'toggable', 'attributes' => ['is_disabled', 'disabled', 'is_deleted', 'deleted', 'is_removed', 'removed', 'is_hidden', 'hidden'], 'class' => \netis\crud\db\ToggableBehavior::className(), 'optionName' => 'disabledAttribute'], ['name' => 'toggable', 'attributes' => ['is_enabled', 'enabled', 'is_active', 'active', 'is_visible', 'visible'], 'class' => \netis\crud\db\ToggableBehavior::className(), 'optionName' => 'enabledAttribute'], ['name' => 'sortable', 'attributes' => ['display_order', 'sort_order'], 'class' => \netis\crud\db\SortableBehavior::className(), 'optionName' => 'attribute'], ['name' => 'versioned', 'attributes' => ['version']]]; $behaviors = ['labels' => ['class' => \netis\crud\db\LabelsBehavior::className(), 'options' => ['attributes' => [$this->getLabelAttribute($table)]]]]; foreach ($table->columns as $column) { foreach ($available as $options) { if (in_array($column->name, $options['attributes'])) { if (isset($options['class'])) { $behaviors[$options['name']]['class'] = $options['class']; $behaviors[$options['name']]['options'][$options['optionName']] = $column->name; } else { $behaviors[$options['name']] = $column->name; } break; } } } return $behaviors; }