插入编码查询

时间:2012-12-08 06:22:49

标签: php mysql yii

我需要自动将下一个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'),
        );
    }

2 个答案:

答案 0 :(得分: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-&gt; addressOrders-&gt; orders_id;等。

模型“OrdersTemp”的更改是

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

基本错误就像您使用的表名一样  1. orders_temp和表名是“订单”  2.在表结构中设置NOT NULL时不要插入NULL。  3.您没有在订单表b中获取值,因为没有值插入表中。

希望你找到这个有用的

答案 1 :(得分:0)

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