yiisoft / yii2-mongodb

Yii 2 MongoDB extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
325 stars 191 forks source link

Erros generating crud with gii #378

Open atrandafir opened 1 month ago

atrandafir commented 1 month ago

What steps will reproduce the problem?

What's expected?

What do you get instead?

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

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 commented 1 month ago

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

Can you show us the model code ?

atrandafir commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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