Unknown Property – yii\base\UnknownPropertyException
Opened this issue · 0 comments
Please help me, i often got this error. But sometime it run ok, i dont know what happens.
Getting unknown property: app\models\CategorySearch::name
table category
Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_by | int(11) | YES | MUL | NULL | |
| created_at | int(11) | YES | | NULL | |
| updated_by | int(11) | YES | MUL | NULL | |
| updated_at | int(11) | YES | | NULL | |
| status | smallint(6) | YES | | 1 | |
| parent_id | int(11) | YES | MUL | NULL |
table category_translation
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_by | int(11) | YES | MUL | NULL | |
| created_at | int(11) | YES | | NULL | |
| updated_by | int(11) | YES | MUL | NULL | |
| updated_at | int(11) | YES | | NULL | |
| language | varchar(6) | NO | MUL | NULL | |
| category_id | int(11) | NO | MUL | NULL | |
| name | varchar(255) | YES | | NULL | |
| slug | varchar(255) | YES | MUL | NULL | |
| description | longtext | YES | | NULL | |
| image_id | int(11) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
`<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Category;
/**
-
CategorySearch represents the model behind the search form about
common\models\Category
.
/
class CategorySearch extends Category
{
/*- @inheritdoc
*/
public function rules()
{
return [
[['id', 'status', 'parent_id'], 'integer'],
[['name', 'slug', 'image_id'], 'safe']
];
}
/**
- @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)
{
//->multilingual()
$query = Category::find()->joinWith('translations');
// add conditions that should always apply here$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => ['attributes'=>['id', 'status', 'parent_id', 'name', 'slug', 'image_id', 'updated_at']],
'pagination' => [
'pageSize' => 1000,
],
]);$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([
'id' => $this->id,
'category_translation.name'=>$this->name,
'category_translation.slug'=>$this->slug,
'category_translation.image_id'=>$this->image_id,
'status' => $this->status,
'parent_id' => $this->parent_id,
'updated_at' => $this->updated_at,
]);return $dataProvider;
}
}`
- @inheritdoc
`<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
use omgdef\multilingual\MultilingualBehavior;
use omgdef\multilingual\MultilingualQuery;
use yii\web\UploadedFile;
/**
-
This is the model class for table "category".
* -
@Property integer $id
-
@Property integer $created_by
-
@Property integer $created_at
-
@Property integer $updated_by
-
@Property integer $updated_at
-
@Property integer $status
-
@Property integer $parent_id
* -
@Property User $createdBy
-
@Property Category $parent
-
@Property Category[] $categories
-
@Property User $updatedBy
-
@Property CategoryTranslation[] $categoryTranslations
-
@Property Product[] $products
/
class Category extends \yii\db\ActiveRecord
{
const STATUS_ACTIVE = 10;
const STATUS_UNACTIVE = 11;
/*- @inheritdoc
*/
public static function tableName()
{
return 'category';
}
public function behaviors()
{
return [
TimestampBehavior::className(),
BlameableBehavior::className(),
'ml' => [
'class' => MultilingualBehavior::className(),
'languages' => Yii::$app->params['languages'],
//'languageField' => 'language',
//'localizedPrefix' => '',
// 'requireTranslations' => true,
// 'dynamicLangClass' => true,
// 'defaultLanguage' => Yii::$app->params['defaultLanguage'],
'langClassName' => CategoryTranslation::className(), // or namespace/for/a/class/PostLang
'langForeignKey' => 'category_id',
'tableName' => "{{%category_translation}}",
'attributes' => [
'name', 'slug', 'description', 'image_id'
]
],
];
}/**
- @inheritdoc
*/
public function rules()
{
return [
[['created_by', 'created_at', 'updated_by', 'updated_at', 'status', 'parent_id'], 'integer'],
[['name','code'], 'required'],
[['description'], 'string'],
[['name', 'slug'], 'string', 'max' => 255],
[['image_id'], 'safe'],
[['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['created_by' => 'id']],
[['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['parent_id' => 'id']],
[['updated_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['updated_by' => 'id']],
];
}
/**
- @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'created_by' => Yii::t('app', 'Created By'),
'created_at' => Yii::t('app', 'Created At'),
'updated_by' => Yii::t('app', 'Updated By'),
'updated_at' => Yii::t('app', 'Updated At'),
'status' => Yii::t('app', 'Status'),
'parent_id' => Yii::t('app', 'Parent ID'),
'code' => Yii::t('app', 'Code'),
'name' => Yii::t('app', 'Name'),
'slug' => Yii::t('app', 'Slug'),
'description' => Yii::t('app', 'Description'),
'image_id' => Yii::t('app', 'Image ID'),
];
}
/**
- @return \yii\db\ActiveQuery
*/
public function getCreatedBy()
{
return $this->hasOne(User::className(), ['id' => 'created_by']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getParent()
{
return $this->hasOne(Category::className(), ['id' => 'parent_id']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getCategories()
{
return $this->hasMany(Category::className(), ['parent_id' => 'id']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getUpdatedBy()
{
return $this->hasOne(User::className(), ['id' => 'updated_by']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getCategoryTranslations()
{
return $this->hasMany(CategoryTranslation::className(), ['category_id' => 'id']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getProducts()
{
return $this->hasMany(Product::className(), ['category_id' => 'id']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getImage()
{
return $this->hasOne(File::className(), ['id' => 'image_id']);
}
public static function find()
{
return new MultilingualQuery(get_called_class());
}public static function getStatusOptions() {
return [
self::STATUS_ACTIVE => Yii::t('app','Active'),
self::STATUS_UNACTIVE => Yii::t('app','Unactive')
];
}public function getStatusLabel() {
$statusOptions = self::getStatusOptions();
return isset($statusOptions[$this->status]) ? $statusOptions[$this->status] : 'Unknown';
}public function getImagePath() {
return Yii::getAlias('@root') . '/data/images/category/';
}public function getImageUrl() {
return '/data/images/category/' . $this->image->name;
}public static function getCategoryRecuisiveOptions(&$options = [], $parent_id = 0, $dept = 0, $char = '--') {
if ($parent_id > 0) {
$categories = Category::find()->where(['parent_id'=>$parent_id])->all();
if (count($categories) > 0) {
foreach ($categories as $category) {
$options[$category->id] = str_repeat($char, $dept) . ' ' . ($category->name ? $category->name : Yii::t('yii','(not set)'));
self::getCategoryRecuisiveOptions($options, $category->id, $dept + 1, $char);
}
}
} else {
if ($dept == 0) {
$categories = Category::find()->where('parent_id is null')->all();
if (count($categories) > 0) {
foreach ($categories as $category) {
$options[$category->id] = $category->name ? $category->name : Yii::t('yii','(not set)');
self::getCategoryRecuisiveOptions($options, $category->id, $dept + 1, $char);
}
}
}
}
return $options;
}public static function getParentCategoryOptions($ignore_ids = []) {
$options = [];
$categories = Category::find()->where('parent_id is null')->andWhere(['not in','id',$ignore_ids])->all();
if (count($categories) > 0) {
foreach ($categories as $category) {
$options[$category->id] = $category->name ? $category->name : Yii::t('yii','(not set)');
}
}
return $options;
}public function beforeSave($insert) {
$result = true;
if (parent::beforeSave($insert)) {
$this->image_id = UploadedFile::getInstance($this,'image_id');
if(is_object($this->image_id)){
$path = $this->getImagePath(); //set directory path to save image
if (!is_dir($path)) {
$image_path = \yii\helpers\FileHelper::createDirectory($path,0777,true);
}$extension = $this->image_id->extension; $slug = \yii\helpers\Inflector::slug($this->name, '-'); $image_name = $slug.".".$extension; $this->image_id->saveAs($path . $image_name); $imagine = \yii\imagine\Image::getImagine(); $image = $imagine->open($path . $image_name); $image_sizes= $image->getSize(); $fileModel = new File(); $fileModel->name = $image_name; $fileModel->location = str_replace(Yii::getAlias('@root'), '', $path); $fileModel->ext = $extension; $fileModel->size = $this->image_id->size; $fileModel->width = $image_sizes->getWidth(); $fileModel->height = $image_sizes->getHeight(); if ($fileModel->save()) { $this->image_id = $fileModel->id; } else { $result = false; } } } else { $result = false; } return $result;
}
}` - @inheritdoc
`<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\SluggableBehavior;
use yii\behaviors\BlameableBehavior;
/**
-
This is the model class for table "category_translation".
* -
@Property integer $id
-
@Property integer $created_by
-
@Property integer $created_at
-
@Property integer $updated_by
-
@Property integer $updated_at
-
@Property string $language
-
@Property integer $category_id
-
@Property string $name
-
@Property string $slug
-
@Property string $description
-
@Property integer $image_id
* -
@Property Category $category
-
@Property User $createdBy
-
@Property File $image
-
@Property User $updatedBy
/
class CategoryTranslation extends \yii\db\ActiveRecord
{
/*- @inheritdoc
*/
public static function tableName()
{
return 'category_translation';
}
/**
- @inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
BlameableBehavior::className(),
[
'class' => SluggableBehavior::className(),
'attribute' => 'name',
'immutable' => true,
'ensureUnique'=>true
],
];
}
/**
- @inheritdoc
*/
public function rules()
{
return [
[['created_by', 'created_at', 'updated_by', 'updated_at', 'category_id', 'image_id'], 'integer'],
[['language', 'category_id'], 'required'],
[['description'], 'string'],
[['language'], 'string', 'max' => 6],
[['name', 'slug'], 'string', 'max' => 255],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
[['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['created_by' => 'id']],
[['image_id'], 'exist', 'skipOnError' => true, 'targetClass' => File::className(), 'targetAttribute' => ['image_id' => 'id']],
[['updated_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['updated_by' => 'id']],
];
}
/**
- @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'created_by' => Yii::t('app', 'Created By'),
'created_at' => Yii::t('app', 'Created At'),
'updated_by' => Yii::t('app', 'Updated By'),
'updated_at' => Yii::t('app', 'Updated At'),
'language' => Yii::t('app', 'Language'),
'category_id' => Yii::t('app', 'Category ID'),
'name' => Yii::t('app', 'Name'),
'slug' => Yii::t('app', 'Slug'),
'description' => Yii::t('app', 'Description'),
'image_id' => Yii::t('app', 'Image ID'),
];
}
/**
- @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getCreatedBy()
{
return $this->hasOne(User::className(), ['id' => 'created_by']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getImage()
{
return $this->hasOne(File::className(), ['id' => 'image_id']);
}
/**
- @return \yii\db\ActiveQuery
*/
public function getUpdatedBy()
{
return $this->hasOne(User::className(), ['id' => 'updated_by']);
}
}`
- @inheritdoc