how to query rows from another table and insert in another

Hi to all yii developers

I need some help

I have two tables

Cart and orders

I need to query each row in cart table and insert it into orders table under specified conditions when I do as below the first row is being inserted and not others.




$orderContent = new  CustomerOrdersContent();

    $sql = "SELECT * FROM {{customer_cart}} WHERE shop_id = :shopId AND user_session_id =    :Token AND status = :Status";

    $command = Yii::app()->db->createCommand($sql);

    $command->bindValue(":shopId", $shop_id, PDO::PARAM_INT);

    $command->bindValue(":Token", $uid, PDO::PARAM_STR);

    $command->bindValue(":Status", CustomerCart::STATUS_CART, PDO::PARAM_INT);

    $dataReader=$command->query();

    

    foreach($dataReader as $item):

    $orderContent['order_id'] = $model->id;

    $orderContent['product_id'] = $item['product_id'];

    $orderContent['product_type'] = $item['type_id'];

    $orderContent['quantity'] = $item['quantity'];

    $orderContent['price_quantity'] = '0';

    $orderContent['create_time'] = time();

    $orderContent['update_time'] = time();

    $orderContent->save();

    endforeach;



I have just hard coded price_quantity to zero but this need to come from product table as it represent price per product. If I would use Ar it would be $item->product->price.

I have tried AR in the above case but now the insert does only for last row in cart table.

I have the order table which stores total items quantity and their total price, then am able to get the order_id which is model->id

But the problem insert each item from cart with the order id.

I have solved the issue by myself

This way




foreach($cart as $item):

    $orderContent = new ShopCustomerOrdersContent();

    $orderContent['order_id'] = $model->id;

    $orderContent['product_id'] = $item->product_id;

    $orderContent['product_type'] = $item->type_id;

    $orderContent['quantity'] = $item->quantity;

    $orderContent['price_quantity'] = $item->product->price;

    $orderContent['create_time'] = time();

    $orderContent['update_time'] = time();

    $orderContent['author_id'] = '0';

    $orderContent->save();

    endforeach;



The model is supposed to be inside the foreach loop.