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

Cannot instantiate abstract class common\models\AbstractActiveRecord #327

Closed
achretien opened this issue Jan 2, 2018 · 1 comment
Closed
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug

Comments

@achretien
Copy link

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.

$baseModel = new $baseClass();

Additional info

Q A
Yii version 2.0.13.1
PHP version 7.1
Operating system alpine-linux
@samdark samdark added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Jan 7, 2018
@NickvdMeij
Copy link
Contributor

NickvdMeij commented May 8, 2018

Same problem here after updating to dev-master from v2.0.5

Yii version: 2.0.15.1
PHP version: 7.0.26
Operation System: Debian


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 ;-).

rhertogh added a commit to rhertogh/yii2-gii that referenced this issue May 23, 2018
Fixes yiisoft#327
Checks if baseClass is an abstract class, and if so wrap it in a regular class before instantiating it.
lav45 added a commit to lav45/yii2-gii that referenced this issue Oct 24, 2018
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug
Projects
None yet
Development

No branches or pull requests

3 participants