Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bscheshirwork/yii2-cubs

Trait for AR - make you class clear! Separate from the main fields: [create|update][At|By]|flags|blockAt
https://github.com/bscheshirwork/yii2-cubs

Last synced: about 1 month ago
JSON representation

Trait for AR - make you class clear! Separate from the main fields: [create|update][At|By]|flags|blockAt

Awesome Lists containing this project

README

        

# yii2-cubs
Trait for AR. Include [create|update][At|By], flags and blockAt fields

## NO MORE monstrous field list!

Many similar AR classes? Already have the specific parent class?

Need `BlameableBehavior` and `BlameableBehavior` for anyone it?

OK.

Example `migration` create table
```
db->driverName == 'mysql' ? 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB' : '';

$this->createTable('{{%project}}', [
'id' => $this->primaryKey(),
'name' => $this->string(64)->notNull(),
'url' => $this->text(),
'description' => $this->text(),
], $options);
$this->createIndex('{{%project_unique_name}}', '{{%project}}', 'name', true);

$this->createTable('{{%project_form}}', [
'id' => $this->primaryKey(),
'projectId' => $this->integer(),
'type' => $this->string(32)->notNull(),
'name' => $this->string(64)->null(),
'url' => $this->text(),
'description' => $this->text(),
], $options);
$this->addForeignKey('{{%fk_project_project_form}}', '{{%project_form}}', 'projectId', '{{%project}}', 'id', 'CASCADE', 'CASCADE');
}

public function down()
{
$this->dropTable('{{%project_form}}');
$this->dropTable('{{%project}}');
}
}

```

Produced schema like this
```
CREATE TABLE `project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`url` text,
`description` text,
`createdAt` datetime NOT NULL,
`createdBy` datetime DEFAULT NULL,
`updatedAt` datetime DEFAULT NULL,
`updatedBy` datetime DEFAULT NULL,
`stateOfFlags` int(11) NOT NULL DEFAULT '1',
`blockedAt` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `project_unique_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `project_form` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`projectId` int(11) DEFAULT NULL,
`type` varchar(32) NOT NULL,
`name` varchar(64) DEFAULT NULL,
`url` text,
`description` text,
`createdAt` datetime NOT NULL,
`createdBy` int(11) DEFAULT NULL,
`updatedAt` datetime DEFAULT NULL,
`updatedBy` int(11) DEFAULT NULL,
`stateOfFlags` int(11) NOT NULL DEFAULT '1',
`blockedAt` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_project_project_form` (`projectId`),
CONSTRAINT `fk_project_project_form` FOREIGN KEY (`projectId`) REFERENCES `project` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
```

Example `gii`:
settings `config/main-local.php`
```
...
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
'allowedIPs' => ['*'],
'generators' => [
'model' => [ // generator name
'class' => 'bscheshirwork\cubs\generators\model\Generator', // generator class
'templates' => [
'default' => '@bscheshirwork/cubs/generators/model/cubs', // default template name => alias and path to template
// or direct
// 'cubs' => '@vendor/bscheshirwork/yii2-cubs/src/generators/model/cubs', // template name => path to template
]
],
'crud' => [
'class' => 'bscheshirwork\cubs\generators\crud\Generator',
'templates' => [
'default' => '@bscheshirwork/cubs/generators/crud/cubs',
]
],
],
];

...
```
Model Generator `gii/model` and CRUD generator `gii/crud` have new field;
check `Cubs`, set `Cubs interface` and select `cubs` template.

Generated the `Project` model like this
```
64],
[['name'], 'unique'],
]);
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return ArrayHelper::merge(parent::attributeLabels(), [
'id' => Yii::t('app', 'ID'),
'name' => Yii::t('app', 'Name'),
'url' => Yii::t('app', 'Url'),
'description' => Yii::t('app', 'Description'),
]);
}

/**
* @inheritdoc
*/
public function hints()
{
return ArrayHelper::merge(parent::hints(), [
]);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getProjectForms()
{
return $this->hasMany(ProjectForm::class, ['projectId' => 'id']);
}

/**
* @inheritdoc
* @return ProjectQuery the active query used by this AR class.
*/
public static function find()
{
return new ProjectQuery(get_called_class());
}
}

```

`ProjectQuery` use `CubsQueryModelTrait`, `ProjectController` use `CubsControllerTrait`, and `ProjectSearch` use `CubsSearchModelTrait`.

#Customize

Create your own interface and use it.
`my\base\CubsDefaultInterface.php`
```
'DISABLED',
self::STATE_ENABLED => 'ACTIVE',
self::STATE_BLOCKED => 'BLOCKED',
self::STATE_PROCESS => 'PROCESS',
];
}
```

The migrations for the existing tables must be created manually.

#i18n

Example settings `config/main.php`
```
...
'components' => [
'i18n' => [
'translations' => [
'cubs' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@vendor/bscheshirwork/yii2-cubs/messages',
],
],
],
],
...
```

#formater

Example settings `config/main.php`
```
...
'components' => [
'formatter' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'php:Y-m-d',
'datetimeFormat' => 'php:Y-m-d H:i:s',
'timeFormat' => 'php:H:i:s',
],
],
...
```

#advanced usage

Example view `views/project/view.php`
```
= \bscheshirwork\cubs\helpers\WidgetHelper::HtmlBlockButton($model) ?>

= DetailView::widget([
'model' => $model,
'attributes' => \yii\helpers\ArrayHelper::merge([
'id',
'name',
'url:ntext',
'description:ntext',
], \bscheshirwork\cubs\helpers\WidgetHelper::DetailViewArray($model)),
]) ?>

```

Example controller `ProjectController`
```
class ProjectController extends Controller
{
use \bscheshirwork\cubs\base\CubsControllerTrait;
...
```

[Info](docs/cubs.md)
[How to create yii2 extentions tests](docs/tests.md)

#Installation
Add to you `require` section `composer.json`
```
"bscheshirwork/yii2-cubs": "*",
```