Prohibited Table Names In Yii

Hi,

Is there a list of prohibited database table names in Yii/Yii2?

I was creating an application in Yii2 with 3 tables, product, attribute, and product_attribute (where product_attribute was a junction table).

Gii, had no problems creating a model for all 3 tables and corresponding CRUD for product and attribute.

But the resultant code had a bug in it for the model ProductAttribute. Specifically for




	/**

 	* @return \yii\db\ActiveQuery

 	*/

	public function getAttribute()



Cannot remember the exact error, but PHPStorm picked it up as well and said something like:

Declaration must be compatible with ActiveRecordInterface->getAttribute(name : string)




	/**

 	* @return \yii\db\ActiveQuery

 	*/

	public function getAttribute()

	{

    	return $this->hasOne(Attribute::className(), ['id' => 'attribute_id']);

	}


	/**

 	* @return \yii\db\ActiveQuery

 	*/

	public function getProduct()

	{

    	return $this->hasOne(Product::className(), ['id' => 'product_id']);

	}



I got around it by calling my table in the database atribute (only 1 t). And everything seemed to work fine. Is attribute a known no-no for db table names in Yii?

Cheers!

The getAttribute() is a Yii AR inbuilt method.

Yes, its recommended to avoid names based on keywords used in Yii, PHP, MYSQL etc. If you are unsure, best is to have tables and Models with some unique Prefix on objects for your app - like AppProduct, AppProductAttribute etc.

BTW… in your example you need not have renamed your table … you could have renamed the classes and relation names.

Thanks for your reply. And for your tip. I did realise its good to avoid using certain names. But was surprised that attribute was one such word.

Cheers!

Been there since Yii 1.x

Surprised that you are surprised! :rolleyes: