1

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.');
      }
    }
}
Dmitry
  • 242
  • 1
  • 9
  • 20
  • How exactly you want to assign it? – arogachev Feb 15 '15 at 10:36
  • @arogachev very simple. 1, 2, 3... Every next creating automatic assign next number. For example, default registration at advanced app assign previous id + 1. But i check that module and don't understand how it occurs. :( – Dmitry Feb 15 '15 at 10:42
  • Why not simply declare this column as primary key in database and remove it from validation in model? – arogachev Feb 15 '15 at 10:52
  • @arogachev my id cullumn already have primary key. First creating assigned id = 0. Second throw exception course that try assign id=0, and id like it already exist. – Dmitry Feb 15 '15 at 11:04
  • Show model validation rules and the code where you are trying to assign it. – arogachev Feb 15 '15 at 11:06
  • @arogachev Added rules. "code where you are trying to assign" - what do you mean? _form.php? – Dmitry Feb 15 '15 at 11:11
  • @arogachev I can add screenshot from phpMyAdmin, to ensure correctness. But it has russian language, and idk how change it – Dmitry Feb 15 '15 at 11:15

1 Answers1

1

You should not include attributes that are managed automatically or by programmer in model validation rules. Validation make sense only for user entered data.

Remove id from that line and everything should be OK:

 [['id', 'category_id', 'price', 'in_stock', 'discount', 'created_at', 'updated_at'], 'integer'],

created_at and updated_at are also redundant because they don't depend on user, so this is enough:

 [['category_id', 'price', 'in_stock', 'discount'], 'integer'],

Update:

After deeper investigation we found that primary key didn't have auto increment. After executing this query problem disappeared:

ALTER TABLE `shop_items`
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`id`);

Credits for Miroslav's answer to this question.

Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117