class ProductSearch extends Product
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'published', 'quantity'], 'integer'],
[['type', 'manufacturer', 'sku', 'name', 'alias', 'short_description', 'full_description', 'image', 'title', 'meta_keywords', 'meta_description', 'additional_data', 'created_date', 'modified_date'], 'safe'],
[['price'], '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 = Product::find();
$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;
}
$query->andFilterWhere([
'id' => $this->id,
'published' => $this->published,
'price' => $this->price,
'quantity' => $this->quantity,
'created_date' => $this->created_date,
'modified_date' => $this->modified_date,
]);
$query->andFilterWhere(['like', 'type', $this->type])
->andFilterWhere(['like', 'manufacturer', $this->manufacturer])
->andFilterWhere(['like', 'sku', $this->sku])
->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'alias', $this->alias])
->andFilterWhere(['like', 'short_description', $this->short_description])
->andFilterWhere(['like', 'full_description', $this->full_description])
->andFilterWhere(['like', 'image', $this->image])
->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'meta_keywords', $this->meta_keywords])
->andFilterWhere(['like', 'meta_description', $this->meta_description])
->andFilterWhere(['like', 'additional_data', $this->additional_data]);
return $dataProvider;
}
}
class Product extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'catalog_product';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['type', 'manufacturer', 'sku', 'name', 'price', 'short_description', 'full_description', 'image', 'quantity', 'title', 'meta_keywords', 'meta_description', 'additional_data'], 'required'],
[['published', 'quantity'], 'integer'],
[['price'], 'number'],
[['short_description', 'full_description', 'image', 'additional_data'], 'string'],
[['created_date', 'modified_date'], 'safe'],
[['type'], 'string', 'max' => 64],
[['manufacturer', 'sku', 'name', 'alias', 'title', 'meta_keywords', 'meta_description'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'type' => 'Type',
'manufacturer' => 'Manufacturer',
'sku' => 'Sku',
'name' => 'Name',
'alias' => 'Alias',
'published' => 'Published',
'price' => 'Price',
'short_description' => 'Short Description',
'full_description' => 'Full Description',
'image' => 'Image',
'quantity' => 'Quantity',
'title' => 'Title',
'meta_keywords' => 'Meta Keywords',
'meta_description' => 'Meta Description',
'additional_data' => 'Additional Data',
'created_date' => 'Created Date',
'modified_date' => 'Modified Date',
];
}
}
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Finds the Product model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Product the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Product::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}