|
Database-02: Tutoriel du composants Database du FrameWork PHP WebSite-PHP. Tutorial : Insert customer
and products:
Customer : customer_id : 1 name : My Customer address : 5th, street principal Product :
product_id
|
name
|
price
|
1
|
Coca
|
0.5 €
|
2
|
Hamburger
|
5.2 €
|
3
|
Water
|
0.8 €
|
4
|
Milk
|
1 €
|
|
Retourner aux tutoriels du composants Database
|
Fichier: /pages/tutorials/database/database-02.php
<?php class Database02 extends Page { public function InitializeComponent() { parent::$PAGE_TITLE = "Tutorial : Insert customer and products"; $this->render = new WSPObject(); DataBase::getInstance()->beginTransaction(); // Insert customer (if not exists in DB) $cutomer = new CustomerObj(1); $cutomer->setName("My Customer"); $cutomer->setAddress("5th, street principal"); if (!$cutomer->isDbObject()) { $cutomer->save(); } $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/>"); // Insert products (if not exists in DB) $coca = new ProductObj(1, "Coca", 0.5); if (!$coca->isDbObject()) { $coca->save(); } $hamburger = new ProductObj(2, "Hamburger", 5.2); if (!$hamburger->isDbObject()) { $hamburger->save(); } $water = new ProductObj(3, "Water", 0.8); if (!$water->isDbObject()) { $water->save(); } $milk = new ProductObj(4, "Milk", 1); if (!$milk->isDbObject()) { $milk->save(); } // display all products $this->render->add(new Label( ucfirst(ProductDbTable::TABLE_NAME), true), " :<br/>"); $product_table = new Table(); $product_table->setId("product_table"); $product_table->addRowColumns( ProductDbTable::FIELD_PRODUCT_ID, ProductDbTable::FIELD_NAME, ProductDbTable::FIELD_PRICE) ->setHeaderClass(0); $product_list = new ProductObjList(); $array_product = $product_list->getProductObjectArray(); for ($i=0; $i < sizeof($array_product); $i++) { $product = $array_product[$i]; $product_table->addRowColumns( $product->getProductId(), $product->getName(), $product->getPrice()." €"); } $product_table->activateAdvanceTable(); $this->render->add($product_table); 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);
|
|
|
|
|