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

Erros generating crud with gii #378

Open
atrandafir opened this issue Aug 5, 2024 · 4 comments
Open

Erros generating crud with gii #378

atrandafir opened this issue Aug 5, 2024 · 4 comments

Comments

@atrandafir
Copy link
Contributor

What steps will reproduce the problem?

  • Install yii2
  • Connect to a MongoDB database
  • Create a mongodb model
  • Open GII web interface and choose CRUD

What's expected?

  • I should get the generated crud code without any errors

What do you get instead?

  • I get the error: Call to undefined method common\models\mongo\Lab::getTableSchema()

Additional info

This is the code causing the error and it is part of https://github.com/yiisoft/yii2-gii package actually, but I'm not sure if the error comes from Gii or this extension needs some tweaking so it doesn't show up.

/**
 * Returns table schema for current model class or false if it is not an active record
 * @return \yii\db\TableSchema|false
 */
public function getTableSchema()
{
	$class = $this->modelClass;
	if (is_subclass_of($class, '\yii\db\BaseActiveRecord')) {
		return $class::getTableSchema();
	}

	return false;
}

What I did see is that in the past it worked, but Gii was updated and it stopped working. Previously the code was:

if (is_subclass_of($class, '\yii\db\ActiveRecord')) {

So as a workaround if you swap one line with the other it works. But again this is Gii code.

Also second lighter error

  • If you manage to finally generate the code, this error shows when running the create action: Calling unknown method: common\models\mongo\Lab::loadDefaultValues()
Q A
Yii version 2.0.51
Yii MongoDB version 3.0.1
Gii version 2.2.6
MongoDB server version MongoDB 7.3.3 Atlas
PHP version 8.1.19
Operating system Windows 11 Pro
@machour
Copy link
Member

machour commented Aug 5, 2024

It seems to me like your model is not extending the right subclass.

Can you show us the model code ?

@atrandafir
Copy link
Contributor Author

Sure @machour this is the model's code:

<?php

namespace common\models\mongo;

use Yii;

/**
 * @property \MongoDB\BSON\ObjectID|string $_id
 * @property string $ent_name
 * @property string $ent_fsid
 * @property string $lab_code
 * @property string $ent_summary
 * @property string $ent_url
 * @property string $palette
 * @property string $picture_url
 */
class Lab extends \yii\mongodb\ActiveRecord {

  public static function collectionName() {
    return 'fst-labs';
  }

  public function attributes() {
    return [
        '_id',
        
        'ent_name',
        'ent_fsid',
        'lab_code',
        'ent_summary',
        'ent_url',
        'palette',
        
        'picture_url',
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function rules() {
    return [
        ['ent_name', 'safe'],
        ['ent_fsid', 'safe'],
        ['lab_code', 'safe'],
        ['ent_summary', 'safe'],
        ['ent_url', 'safe'],
//        ['palette', 'safe'],
        ['picture_url', 'safe'],
    ];
  }

  public function attributeLabels() {
    return [
    ];
  }

}

The model was created manually copying code from a previous project but if I try to generate with Gii I get a similar result:

<?php

namespace common\models;

use Yii;

/**
 * This is the model class for collection "fst-labs".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 */
class FstLabs extends \yii\mongodb\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function collectionName()
    {
        return 'fst-labs';
    }

    /**
     * {@inheritdoc}
     */
    public function attributes()
    {
        return [
            '_id',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            '_id' => 'ID',
        ];
    }
}

@machour
Copy link
Member

machour commented Aug 5, 2024

Oh, it seems that mongodb requires an extra configuration to be used with Gii, could you check if you have this in place in your configuration?
https://www.yiiframework.com/extension/yiisoft/yii2-mongodb/doc/guide/2.0/en/topics-gii

@atrandafir
Copy link
Contributor Author

Yes I do and if I'm not wrong that has nothing to do with CRUD generation but Model generation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants