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
Cannot instantiate abstract class common\models\AbstractActiveRecord #327
Comments
Same problem here after updating to dev-master from v2.0.5 Yii version: 2.0.15.1 After diving in deeper, I found that the $baseModel cannot be an abstract class due to how the code works: protected function generateRelationName($relations, $table, $key, $multiple)
{
/*
* It created a "dummy" class using the specified base class
*/
// static $baseModel;
// /* @var $baseModel \yii\db\ActiveRecord */
// if ($baseModel === null) {
// $baseClass = $this->baseClass;
// var_dump($relations, $table, $key, $multiple, $baseClass);
// $baseModel = new $baseClass();
// $baseModel->setAttributes([]);
// }
if (!empty($key) && strcasecmp($key, 'id')) {
if (substr_compare($key, 'id', -2, 2, true) === 0) {
$key = rtrim(substr($key, 0, -2), '_');
} elseif (substr_compare($key, 'id', 0, 2, true) === 0) {
$key = ltrim(substr($key, 2, strlen($key)), '_');
}
}
if ($multiple) {
$key = Inflector::pluralize($key);
}
$name = $rawName = Inflector::id2camel($key, '_');
$i = 0;
/*
* And uses it here to generate a name if the base model has the property $name (camelcased).
*/
// while ($baseModel->hasProperty(lcfirst($name))) {
// $name = $rawName . ($i++);
// }
while (isset($table->columns[lcfirst($name)])) {
$name = $rawName . ($i++);
}
while (isset($relations[$table->fullName][$name])) {
$name = $rawName . ($i++);
}
return $name;
} Like you can see, if you use an abstract class as base class and comment out the sections shown above, it will generate all models like usual. Its a temperary workaround ofcourse, but it should do the trick for everyone currently encountering this problem ;-). |
Fixes yiisoft#327 Checks if baseClass is an abstract class, and if so wrap it in a regular class before instantiating it.
* upstream/master: Remove useless import of `Yii` from CRUD generator search model template Fixes yiisoft#379: Fixed bug in view page where delete button not work well Fixes yiisoft#366: Option to allow standardized class names capitals in model generator Fixes yiisoft#327: Fixed bug in Model generator when $baseClass is an abstract class Fixes yiisoft#366: Better class and file names for uppercase tables Update composer.json (yiisoft#364) Fix codestyle (yiisoft#362) prepare for next release release version 2.0.7 Register CSRF meta-tags dynamically Updated CHANGELOG [ci skip] Fixed rules generation for JSON columns Removed redundant line from license [skip ci] added database version to issue template (yiisoft#352) [skip ci] docs/guide-ja revised [ci skip] (yiisoft#349) Updated issue template [skip ci] Fixes yiisoft#185: Fix bug in Model generators when FKs pointing to non-existing tables Fixes yiisoft#340: Fix bug in CRUD SearchModel generator Require Yii 2.0.14 (yiisoft#339) Conflicts: composer.json
What steps will reproduce the problem?
Use an abstract class as $baseClass
What's expected?
Generate the files properly.
What do you get instead?
The error "Cannot instantiate abstract class common\models\AbstractActiveRecord" in
generators/model/Generator.php
at line 686.yii2-gii/generators/model/Generator.php
Line 686 in f15b8dc
Additional info
The text was updated successfully, but these errors were encountered: