Radria Core for PHP5
With PHP 5 we have introduced a new object: DataObject
Its a Data Access Object that we uses as an Object Relation Mapping.
To illustrate its usage we will create a small movie database.
Create a new project called movie with the SiteManager, open the project install all the base packages and the following database related packages:
pb_addon_database report_template phpmyadmin
Lets create the Movie table in the WebIDE or dump the SQL below in phpMyAdmin
CREATE TABLE `movie` ( `idmovie` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(60) NOT NULL, `type` varchar(60) NOT NULL, `format` varchar(10) NOT NULL, `director` varchar(70) NOT NULL, `release` date NOT NULL, `duration` time NOT NULL, PRIMARY KEY (`idmovie`) )
And a base class for our Movie object.
<?php /** * Class movie */ class Movie extends DataObject { public $table = "movie"; protected $primary_key = "idmovie"; } ?>
Save the class in the class folder of your project as Movie.class.php
Then you can create a configuration file for your project create a file as includes/movie.conf.inc.php with
<?php // Configuration of movie project include_once("class/Movie.class.php"); ?>
Now lets initiate a Movie object and display a form so we can add data into that table.
Open the PageBuilder, create a new page called movies.php, add a script, click on it and replace the code with the following:
<?php echo "Add a Movie"; $do_movie = new Movie(); $do_movie->form(); ?>
Click on the save button of the syntax editor to save the source code and then the Save button in the main PageBuilder tool bar to save the page. Finally click on Preview to test the form.
You can view the records you entered from the WebIDE in the Table Tab by selecting the table and clicking the View button.
Now lets display the content of the data we just entered. Lets create a new script in the PageBuilder and add the following:
$do_movie = new Movie();
$do_movie->view();
Now if you want to edit one of the record in a form you can do the following
<?php $do_movie = new Movie(); $do_movie->getId(1); echo "Editing movie:".$do_movie->name; $do_movie->form(); ?>
$do_movie→getId(1) will load the record with primary key idmovie = 1.
Lets create a Comment object linked to the Movie. So each movies can have multiple comments.
CREATE TABLE `comments` ( `idcomments` int(10) NOT NULL AUTO_INCREMENT, `idmovie` int(10) NOT NULL, `author_name` varchar(50) NOT NULL, `author_email` varchar(80) NOT NULL, `comment_content` text NOT NULL, PRIMARY KEY (`idcomments`), UNIQUE KEY `idcomments_2` (`idcomments`), KEY `idcomments` (`idcomments`) )
Then we create the class in class/Comment.class.php:
<?php /** * Class Comment */ class Comment extends DataObject { public $table = "comments"; protected $primary_key = "idcomments"; } ?>
in the movie configuration project includes/movie.conf.inc.php we add the new class:
<?php // Configuration of movie project include_once("class/Movie.class.php"); include_once("class/Comment.class.php"); ?>
To display all the comments of a movie just insert in a web page:
<?php $movie = new Movie(); $movie->getId(1); $comment = $movie->getChildComments(); echo "Comments for: ".$movie->name; while($comment->next()) { echo "<hr>".$comment->comment_content; } ?>
The registry is an object that describe how data elements (table fields) should be display in a form or a web page.
Each element is associated with a type and that type is described in a class that will generate the appropriate HTML.
If you have already open the Movie table in WebIDE you should have a registry that got auto generated for it and can view it in the Registry tab.
If not just execute the following PHP code in a web page:
<?php $reg = new Registry($GLOBALS['conx']); $reg->registryFromTable("movie"); $reg->serializeToXML("movie"); ?>
it will create and serialize a registry for the movie object (or table) called movie.
We will get in more details about the registry later on.
This example will display in details how to display a custom form using PHP and the Dataobject.
Add this code example to one of your web page:
<?php $movie2 = new Movie(); $movie2->newUpdateForm("movie2"); $movie2->getId(1); $movie2->setRegistry("movie"); $movie2->setApplyRegistry(true, "Form"); echo $movie2->form->getFormHeader(); echo $movie2->form->getFormEvent(); ?> this is a custom form. <br> <?php echo $movie2->name; ?> <? echo $movie2->form->getFormFooter("Save"); $movie2->sessionPersistent("movie2", "index.php", 2333); ?>
Now lets explain a bit what this is doing. Simply, it load a data record and display a form field with the value of that record and allow to edit it.
Now more details on what each lines does.
$movie2->newUpdateForm("movie2");
Initiate a form object attached to the movie2 session Persistent object. (set a few lines down) The other possible method are:
newAddForm();
newForm();
$movie2->getId(1);
Load the first record in the movie2 instance.
$movie2->setRegistry("movie");
Load a registry object called: movie
$movie2->setApplyRegistry(true, "Form");
Apply the registry to all data access on the movie2 object in Form context. By default when calling a variable of the object it display its value, when you call this method it will then apply the Registry object field type associated with that variable. Currently we have 2 contexts: Form and Disp one for forms the other for web page display.
echo $movie2->form->getFormHeader(); echo $movie2->form->getFormEvent();
getFormHeader() Generate and echo first the form header, this is simple the form tag, <form…>_ you can replace it by your own form tag, the only requirement is the action=“eventcontroler.php”
The getFormEvent() will add all the hidden fields needed by the eventcontroler to process the request.
<?php echo $movie2->name; ?>
This in general would just echo the value of the movie name variable. But since we have enabled the registry with setApplyRegistry() now a class will be loaded and executed to generate HTML code around the value. In this case it will be a simple input text field.
echo $movie2->form->getFormFooter("Save");
This will simple display the submit tag and close the form. Nothing else so it can be replaced by your own html code.
$movie2->sessionPersistent("movie2", "index.php", 2333);
This is an important one. It will save in the session the object we just created with all its values. The first parameter is the name of the object in the session. So it can be called any where with: $_SESSION['movie'] The second is on which page the movie2 object should die. The third parameter is the number of second before that object is garbaged collected.
See session_persistent for more details.
The examples above are using a template to display the view. A base set of templates are available in the report_template Radria package.
To know which is the default template you can do.
<?php echo RADRIA_DEFAULT_REPORT_TEMPLATE; ?>
The report_template package has the following view templates
You can load those templates with the setViewTemplate() method:
<?php $do_movie = new Movie(); $do_movie->setViewTemplate("search_report_detail"); $do_movie->view(); ?>
View templates are stored in the report/ folder they are serialized Report Objects.
$do_movie = new Movie();
$do_movie->getAll(); // run a query that create a resource with all the records
$do_movie->view_movie_listing();
The view_ magic method will instantiate a Report object using serialized object from the /report/movie_listing.report.xml file.
$do_movie = new Movie();
$do_movie->getid(3); // run a query that create a resource with all the records
$do_movie->form_short_form();
The view_ magic method will instantiate a ReportForm object using serialized object from the /form/short_form.form.xml file.
<?php $my_movie = new Movie(); $my_movie->getid(1); $my_movie->sessionPersistent("my_session_movie", "index.php", 300); ?>
This will make this $my_movie object accessible in the session for 300 seconds or until the user reach the index.php page.
After the code above is executed the $my_movie object can be accessed with all its variables and properties with $_SESSION['my_session_movie']
<?php echo $_SESSION['my_session_movie']->name; ?>
This will display the name of the movie with idmovie=1.
You can create method that will be triggered by Event objects in the user interface.
Update your class/Movie.class.php
<?php /** * Class movie */ class Movie extends DataObject { public $table = "movie"; protected $primary_key = "idmovie"; function eventIncreaseDuration(EventControler $evctl) { $this->duration = $this->duration*2+1; $this->update(); $evctl->SetUrlNext($evctl->getParam("goto")); } } ?>
Once this method is created it can be used in Event objects on a web page to let the user trigger that event. Add this script in a web page called movies.php
<?php $my_movie = new Movie(); $my_movie->getid(1); $my_movie->sessionPersistent("my_movie", "index.php", 300); $e_calc = new Event("my_movie->eventIncreaseDuration"); $e_calc->addParam("goto", "movies.php"); ?> <?php echo $e_calc->getLink("Increase duration")." for "; echo $_SESSION['my_movie']->name; ?>
This will display a link and when the user click on the link: Recalculate duration it will double the duration or the movie with primary key is 3.
$do_movie = new Movie();
$do_movie->addNew();
$do_movie->name = "Blade Runner";
$do_movie->type = "SF";
$do_movie->format = "DVD";
$do_movie->director = "Ridley Scott";
$do_movie->release = "1981";
$do_movie->duration = "2:30";
$do_movie->add();
$do_movie = new Movie();
$do_movie->getId(3); // Primarykey id of the record to update
$do_movie->release = "1984";
$do_movie->duration = "1:40";
$do_movie->update();
$do_movie = new Movie();
$do_movie->getId(3); // Primarykey id of the record to delete
$do_movie->delete();Testing a bog