WebSite-PHP Framework PHP
Multi language
Simple Ajax mode
No HTML, no JavaScript
URL rewriting
Mail sending
Sitemap - RSS - Web service
Download WebSite-PHP FrameWork now
 


Loading
 


Tutorial : Create Form 
with database model object

Tutorials

>

Tutorial Database

>

Tutorial : Create Form with database model object


Database-09: Tutorial of the composant Database of the PHP FrameWork WebSite-PHP.

Tutorial : Create Form with database model object:

name: 
birthday: 
sex_id: 
favorite_page_id: 
actif: 


Go back to tutorial of the componant Database



Source code

Source code


File: /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.":&nbsp;"$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);
    }
}
?>


Go back to tutorial of the componant Database


Share

 


Copyright © 2009-2024 WebSite-PHP Framework PHP
Home Documentation Download Quick start Tutorials Wiki Issue Tracker