2017 © Pedro Peláez
 

library yii2-cubs

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

image

bscheshirwork/yii2-cubs

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

  • Tuesday, June 26, 2018
  • by bscheshirwork
  • Repository
  • 1 Watchers
  • 1 Stars
  • 54 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 6 % Grown

The README.md

yii2-cubs

Trait for AR. Include [create|update][At|By], flags and blockAt fields, (*1)

NO MORE monstrous field list!

Many similar AR classes? Already have the specific parent class?, (*2)

Need BlameableBehavior and BlameableBehavior for anyone it?, (*3)

OK., (*4)

Example migration create table, (*5)

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, (*6)

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

Info How to create yii2 extentions tests, (*7)

Installation

Add to you require section composer.json, (*8)

"bscheshirwork/yii2-cubs": "*",

The Versions

26/06 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bscheshir

yii2 migration activerecord db ar gii template

15/03 2018

1.0.2

1.0.2.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bscheshir

yii2 migration activerecord db ar gii template

24/10 2017

1.0.1rc1

1.0.1.0-RC1

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bscheshir

yii2 migration activerecord db ar gii template

24/10 2017

1.0.2rc1

1.0.2.0-RC1

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bscheshir

yii2 migration activerecord db ar gii template

05/05 2017

v1.0.1

1.0.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bscheshir

yii2 migration activerecord db ar gii template

27/02 2017

v1.0.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar bscheshir

yii2 migration activerecord db ar gii template