|
Database-03: Tutoriel du composants Database du FrameWork PHP WebSite-PHP. Tutorial : Insert order
for custumer 1:
Customer : customer_id : 1 name : My Customer address : 5th, street principal Order :
name
|
quantity
|
price
|
TOTAL PRICE
|
Coca
|
6
|
0.5 €
|
3 €
|
Hamburger
|
1
|
5.2 €
|
5.2 €
|
Water
|
2
|
0.8 €
|
1.6 €
|
Milk
|
1
|
1 €
|
1 €
|
|
Retourner aux tutoriels du composants Database
|
Fichier: /pages/tutorials/database/database-03.php
<?php class Database03 extends Page { public function InitializeComponent() { parent::$PAGE_TITLE = "Tutorial : Insert order for custumer 1"; $this->render = new WSPObject(); DataBase::getInstance()->beginTransaction(); // Insert order / order items for custumer 1 $order = new OrderObj(); $order->setCustomerId(1); $order->setDate(new DateTime()); $order->save(); $order_coca = new OrderItemObj($order->getOrderId(), 1, 6); $order_coca->save(); $order_hamburger = new OrderItemObj($order->getOrderId(), 2, 1); $order_hamburger->save(); $order_water = new OrderItemObj($order->getOrderId(), 3, 2); $order_water->save(); $order_milk = new OrderItemObj($order->getOrderId(), 4, 1); $order_milk->save(); // Display customer $cutomer = $order->getCustomerObject(); $this->render->add(new Label( ucfirst(CustomerDbTable::TABLE_NAME), true), " :<br/>"); $this->render->add(CustomerDbTable::FIELD_CUSTOMER_ID, ":", $cutomer->getCustomerId(), "<br/>"); $this->render->add(CustomerDbTable::FIELD_NAME, ":", $cutomer->getName(), "<br/>"); $this->render->add(CustomerDbTable::FIELD_ADDRESS, ":", $cutomer->getAddress(), "<br/><br/>"); // Display order $this->render->add(new Label( ucfirst(OrderDbTable::TABLE_NAME), true), " :<br/>"); $order_table = new Table(); $order_table->setId("order_table"); $order_table->addRowColumns( ProductDbTable::FIELD_NAME, OrderItemDbTable::FIELD_QUANTITY, ProductDbTable::FIELD_PRICE, "TOTAL PRICE") ->setHeaderClass(0); $array_order = $order->getOrderItemObjectArray(); for ($i=0; $i < sizeof($array_order); $i++) { $order_item = $array_order[$i]; $product = $order_item->getProductObject(); $total_price = $product->getPrice()* $order_item->getQuantity(); $order_table->addRowColumns( $product->getName(), $order_item->getQuantity(), $product->getPrice()." €", $total_price." €"); } $order_table->activateAdvanceTable(); $this->render->add($order_table); // Delete current order $array_item = $order->getOrderItemObjectArray(); for ($i=0; $i < sizeof($array_item); $i++) { $array_item[$i]->delete(); } $order->delete(); DataBase::getInstance()->commitTransaction(); $this->render = new WSPObject($this->render, "<br/>"); } } ?>
Retourner aux tutoriels du composants Database
|
drop table if exists order_item; drop table if exists `order`; drop table if exists product; drop table if exists customer;
/*==============================================================*/ /* table: customer */ /*==============================================================*/ create table customer ( customer_id int not null auto_increment, name varchar(50), address varchar(100), primary key (customer_id) ) engine = innodb;
/*==============================================================*/ /* table: `order` */ /*==============================================================*/ create table `order` ( order_id int not null auto_increment, customer_id int, date datetime, primary key (order_id) ) engine = innodb;
/*==============================================================*/ /* table: order_item */ /*==============================================================*/ create table order_item ( order_id int, product_id int, quantity int ) engine = innodb;
/*==============================================================*/ /* table: product */ /*==============================================================*/ create table product ( product_id int not null auto_increment, name varchar(50), price double, primary key (product_id) ) engine = innodb;
alter table `order` add constraint fk_reference_1 foreign key (customer_id) references customer (customer_id);
alter table order_item add constraint fk_reference_2 foreign key (order_id) references `order` (order_id);
alter table order_item add constraint fk_reference_3 foreign key (product_id) references product (product_id);
|
|
|
|
|