gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:03:40 25 4
gpt4 key购买 nike

我有创建由 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.');
}
}
}

最佳答案

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

从该行中删除 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`);

感谢 Miroslavthis question 的回答.

关于mysql - 如何以创建项目的形式自动分配id值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28525178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com