|
File: /pages/tutorials/database/database-08.php
<?php class Database08 extends Page { public function InitializeComponent() { parent::$PAGE_TITLE = "Tutorial : Form loaded by a query and foreign key"; // With this technique it will be possible to // create easily a formular with all the fields // of a SqlDataView object. // Create table $this->form = new Form($this); // Define Form properties (width, display, ...) // You need to configure foreign key to display // the foreign key value (and not the id) $properties = array( UserDbTable::FIELD_USER_ID => array( "display" => false ), UserDbTable::FIELD_NAME => array( "width" => 150, "strip_tags" => true, "allowable_tags" => "<b><i>", "title" => "Name", // If you want to use TextArea as TextBox // (See below for more information) "wspobject" => TextArea ), UserDbTable::FIELD_BIRTHDAY => array( "width" => 150, "title" => "Birthday" ), UserDbTable::FIELD_SEX_ID => array( "fk_attribute" => SexDbTable::FIELD_VALUE, "width" => 150, "align" => RowTable::ALIGN_LEFT, "title" => "Sex" ), UserDbTable::FIELD_FAVORITE_PAGE_ID => array( "fk_attribute" => FavoritePageDbTable::FIELD_NAME, "fk_where" => UserDbTable::FIELD_ACTIF." = 1", "fk_orderby" => UserDbTable::FIELD_NAME, "width" => 150, "title" => "Page" ), UserDbTable::FIELD_ACTIF => array( "title" => "Actif" ) ); // It's possible to specify the type of the field with // the property wspobject // The authorized type are: TextBox, ComboBox, CheckBox, // Calendar, TextArea, Editor. // Advanced properties to use Editor: // "wspobject" => "Editor", // "editor_param" => Editor::TOOLBAR_NONE // Advanced properties to use ComboBox: // "wspobject" => "ComboBox", // "combobox_values" => // array(array('value' => 'Test', 'text' => 'Test'), // array('value' => 'Test2', 'text' => 'Test2'), // array('value' => 'Test3', 'text' => 'Test3')) // If you want to specify an id on each line of the Form: // "row_id" => "row_of_my_field", // Load data from query $sql = new SqlDataView(new UserDbTable()); $sql->addOrder(UserDbTable::FIELD_USER_ID, SqlDataView::ORDER_ASC); $sql->setLimit(0, 1); $this->form->loadFromSqlDataView($sql,$properties); // Create Table for the buttons $btn_table = new Table(); $btn_table->addRow(); $btn_table->setWidth(250); $btn_table->setDefaultAlign(RowTable::ALIGN_CENTER); // Add Insert, Update and Delete buttons $btn_add = new Button($this->form); $btn_add->setValue("Insert")->onClick("insertUser"); $this->btn_upd = new Button($this->form); $this->btn_upd->setValue("Update"); $this->btn_upd->onClick("updateUser"); $this->btn_del = new Button($this->form); $this->btn_del->setValue("Delete"); $this->btn_del->onClick("deleteUser"); $this->btn_del->onClickJs("if (confirm('Sure?')==false) { return false; }"); $btn_table->addRowColumns($btn_add, $this->btn_upd, $this->btn_del); $this->form->setContent($btn_table); $this->render = $this->form; } public function Loaded() { $it = $this->form->getDataRowIterator(); if ($it->getRowsNum() == 0) { $this->btn_upd->hide(); $this->btn_del->hide(); } } public function insertUser($sender) { if ($this->form->insertFormFromSqlDataView()) { $dialog = new DialogBox("Message", "User inserted"); $dialog->activateCloseButton(); $this->addObject($dialog); } } public function updateUser($sender) { if ($this->form->updateFormFromSqlDataView()) { $dialog = new DialogBox("Message", "User updated"); $dialog->activateCloseButton(); $this->addObject($dialog); } } public function deleteUser($sender) { if ($this->form->deleteFormFromSqlDataView()) { $dialog = new DialogBox("Message", "User deleted"); $dialog->activateCloseButton(); $this->addObject($dialog); } } } ?>
Go back to tutorial of the componant Database
|
drop table if exists `user`; drop table if exists `sex`; drop table if exists `favorite_page`;
/*==============================================================*/ /* table: `user` */ /*==============================================================*/ create table `user` ( user_id int not null auto_increment, name varchar(50), birthday date, sex_id int, favorite_page_id int, actif boolean, primary key (user_id) ) engine = innodb;
/*==============================================================*/ /* table: `sex` */ /*==============================================================*/ create table `sex` ( sex_id int not null auto_increment, value varchar(20), primary key (sex_id) ) engine = innodb;
insert into `sex` (value) values('Man'); insert into `sex` (value) values('Woman');
/*==============================================================*/ /* table: `favorite_page` */ /*==============================================================*/ create table `favorite_page` ( favorite_page_id int not null auto_increment, name varchar(20), url varchar(255), actif boolean, primary key (favorite_page_id) ) engine = innodb;
insert into `favorite_page` (name, url, actif) values('WebSite-PHP', 'http://www.website-php.com', true); insert into `favorite_page` (name, url, actif) values('Meteo Europ', 'http://www.meteo-europ.com', true); insert into `favorite_page` (name, url, actif) values('OracleRef', 'http://www.oracleref.com', true); insert into `favorite_page` (name, url, actif) values('WebSite-PHP', 'http://www.website-php.com', false); insert into `favorite_page` (name, url, actif) values('Meteo Europ', 'http://www.meteo-europ.com', false); insert into `favorite_page` (name, url, actif) values('OracleRef', 'http://www.oracleref.com', false);
alter table `user` add constraint fk_ref_user_sex foreign key (sex_id) references `sex` (sex_id);
alter table `user` add constraint fk_ref_user_favorite foreign key (favorite_page_id) references `favorite_page` (favorite_page_id);
|
|
|
|
|