我需要将下一个自动插入到数据库中orders_id
的Orders
表中。orders_id
不是主键,也不会自动递增
我需要查询数据库,找到最后(最高)的 id 值,递增 1,并将其插入数据库的 Orders 表中。实际上,我有一个发货操作,它将提供订单发货地址。因此,一旦用户填写地址表格并转到付款页面,我想同时填写我的Orders
表格max(orders_id)+1
。
Address
我已经在表格和表格之间建立了关系,Orders
但我的orders
表格没有被填充。请详细说明我在 Yii 的控制器中实现它的正确代码。
我的运输行动控制器是这样的:
public function actionCheckout_shipping()
{
$address_id = Yii::app()->session->get('address_id');
if(!empty($address_id))
$address = Address::model()->findByPk($address_id);
else
$address = new Address;
$orders=new Orders;
$orders_id = Yii::app()->db->createCommand("SELECT MAX('orders_id') FROM orders_temp")->queryScalar();
$orders->orders_id=$orders_id+1;
$orders->customer_id = 1;
$orders->billing_address_id = Address::model()->address_id;
$orders->shipping_address_id = Address::model()->address_id;
$orders->payment_details_id = 1;
$orders->order_date = date('d-m-y');
$orders->shipping_amount= NULL;
$orders->total_amount = 100;
$orders->created_date = date('d-m-y');
$orders->save();
$this->render('checkout_shipping', array('address'=>$address,'selected_address'=>$selected_address),false,true);
在用户完成填写送货地址表格后,我已插入 ORDERS 表的代码以填写它。
好的,我在我的 ORDERS 模型中使用了这样的关系:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'billingAddress' => array(self::BELONGS_TO, 'Address', 'billing_address_id'),
'shippingAddress' => array(self::BELONGS_TO, 'Address', 'shipping_address_id'),
);
}