Mssql Error : The Column Must Be Specified In The Format Of "name:type:label", Where "type" And "label" Are Optional.

Hello,

I want to show data in column name "[color="#00FFFF"]Search Description[/color]" from MSSQL Server.

But column name has a space eg. [No_] ,[Description],[[color="#00FFFF"]Search Description[/color]]

System Show ERROR.

The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.

C:\inetpub\framework\zii\widgets\grid\CGridView.php(412)







400         foreach($this->columns as $column)

401             $column->init();

402     }

403 

404     /**

405      * Creates a {@link CDataColumn} based on a shortcut column specification string.

406      * @param string $text the column specification string

407      * @return CDataColumn the column instance

408      */

409     protected function createDataColumn($text)

410     {

411         if(!preg_match('/^([\w\.]+)(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />\w*))?(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />.*))?$/',$text,$matches))

412             throw new CException(Yii::t('zii','The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));

413         $column=new CDataColumn($this);

414         $column->name=$matches[1];

415         if(isset($matches[3]) && $matches[3]!=='')

416             $column->type=$matches[3];

417         if(isset($matches[5]))

418             $column->header=$matches[5];

419         return $column;

420     }

421 

422     /**

423      * Registers necessary client scripts.

424      */



My View





        $this->widget('zii.widgets.grid.CGridView', array(        

            'dataProvider' => $Items->search(),	

	    'filter' => $Items,            

            'columns' => array(               

		'No_',

                'Description',                

                'Search Description',  // Error if enable;

            ),

        ));




My Model


 

class Items extends CActiveRecord {


    public static function model($className = __CLASS__) {

        return parent::model($className);

    }


    public function tableName() {

        return "[DB_TH\$Item]";

    }   


   public function attributeLabels(){

		return array(

			"[No_]" => "Items",	

			"[Description]" => "Description",			

			"[Search Description]" => "Search Description" 

			);

	}

	

   public function search() {

        $criteria = new CDbCriteria;

		

	$criteria->compare("[No_]", $this->No_, true);

	$criteria->compare("[Description]", $this->Description, true); 

        //$criteria->compare("[Search Description]", $this->Search  Description, true);  // Error if enable;


        return new CActiveDataProvider($this, array(

                    'criteria' => $criteria,

					"pagination" => array(

						"pageSize" => 30

						)

                ));

    }	

    public function rules(){

		return array(

			array('No_,Description','required'),

			array('No_,Description', 'safe' ,'on' => 'search'),

			);	

	}

}



My Controller




 public function actionShowItems()

	{

		$Item = new Items();		

		$Item->unsetAttributes();

		 

		

		if(isset($_GET['Items'])){

				$Item->attributes = $_GET['Items'];

		}

                

		$this->render("Items", array("Items" => $Item));		 

		

	}  



*** If I do not put "Search Description" can run normally.

Help me please.

Can’t you rename the column in MSSQL?

Databases from other offline applications .

I want create report online for sale team.

With that limitation I don’t see an easy way around this unless you use Data Access Objects to bind the column values (see this) or else use straight SQL.

Good luck.

I have the solution for myself.

Just create Views in MSSQL Server and change Alias name as needed.

5322

View.png

Thanks for the suggestion.

Excellent idea! However, take care of a couple of caveats when you use database views in Yii because databases don’t expose the following to the schema Yii can access:

  1. Primary key - if you need to access the primary key you have to declare it explicitly in the model:



public function primaryKey()

{

    return 'id'; // or whatever the primary key name is

}



  1. Updatable view - if you plan on inserting records through the view (must be updatable as per MSSQL requirements (see the remarks section here) its primary key must be retrieved manually in the model:



public function afterSave()

{

    if ($this->getIsNewRecord()) {

       $this->id = Yii::app()->db->getLastInsertID();

    }

    return parent::afterSave();

}



After that, you’re all set. Good luck.