Datei: /pages/tutorials/database/database-09.php
<?php class Database09 extends Page { public function InitializeComponent() { parent::$PAGE_TITLE = "Tutorial : Create Form with database model object"; // It's another technique than use // Form->loadFromSqlDataView() // In this tutourial, we use ModelViewMapper // to create all the input fields and to save the // data, it's only a simple call of the method // save() of the mapper. // The big advantage of this solution, is you can // specify the display that you want. For example // if you want to split the fields in different area // it' possible because you need to define where the // fields will displayed. // Create table $form = new Form($this); // Load user object and creat mapper $user = new UserObj(); $this->mapper = new ModelViewMapper($form, $user, new UserDbTable()); // Define Mapper fields 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( "wspobject" => TextArea ), UserDbTable::FIELD_SEX_ID => array( "fk_attribute" => SexDbTable::FIELD_VALUE, ), UserDbTable::FIELD_FAVORITE_PAGE_ID => array( "fk_attribute" => FavoritePageDbTable::FIELD_NAME, "fk_where" => UserDbTable::FIELD_ACTIF." = 1", "fk_orderby" => UserDbTable::FIELD_NAME, ) ); // Create fields $fields_array = $this->mapper->prepareFieldsArray( $properties); // Create formular table // Note: Here you can define more advanced display. // It's the advantage of this method, you can define // the display that you want $table = new Table(); foreach ($fields_array as $attribute => $field) { $table->addRowColumns($attribute.": ", $field); } // Add Insert button $btn_add = new Button($form); $btn_add->setValue("Insert")->onClick("insertUser"); $row = $table->addRowColumns($btn_add); $row->setColspan(2)->setAlign(RowTable::ALIGN_CENTER); $form->setContent($table); $this->render = $form; } public function insertUser($sender) { try { $this->mapper->save(); $dialog = new DialogBox("Message", "User inserted"); } catch (Exception $ex) { $dialog = new DialogBox("Message", "Error user insert: ". $ex->getMessage()); } $dialog->activateCloseButton(); $this->addObject($dialog); } } ?>
Zurück zum Tutorial der Komponente Database
|