简体   繁体   English

Yii2 Gii CRUD生成,名称中包含空格

[英]Yii2 Gii CRUD generation with fields spaces in names

I have a PostgreSQL database with spaces in both table names and field names. 我有一个PostgreSQL数据库,在表名和字段名中都带有空格。 I have no control over this database structure. 我无法控制此数据库结构。 The problem isn't the table names, but the field names with the scaffold CRUD code. 问题不是表名,而是带有脚手架CRUD代码的字段名。 Here's an example: 这是一个例子:

CREATE TABLE "public"."MILESTONES 4 FORM TABLE" (
"Estimate ID" varchar(250) NOT NULL DEFAULT 'None'::character varying COLLATE "default",
"MS 4 Name" varchar(100) NOT NULL COLLATE "default",
"MS 4 Begin" varchar(50) COLLATE "default",
"MS 4 End" varchar(50) COLLATE "default",
"MS 4 Weight" float4 DEFAULT 0.0,
"MS 4 % Oct" float4 DEFAULT 0.0,
"MS 4 % Nov" float4 DEFAULT 0.0,
....the other months here... deleted for brevity
"MS 4 % Sep" float4 DEFAULT 0.0,
"Change Control Choice" varchar(50) COLLATE "default",
"CV% MS Oct Cum" varchar(50) COLLATE "default",
"SV% MS Oct Cum" varchar(50) COLLATE "default",
"CV% MS NOV Cum" varchar(50) COLLATE "default",
"SV% MS NOV Cum" varchar(50) COLLATE "default",
....the other months here... deleted for brevity
"CV% MS Sep Cum" varchar(50) COLLATE "default",
"SV% MS Sep Cum" varchar(50) COLLATE "default"
);

Gii generated the following for the model (I had to comment out the @property declarations because PHPStorm marked them as an error): Gii为模型生成了以下内容(我必须注释掉@property声明,因为PHPStorm将其标记为错误):

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "MILESTONES 4 FORM TABLE".
 *
//* @property string $Estimate ID
//* @property string $MS 4 Name
//* @property string $MS 4 Begin
//* @property string $MS 4 End
//* @property double $MS 4 Weight
//* @property double $MS 4 % Oct
//* @property double $MS 4 % Nov
...deleted for brevity
//* @property double $MS 4 % Sep
//* @property string $Change Control Choice
//* @property string $CV% MS Oct Cum
//* @property string $SV% MS Oct Cum
...deleted for brevity...
//* @property string $CV% MS Sep Cum
//* @property string $SV% MS Sep Cum
 */
class Milestone4AR extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'MILESTONES 4 FORM TABLE';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Estimate ID', 'MS 4 Name'], 'required'],
            [['MS 4 Weight', 'MS 4 % Oct', 'MS 4 % Nov', 'MS 4 % Dec', 'MS 4 % Jan', 'MS 4 % Feb', 'MS 4 % Mar', 'MS 4 % Apr', 'MS 4 % May', 'MS 4 % Jun', 'MS 4 % Jul', 'MS 4 % Aug', 'MS 4 % Sep'], 'number'],
            [['Estimate ID'], 'string', 'max' => 250],
            [['MS 4 Name'], 'string', 'max' => 100],
            [['MS 4 Begin', 'MS 4 End', 'Change Control Choice', 'CV% MS Oct Cum', 'SV% MS Oct Cum', 'CV% MS NOV Cum', 'SV% MS NOV Cum', 'CV% MS Dec Cum', 'SV% MS Dec Cum', 'CV% MS Jan Cum', 'SV% MS Jan Cum', 'CV% MS Feb Cum', 'SV% MS Feb Cum', 'CV% MS Mar Cum', 'SV% MS Mar Cum', 'CV% MS Apr Cum', 'SV% MS Apr Cum', 'CV% MS May Cum', 'SV% MS May Cum', 'CV% MS Jun Cum', 'SV% MS Jun Cum', 'CV% MS Jul Cum', 'SV% MS Jul Cum', 'CV% MS Aug Cum', 'SV% MS Aug Cum', 'CV% MS Sep Cum', 'SV% MS Sep Cum'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'Estimate ID' => 'Estimate  ID',
            'MS 4 Name' => 'Ms 4  Name',
            'MS 4 Begin' => 'Ms 4  Begin',
            'MS 4 End' => 'Ms 4  End',
            'MS 4 Weight' => 'Ms 4  Weight',
            'MS 4 % Oct' => 'Ms 4 %  Oct',
            'MS 4 % Nov' => 'Ms 4 %  Nov',
...deleted for brevity...
            'MS 4 % Aug' => 'Ms 4 %  Aug',
            'MS 4 % Sep' => 'Ms 4 %  Sep',
            'Change Control Choice' => 'Change  Control  Choice',
            'CV% MS Oct Cum' => 'Cv%  Ms  Oct  Cum',
            'SV% MS Oct Cum' => 'Sv%  Ms  Oct  Cum',
...deleted for brevity...
            'CV% MS Sep Cum' => 'Cv%  Ms  Sep  Cum',
            'SV% MS Sep Cum' => 'Sv%  Ms  Sep  Cum',
        ];
    }
}

I then used Gii to generate the CRUD code from the model. 然后,我使用Gii从模型生成CRUD代码。 You can see an example of the problem with the $query->andFilterWhere section. 您可以在$ query-> andFilterWhere部分中看到问题的示例。 For example, ... andFilterWhere(['like', 'MS 4 Name', $this->MS 4 Name]) will not parse. 例如,... andFilterWhere(['like','MS 4 Name',$ this-> MS 4 Name])将不会解析。

What have I done wrong, and how can I fix it? 我做错了什么,如何解决?

This isn't production code (yet) so I can easily blow away what I've already done, but I can't alter the original database table structure (boy, do I wish I could! I really dislike working on legacy code). 这还不是生产代码,所以我可以轻松地吹走已经完成的工作,但是我不能更改原始数据库表的结构(男孩,我希望我可以!我真的不喜欢使用遗留代码) 。 I have over 100 tables using this same naming convention, so need a fix that can be applied over all of them. 我有100多个表使用相同的命名约定,因此需要一种可以应用于所有表的修复程序。

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Milestone4AR;

/**
 * Milestone4Search represents the model behind the search form about `app\models\Milestone4AR`.
 */
class Milestone4Search extends Milestone4AR
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Estimate ID', 'MS 4 Name', 'MS 4 Begin', 'MS 4 End', 'Change Control Choice', 'CV% MS Oct Cum', 'SV% MS Oct Cum', 'CV% MS NOV Cum', 'SV% MS NOV Cum', 'CV% MS Dec Cum', 'SV% MS Dec Cum', 'CV% MS Jan Cum', 'SV% MS Jan Cum', 'CV% MS Feb Cum', 'SV% MS Feb Cum', 'CV% MS Mar Cum', 'SV% MS Mar Cum', 'CV% MS Apr Cum', 'SV% MS Apr Cum', 'CV% MS May Cum', 'SV% MS May Cum', 'CV% MS Jun Cum', 'SV% MS Jun Cum', 'CV% MS Jul Cum', 'SV% MS Jul Cum', 'CV% MS Aug Cum', 'SV% MS Aug Cum', 'CV% MS Sep Cum', 'SV% MS Sep Cum'], 'safe'],
            [['MS 4 Weight', 'MS 4 % Oct', 'MS 4 % Nov', 'MS 4 % Dec', 'MS 4 % Jan', 'MS 4 % Feb', 'MS 4 % Mar', 'MS 4 % Apr', 'MS 4 % May', 'MS 4 % Jun', 'MS 4 % Jul', 'MS 4 % Aug', 'MS 4 % Sep'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Milestone4AR::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'MS 4 Weight' => $this->'MS 4 Weight',
            'MS 4 % Oct' => $this->'MS 4 % Oct',
            'MS 4 % Nov' => $this->'MS 4 % Nov',
 ...deleted for brevity...
          'MS 4 % Jul' => $this->'MS 4 % Jul',
            'MS 4 % Aug' => $this->'MS 4 % Aug',
            'MS 4 % Sep' => $this->'MS 4 % Sep',
        ]);

        $query->andFilterWhere(['like', 'Estimate ID', $this->Estimate ID])
            ->andFilterWhere(['like', 'MS 4 Name', $this->MS 4 Name])
            ->andFilterWhere(['like', 'MS 4 Begin', $this->MS 4 Begin])
            ->andFilterWhere(['like', 'MS 4 End', $this->MS 4 End])
            ->andFilterWhere(['like', 'Change Control Choice', $this->Change Control Choice])
            ->andFilterWhere(['like', 'CV% MS Oct Cum', $this->CV% MS Oct Cum])
            ->andFilterWhere(['like', 'SV% MS Oct Cum', $this->SV% MS Oct Cum])
            ->andFilterWhere(['like', 'CV% MS NOV Cum', $this->CV% MS NOV Cum])
            ->andFilterWhere(['like', 'SV% MS NOV Cum', $this->SV% MS NOV Cum])
            ->andFilterWhere(['like', 'CV% MS Dec Cum', $this->CV% MS Dec Cum])
 ...deleted for brevity...
            ->andFilterWhere(['like', 'CV% MS Aug Cum', $this->CV% MS Aug Cum])
            ->andFilterWhere(['like', 'SV% MS Aug Cum', $this->SV% MS Aug Cum])
            ->andFilterWhere(['like', 'CV% MS Sep Cum', $this->CV% MS Sep Cum])
            ->andFilterWhere(['like', 'SV% MS Sep Cum', $this->SV% MS Sep Cum]);

        return $dataProvider;
    }
}

In your case you can not use shorthand properties because it's conflicting PHP attribute naming rules. 在您的情况下,您不能使用速记属性,因为它与PHP属性命名规则冲突。

There are solutions you can use though. 您可以使用一些解决方案。

You can call attribute like this: 您可以像这样调用属性:

$model->getAttribute('MS 4 Name')

or you can prepare aliased attributes in model like: 或者您可以在模型中准备别名属性,例如:

public $ms4name;

And then fill them on afterFind() like: 然后像下面这样将它们填充到afterFind()

public function afterFind()
{
    $this->ms4name = $this->getAttribute('MS 4 Name');
    parent::afterFind();
}

So from this point you can use 所以从这一点上来说,您可以使用

$model->ms4name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM