I have form of creating shop item which was generated by Gii.
So i myself must to enter id value. But I need to get rid of this field, and make an assignment automatically.
For auto assignment creating and updating time, i just adding this function in my module
public function behaviors() {
return [
TimestampBehavior::className(),
];
}
How can i do this for id value?
UPD
Rules:
public function rules() {
return [
[['category_id', 'title', 'desc', 'price', ], 'required'],
[['id', 'category_id', 'price', 'in_stock', 'discount', 'created_at', 'updated_at'], 'integer'],
[['desc', 'options', 'photos'], 'string'],
[['title'], 'string', 'max' => 100]
];
}
UPD 2
<?php
namespace backend\modules\shop\controllers;
use backend\modules\shop\models\ShopCategories;
use Yii;
use backend\modules\shop\models\ShopItems;
use backend\modules\shop\models\ShopItemsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* ItemsController implements the CRUD actions for ShopItems model.
*/
class ItemsController extends Controller {
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all ShopItems models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new ShopItemsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single ShopItems model.
* @param integer $id
* @return mixed
*/
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new ShopItems model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new ShopItems();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'category' => ShopCategories::find()->all()
]);
}
}
/**
* Updates an existing ShopItems model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
'category' => ShopCategories::find()->all()
]);
}
}
/**
* Deletes an existing ShopItems model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id) {
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the ShopItems model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return ShopItems the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = ShopItems::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}