如何以创建项目的形式自动分配id值

时间:2015-02-15 10:33:25

标签: mysql yii2

我有创建由Gii生成的商店项目的形式。

所以我自己必须输入id值。但我需要摆脱这个领域,并自动进行分配。

对于自动分配创建和更新时间,我只是在我的模块中添加此功能

  public function behaviors() {
    return [
      TimestampBehavior::className(),
    ];
  }

我如何为id值执行此操作?

UPD

规则:

  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.');
      }
    }
}

1 个答案:

答案 0 :(得分:1)

您不应在模型验证规则中包含自动管理或由程序员管理的属性。验证仅对用户输入的数据有意义。

从该行中删除id,一切正常:

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

created_atupdated_at也是多余的,因为它们不依赖于用户,所以这就足够了:

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

<强>更新

经过深入调查,我们发现主键没有自动增量。执行此查询后,问题消失了:

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

Miroslav 回答this question的回复。