Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert coding query
    text
    copied!<p>I need to auto insert the next <code>orders_id</code> into an <code>Orders</code> table in a database. <code>orders_id</code> is not the primary Key, and it does not auto-increment </p> <p>I need to query the database, find the last (highest) id value, increment 1, and insert it in Orders table in database. Actually, I have a shipping action which will provide address of shipping of orders. So as soon as the user fills address form and move to payments page, I want to simultaneously fill my <code>Orders</code> table by <code>max(orders_id)+1</code>.</p> <p>I have built relations between <code>Address</code> table and <code>Orders</code> table but my <code>orders</code> Table is not getting populated. Please detail me correct codes to implement it in my controller in Yii.</p> <p>My controller for shipping action goes like this:</p> <pre><code>public function actionCheckout_shipping() { $address_id = Yii::app()-&gt;session-&gt;get('address_id'); if(!empty($address_id)) $address = Address::model()-&gt;findByPk($address_id); else $address = new Address; $orders=new Orders; $orders_id = Yii::app()-&gt;db-&gt;createCommand("SELECT MAX('orders_id') FROM orders_temp")-&gt;queryScalar(); $orders-&gt;orders_id=$orders_id+1; $orders-&gt;customer_id = 1; $orders-&gt;billing_address_id = Address::model()-&gt;address_id; $orders-&gt;shipping_address_id = Address::model()-&gt;address_id; $orders-&gt;payment_details_id = 1; $orders-&gt;order_date = date('d-m-y'); $orders-&gt;shipping_amount= NULL; $orders-&gt;total_amount = 100; $orders-&gt;created_date = date('d-m-y'); $orders-&gt;save(); $this-&gt;render('checkout_shipping', array('address'=&gt;$address,'selected_address'=&gt;$selected_address),false,true); </code></pre> <p>I have inserted the code for ORDERS table to fill it after a user completes the filling of shipping address form.</p> <p>ok,I have used the relations in my ORDERS model like this:</p> <pre><code>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' =&gt; array(self::BELONGS_TO, 'Address', 'billing_address_id'), 'shippingAddress' =&gt; array(self::BELONGS_TO, 'Address', 'shipping_address_id'), ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload