From 337b74474699e3521d9304e40534cea390003140 Mon Sep 17 00:00:00 2001 From: Blackout Date: Thu, 29 Aug 2019 13:03:53 +1000 Subject: [PATCH 1/2] Update Generator.php Optimistic Locking used to only check if the field was filled in, now it will check if the field is filled in and the field actually exists in the database --- model/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/Generator.php b/model/Generator.php index 7091ecb..bedc5d7 100644 --- a/model/Generator.php +++ b/model/Generator.php @@ -493,7 +493,7 @@ public function generateRules($table) { } } } - if (!empty($this->optimisticLock)) { + if (!empty($this->optimisticLock) && isset($table->columns[$this->optimisticLock])) { $rules[] = "[['" . $this->optimisticLock . "'], 'default', 'value' => '0']"; $rules[] = "[['" . $this->optimisticLock . "'], 'mootensai\\components\\OptimisticLockValidator']"; } From 181565d6dda2e345333b7cafe2dcede8f3705e68 Mon Sep 17 00:00:00 2001 From: Blackout Date: Thu, 29 Aug 2019 13:56:04 +1000 Subject: [PATCH 2/2] Checks if database columns exist for settings Checks to see if the database columns actually exist before enabling a Model feature. i.e. created_by, created_at, updated_by, updated_at, deleted_by, deleted_at and optimistic lock --- model/default/model.php | 335 ++++++++++++++++++++-------------------- 1 file changed, 170 insertions(+), 165 deletions(-) diff --git a/model/default/model.php b/model/default/model.php index 408b83e..313dcad 100644 --- a/model/default/model.php +++ b/model/default/model.php @@ -13,16 +13,21 @@ /* @var $labels string[] list of attribute labels (name => label) */ /* @var $rules string[] list of validation rules */ /* @var $relations array list of relations (name => relation declaration) */ + +// Used to check if a feature is enabled (by the field being filled in) and if the field actually exists in the database +$enabled = new stdClass(); +foreach (['deletedBy', 'createdBy', 'createdAt', 'updatedBy', 'updatedAt', 'deletedBy', 'deletedAt', 'UUIDColumn', 'optimisticLock'] AS $check) $enabled->$check = ($generator->$check && isset($generator->tableSchema->columns[$generator->$check])); + echo " namespace nsModel ?>\base; use Yii; -createdAt || $generator->updatedAt): ?> +createdAt || $enabled->updatedAt): ?> use yii\behaviors\TimestampBehavior; -createdBy || $generator->updatedBy): ?> +createdBy || $enabled->updatedBy): ?> use yii\behaviors\BlameableBehavior; UUIDColumn): ?> @@ -46,203 +51,203 @@ */ class extends baseModelClass, '\\') . "\n" ?> { - + -deletedBy): ?> - private $_rt_softdelete; - private $_rt_softrestore; +deletedBy): ?> + private $_rt_softdelete; + private $_rt_softrestore; - public function __construct(){ - parent::__construct(); - $this->_rt_softdelete = [ - 'deletedBy ?>' => deletedByValue)) ? 1 : $generator->deletedByValue ?>, -deletedAt): ?> - 'deletedAt ?>' => deletedAtValue)) ? 1 : $generator->deletedAtValue ?>, + public function __construct(){ + parent::__construct(); + $this->_rt_softdelete = [ + 'deletedBy ?>' => deletedByValue)) ? 1 : $generator->deletedByValue ?>, +deletedAt): ?> + 'deletedAt ?>' => deletedAtValue)) ? 1 : $generator->deletedAtValue ?>, + + ]; + $this->_rt_softrestore = [ + 'deletedBy ?>' => deletedByValueRestored)) ? 0 : $generator->deletedByValueRestored ?>, +deletedAt): ?> + 'deletedAt ?>' => deletedAtValueRestored)) ? 0 : $generator->deletedAtValueRestored ?>, + + ]; + } - ]; - $this->_rt_softrestore = [ - 'deletedBy ?>' => deletedByValueRestored)) ? 0 : $generator->deletedByValueRestored ?>, -deletedAt): ?> - 'deletedAt ?>' => deletedAtValueRestored)) ? 0 : $generator->deletedAtValueRestored ?>, + + + /** + * This function helps \mootensai\relation\RelationTrait runs faster + * @return array relation names of this model + */ + public function relationNames() + { + return []; + } + - ]; - } + /** + * @inheritdoc + */ + public function rules() + { + return []; + } + + /** + * @inheritdoc + */ + public static function tableName() + { + return 'generateTableName($tableName) ?>'; + } +db !== 'db'): ?> + + /** + * @return \yii\db\Connection the database connection used by this AR class. + */ + public static function getDb() + { + return Yii::$app->get('db ?>'); + } + +optimisticLock): ?> + + /** + * + * @return string + * overwrite function optimisticLock + * return string name of field are used to stored optimistic lock + * + */ + public function optimisticLock() { + return 'optimisticLock ?>'; + } - - /** - * This function helps \mootensai\relation\RelationTrait runs faster - * @return array relation names of this model - */ - public function relationNames() - { - return []; - } - - - /** - * @inheritdoc - */ - public function rules() - { - return []; - } - - /** - * @inheritdoc - */ - public static function tableName() - { - return 'generateTableName($tableName) ?>'; - } -db !== 'db'): ?> - - /** - * @return \yii\db\Connection the database connection used by this AR class. - */ - public static function getDb() - { - return Yii::$app->get('db ?>'); - } - -optimisticLock)): ?> - - /** - * - * @return string - * overwrite function optimisticLock - * return string name of field are used to stored optimistic lock - * - */ - public function optimisticLock() { - return 'optimisticLock ?>'; - } - - - /** - * @inheritdoc - */ - public function attributeLabels() - { - return [ + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ $label): ?> skippedColumns)): ?> - " . $generator->generateString($label) . ",\n" ?> + " . $generator->generateString($label) . ",\n" ?> - ]; - } + ]; + } $relation): ?> - skippedRelations)): ?> - - /** - * @return \yii\db\ActiveQuery - */ - public function get() - { - - } - + skippedRelations)): ?> + + /** + * @return \yii\db\ActiveQuery + */ + public function get() + { + + } + -createdAt || $generator->updatedAt - || $generator->createdBy || $generator->updatedBy - || $generator->UUIDColumn): - echo "\n"; ?> - /** - * @inheritdoc - * @return array mixed - */ - public function behaviors() - { - return [ -createdAt || $generator->updatedAt):?> - 'timestamp' => [ - 'class' => TimestampBehavior::className(), -createdAt)):?> - 'createdAtAttribute' => 'createdAt?>', +createdAt || $enabled->updatedAt + || $enabled->createdBy || $enabled->updatedBy + || $enabled->UUIDColumn): + echo "\n"; ?> + /** + * @inheritdoc + * @return array mixed + */ + public function behaviors() + { + return [ +createdAt || $enabled->updatedAt):?> + 'timestamp' => [ + 'class' => TimestampBehavior::className(), +createdAt):?> + 'createdAtAttribute' => 'createdAt?>', - 'createdAtAttribute' => false, + 'createdAtAttribute' => false, -updatedAt)):?> - 'updatedAtAttribute' => 'updatedAt?>', +updatedAt):?> + 'updatedAtAttribute' => 'updatedAt?>', - 'updatedAtAttribute' => false, + 'updatedAtAttribute' => false, timestampValue) && $generator->timestampValue != 'time()'):?> - 'value' => timestampValue?>, + 'value' => timestampValue?>, - ], + ], -createdBy || $generator->updatedBy):?> - 'blameable' => [ - 'class' => BlameableBehavior::className(), -createdBy)):?> - 'createdByAttribute' => 'createdBy?>', +createdBy || $enabled->updatedBy):?> + 'blameable' => [ + 'class' => BlameableBehavior::className(), +createdBy):?> + 'createdByAttribute' => 'createdBy?>', - 'createdByAttribute' => false, + 'createdByAttribute' => false, -updatedBy)):?> - 'updatedByAttribute' => 'updatedBy?>', +updatedBy):?> + 'updatedByAttribute' => 'updatedBy?>', - 'updatedByAttribute' => false, + 'updatedByAttribute' => false, blameableValue) && $generator->blameableValue != '\\Yii::$app->user->id'):?> - 'value' => blameableValue?>, + 'value' => blameableValue?>, - ], + ], UUIDColumn):?> - 'uuid' => [ - 'class' => UUIDBehavior::className(), -UUIDColumn)):?> - 'column' => 'UUIDColumn?>', + 'uuid' => [ + 'class' => UUIDBehavior::className(), +UUIDColumn) && isset($generator->tableSchema->columns[$generator->UUIDColumn])):?> + 'column' => 'UUIDColumn?>', - ], + ], - ]; - } + ]; + } queryNs . '\\' . $queryClassName; - echo "\n"; + $queryClassFullName = '\\' . $generator->queryNs . '\\' . $queryClassName; + echo "\n"; ?> -deletedBy): ?> - /** - * The following code shows how to apply a default condition for all queries: - * - * ```php - * class Customer extends ActiveRecord - * { - * public static function find() - * { - * return parent::find()->where(['deleted' => false]); - * } - * } - * - * // Use andWhere()/orWhere() to apply the default condition - * // SELECT FROM customer WHERE `deleted`=:deleted AND age>30 - * $customers = Customer::find()->andWhere('age>30')->all(); - * - * // Use where() to ignore the default condition - * // SELECT FROM customer WHERE age>30 - * $customers = Customer::find()->where('age>30')->all(); - * ``` - */ - - - /** - * @inheritdoc - * @return the active query used by this AR class. - */ - public static function find() - { -deletedBy): ?> - $query = new (get_called_class()); - return $query->where(['.deletedBy ?>' => deletedByValueRestored ?>]); +deletedBy): ?> + /** + * The following code shows how to apply a default condition for all queries: + * + * ```php + * class Customer extends ActiveRecord + * { + * public static function find() + * { + * return parent::find()->where(['deleted' => false]); + * } + * } + * + * // Use andWhere()/orWhere() to apply the default condition + * // SELECT FROM customer WHERE `deleted`=:deleted AND age>30 + * $customers = Customer::find()->andWhere('age>30')->all(); + * + * // Use where() to ignore the default condition + * // SELECT FROM customer WHERE age>30 + * $customers = Customer::find()->where('age>30')->all(); + * ``` + */ + + + /** + * @inheritdoc + * @return the active query used by this AR class. + */ + public static function find() + { +deletedBy): ?> + $query = new (get_called_class()); + return $query->where(['.deletedBy ?>' => deletedByValueRestored ?>]); - return new (get_called_class()); + return new (get_called_class()); - } + } }