Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks to see if the database columns actually exist #151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']";
}
Expand Down
335 changes: 170 additions & 165 deletions model/default/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<?php\n";
?>

namespace <?= $generator->nsModel ?>\base;

use Yii;
<?php if ($generator->createdAt || $generator->updatedAt): ?>
<?php if ($enabled->createdAt || $enabled->updatedAt): ?>
use yii\behaviors\TimestampBehavior;
<?php endif; ?>
<?php if ($generator->createdBy || $generator->updatedBy): ?>
<?php if ($enabled->createdBy || $enabled->updatedBy): ?>
use yii\behaviors\BlameableBehavior;
<?php endif; ?>
<?php if ($generator->UUIDColumn): ?>
Expand All @@ -46,203 +51,203 @@
*/
class <?= $className ?> extends <?= ($isTree) ? '\kartik\tree\models\Tree' . "\n" : '\\' . ltrim($generator->baseModelClass, '\\') . "\n" ?>
{
<?= (!$isTree) ? " use \\mootensai\\relation\\RelationTrait;\n" : "" ?>
<?= (!$isTree) ? " use \\mootensai\\relation\\RelationTrait;\n" : "" ?>

<?php if($generator->deletedBy): ?>
private $_rt_softdelete;
private $_rt_softrestore;
<?php if ($enabled->deletedBy): ?>
private $_rt_softdelete;
private $_rt_softrestore;

public function __construct(){
parent::__construct();
$this->_rt_softdelete = [
'<?= $generator->deletedBy ?>' => <?= (empty($generator->deletedByValue)) ? 1 : $generator->deletedByValue ?>,
<?php if($generator->deletedAt): ?>
'<?= $generator->deletedAt ?>' => <?= (empty($generator->deletedAtValue)) ? 1 : $generator->deletedAtValue ?>,
public function __construct(){
parent::__construct();
$this->_rt_softdelete = [
'<?= $generator->deletedBy ?>' => <?= (empty($generator->deletedByValue)) ? 1 : $generator->deletedByValue ?>,
<?php if ($enabled->deletedAt): ?>
'<?= $generator->deletedAt ?>' => <?= (empty($generator->deletedAtValue)) ? 1 : $generator->deletedAtValue ?>,
<?php endif; ?>
];
$this->_rt_softrestore = [
'<?= $generator->deletedBy ?>' => <?= (empty($generator->deletedByValueRestored)) ? 0 : $generator->deletedByValueRestored ?>,
<?php if ($enabled->deletedAt): ?>
'<?= $generator->deletedAt ?>' => <?= (empty($generator->deletedAtValueRestored)) ? 0 : $generator->deletedAtValueRestored ?>,
<?php endif; ?>
];
}
<?php endif; ?>
];
$this->_rt_softrestore = [
'<?= $generator->deletedBy ?>' => <?= (empty($generator->deletedByValueRestored)) ? 0 : $generator->deletedByValueRestored ?>,
<?php if($generator->deletedAt): ?>
'<?= $generator->deletedAt ?>' => <?= (empty($generator->deletedAtValueRestored)) ? 0 : $generator->deletedAtValueRestored ?>,
<?php if (!$isTree): ?>

/**
* This function helps \mootensai\relation\RelationTrait runs faster
* @return array relation names of this model
*/
public function relationNames()
{
return [<?= "\n\t\t\t'" . implode("',\n\t\t\t'", array_keys($relations)) . "'\n\t\t" ?>];
}

<?php endif; ?>
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [<?= "\n\t\t\t" . implode(",\n\t\t\t", $rules) . "\n\t\t" ?>];
}

/**
* @inheritdoc
*/
public static function tableName()
{
return '<?= $generator->generateTableName($tableName) ?>';
}
<?php if ($enabled->db !== 'db'): ?>

/**
* @return \yii\db\Connection the database connection used by this AR class.
*/
public static function getDb()
{
return Yii::$app->get('<?= $generator->db ?>');
}
<?php endif; ?>
<?php if ($enabled->optimisticLock): ?>

/**
*
* @return string
* overwrite function optimisticLock
* return string name of field are used to stored optimistic lock
*
*/
public function optimisticLock() {
return '<?= $generator->optimisticLock ?>';
}
<?php endif; ?>
<?php if (!$isTree): ?>

/**
* This function helps \mootensai\relation\RelationTrait runs faster
* @return array relation names of this model
*/
public function relationNames()
{
return [<?= "\n '" . implode("',\n '", array_keys($relations)) . "'\n " ?>];
}

<?php endif; ?>
/**
* @inheritdoc
*/
public function rules()
{
return [<?= "\n " . implode(",\n ", $rules) . "\n " ?>];
}

/**
* @inheritdoc
*/
public static function tableName()
{
return '<?= $generator->generateTableName($tableName) ?>';
}
<?php if ($generator->db !== 'db'): ?>

/**
* @return \yii\db\Connection the database connection used by this AR class.
*/
public static function getDb()
{
return Yii::$app->get('<?= $generator->db ?>');
}
<?php endif; ?>
<?php if (!empty($generator->optimisticLock)): ?>

/**
*
* @return string
* overwrite function optimisticLock
* return string name of field are used to stored optimistic lock
*
*/
public function optimisticLock() {
return '<?= $generator->optimisticLock ?>';
}
<?php endif; ?>

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
<?php foreach ($labels as $name => $label): ?>
<?php if (!in_array($name, $generator->skippedColumns)): ?>
<?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
<?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
<?php endif; ?>
<?php endforeach; ?>
];
}
];
}
<?php foreach ($relations as $name => $relation): ?>
<?php if(!in_array($name, $generator->skippedRelations)): ?>

/**
* @return \yii\db\ActiveQuery
*/
public function get<?= ucfirst($name) ?>()
{
<?= $relation[0] . "\n" ?>
}
<?php endif; ?>
<?php if (!in_array($name, $generator->skippedRelations)): ?>

/**
* @return \yii\db\ActiveQuery
*/
public function get<?= ucfirst($name) ?>()
{
<?= $relation[0] . "\n" ?>
}
<?php endif; ?>
<?php endforeach; ?>
<?php if ($generator->createdAt || $generator->updatedAt
|| $generator->createdBy || $generator->updatedBy
|| $generator->UUIDColumn):
echo "\n"; ?>
/**
* @inheritdoc
* @return array mixed
*/
public function behaviors()
{
return <?= ($isTree) ? "array_merge(parent::behaviors(), " : ""; ?>[
<?php if ($generator->createdAt || $generator->updatedAt):?>
'timestamp' => [
'class' => TimestampBehavior::className(),
<?php if (!empty($generator->createdAt)):?>
'createdAtAttribute' => '<?= $generator->createdAt?>',
<?php if ($enabled->createdAt || $enabled->updatedAt
|| $enabled->createdBy || $enabled->updatedBy
|| $enabled->UUIDColumn):
echo "\n"; ?>
/**
* @inheritdoc
* @return array mixed
*/
public function behaviors()
{
return <?= ($isTree) ? "array_merge(parent::behaviors(), " : ""; ?>[
<?php if ($enabled->createdAt || $enabled->updatedAt):?>
'timestamp' => [
'class' => TimestampBehavior::className(),
<?php if ($enabled->createdAt):?>
'createdAtAttribute' => '<?= $generator->createdAt?>',
<?php else :?>
'createdAtAttribute' => false,
'createdAtAttribute' => false,
<?php endif; ?>
<?php if (!empty($generator->updatedAt)):?>
'updatedAtAttribute' => '<?= $generator->updatedAt?>',
<?php if ($enabled->updatedAt):?>
'updatedAtAttribute' => '<?= $generator->updatedAt?>',
<?php else :?>
'updatedAtAttribute' => false,
'updatedAtAttribute' => false,
<?php endif; ?>
<?php if (!empty($generator->timestampValue) && $generator->timestampValue != 'time()'):?>
'value' => <?= $generator->timestampValue?>,
'value' => <?= $generator->timestampValue?>,
<?php endif; ?>
],
],
<?php endif; ?>
<?php if ($generator->createdBy || $generator->updatedBy):?>
'blameable' => [
'class' => BlameableBehavior::className(),
<?php if (!empty($generator->createdBy)):?>
'createdByAttribute' => '<?= $generator->createdBy?>',
<?php if ($enabled->createdBy || $enabled->updatedBy):?>
'blameable' => [
'class' => BlameableBehavior::className(),
<?php if ($enabled->createdBy):?>
'createdByAttribute' => '<?= $generator->createdBy?>',
<?php else :?>
'createdByAttribute' => false,
'createdByAttribute' => false,
<?php endif; ?>
<?php if (!empty($generator->updatedBy)):?>
'updatedByAttribute' => '<?= $generator->updatedBy?>',
<?php if ($enabled->updatedBy):?>
'updatedByAttribute' => '<?= $generator->updatedBy?>',
<?php else :?>
'updatedByAttribute' => false,
'updatedByAttribute' => false,
<?php endif; ?>
<?php if (!empty($generator->blameableValue) && $generator->blameableValue != '\\Yii::$app->user->id'):?>
'value' => <?= $generator->blameableValue?>,
'value' => <?= $generator->blameableValue?>,
<?php endif; ?>
],
],
<?php endif; ?>
<?php if ($generator->UUIDColumn):?>
'uuid' => [
'class' => UUIDBehavior::className(),
<?php if (!empty($generator->UUIDColumn)):?>
'column' => '<?= $generator->UUIDColumn?>',
'uuid' => [
'class' => UUIDBehavior::className(),
<?php if (!empty($generator->UUIDColumn) && isset($generator->tableSchema->columns[$generator->UUIDColumn])):?>
'column' => '<?= $generator->UUIDColumn?>',
<?php endif; ?>
],
],
<?php endif; ?>
]<?= ($isTree) ? ")" : "" ?>;
}
]<?= ($isTree) ? ")" : "" ?>;
}
<?php endif; ?>
<?php if ($queryClassName): ?>
<?php
$queryClassFullName = '\\' . $generator->queryNs . '\\' . $queryClassName;
echo "\n";
$queryClassFullName = '\\' . $generator->queryNs . '\\' . $queryClassName;
echo "\n";
?>
<?php if( $generator->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();
* ```
*/
<?php endif; ?>

/**
* @inheritdoc
* @return <?= $queryClassFullName ?> the active query used by this AR class.
*/
public static function find()
{
<?php if($generator->deletedBy): ?>
$query = new <?= $queryClassFullName ?>(get_called_class());
return $query->where(['<?= $tableName ?>.<?= $generator->deletedBy ?>' => <?= $generator->deletedByValueRestored ?>]);
<?php if ($enabled->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();
* ```
*/
<?php endif; ?>

/**
* @inheritdoc
* @return <?= $queryClassFullName ?> the active query used by this AR class.
*/
public static function find()
{
<?php if ($enabled->deletedBy): ?>
$query = new <?= $queryClassFullName ?>(get_called_class());
return $query->where(['<?= $tableName ?>.<?= $generator->deletedBy ?>' => <?= $generator->deletedByValueRestored ?>]);
<?php else: ?>
return new <?= $queryClassFullName ?>(get_called_class());
return new <?= $queryClassFullName ?>(get_called_class());
<?php endif; ?>
}
}
<?php endif; ?>
}