0

我需要将下一个自动插入到数据库中orders_idOrders表中。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'),
        );
    }
4

2 回答 2

1

放入你的控制器。

//$address_id = Yii::app()->session->get('address_id');// Temporary I set
$address_id = 1; // Remove this

        if(!empty($address_id))
    {
           $address = Address::model()->findByPk($address_id);
    }
        else
    {
        echo "error address id should be there";                
    }

  $order_id = Yii::app()->db->createCommand("SELECT MAX(orders_id) FROM orders")->queryScalar();
 $order_id = ($order_id)?($order_id+1):1;
      $orders=new OrdersTemp;
 $orders->orders_id = $order_id;
 $orders->customer_id = 1;
     $orders->billing_address_id = $address_id;
     $orders->shipping_address_id = $address_id;
     $orders->payment_details_id = 1;
     $orders->order_date = date('Y-m-d G:i:s');
     $orders->shipping_amount= '44';
     $orders->total_amount = '10';
 $orders->shipping_address_id = 1;           
     $orders->created_date = date('Y-m-d G:i:s');
 $orders->modified_date = date('Y-m-d G:i:s');
     $orders->save();
 echo $address->address_id;
 echo "<br />";
 echo $address->addressOrders->orders_id;
 echo "<br />";             
 echo $address->addressOrders->total_amount;
     $this->render('checkout_shipping', array('address'=>$address),false,true);

将此函数替换为 CartController n 检查视图文件中的回显,例如 echo $address->addressOrders->orders_id; 等等

模型“OrdersTemp”的变化是

    public function tableName()
{
    return 'orders';
}

存在基本错误,例如您使用的表名 1. orders_temp 和表名是“orders” 2. 不要插入 NULL,因为您在表结构中设置了 NOT NULL。3. 您没有在订单表 b 中获得值,因为没有值插入到表中。

希望你觉得这个有用

于 2012-12-08T11:01:33.857 回答
0

insert into orders (orders_id) values (select max(orders_id)+1 from orders);

于 2012-12-08T06:38:11.983 回答