Persistence Framework

From struts4php

Jump to: navigation, search

Persistence Framework

struts4php provides its own Persistence Framework. The persistence framework is a OR-Mapper that hides the data handling by wrapping it into objects and functions. The configuration is XML based and not too far away from hibernate.

Example configuration:

<?xml version="1.0" encoding="ISO-8859-1"?>
 <storable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://pear.struts4php.org/schema/entity-1.0.0.xsd" name="Address" type="entity">
     <description>Write your own description here</description>
     <members>
         <member name="addressId" sqlname="address_id" type="integer" initial="null"/>
         <member name="localeIdFk" sqlname="locale_id_fk" type="integer" initial="null"/>
         <member name="name" sqlname="name" type="string" initial="null"/>
     </members>
     <querys>
         <query>
             <description><![CDATA[Returns all addresses with the locale id passed as parameter.]]></description>
             <method name="findAll">
                 <params>
                     <param name="localeIdFk" type="integer"/>
                 </params>
             </method>
             <result-type>ArrayList</result-type>
             <sql><![CDATA[SELECT * FROM address WHERE locale_id_fk = ?]]></sql>
         </query>
     </querys>
     <references>
         <reference name="address-has-address-locales">
             <method-name>AddressLocale</method-name>
             <multiplicity>many</multiplicity>
             <source>
                 <storable-name>AddressLocale</storable-name>
                 </source>
                 <target>
                     <member-name>addressId</member-name>
                 </target>
             </source>
         </reference>
     </references>
     <tables>
         <table type="InnoDB" name="address">
             <keys>
                 <pk name="address_pk" field="address_id"/>
                 <fk name="address_fk_01" field="gender_id_fk" target-table="gender" target-field="gender_id" on-delete="cascade"/>
                 <index name="address_idx_01" field="gender_id_fk"/>
             </keys>	
             <fields>
                 <field name="address_id" type="int" length="10" nullable="false" autoincrement="true"/>
                 <field name="gender_id_fk" type="int" length="10" nullable="false"/>
                 <field name="name" type="varchar" length="50" nullable="true"/>
             </fields>
         </table>
     </tables>
 </storable>

Source Code Generator

The files for the persistent objects can be generated or can be written by hand. It is recommended to generate the files based on the configuration files like the example above. Therefore the generator package has to be installed. The generator package is also a PEAR package, that can be installed over the PEAR installer and executed as a phing task. You can install the generator package with:

pear channel-discover pear.struts4php.org
pear install struts4php/generator

The generator uses a configuration file for specifying the source path with the XML definitions of the classes to generate. The generator configuration looks like this:

<?xml version="1.0"?>
 <conf>
     <includes>
         <include id="include_path_manager">epb4php/manager/</include>
         <include id="include_path_storables">epb/storables/</include>
         <include id="include_path_assembler">epb/assembler/</include>
         <include id="include_path_homes">epb/homes/</include>
         <include id="include_path_utils">epb/utils/</include>
         <include id="include_path_exceptions">epb/exceptions/</include>
         <include id="include_path_value_objects">common/valueobjects/</include>
         <include id="include_path_ejbactions">epb/epbactions/</include>
         <include id="hibernate_package_cfg">com.brainguide.importer.model</include>
         <include id="hibernate_package_dao">com.brainguide.importer.model.dao</include>
         <include id="hibernate_package_domain">com.brainguide.importer.model.domain</include>
         <include id="datasource">epb/domain/datasource.php</include>
     </includes>
     <plugins>
         <plugin name="StorableLocalHomePlugin" type="XSLTPlugin" include="generator/plugins/xsltplugin.php">
             <input>xml/entity/</input>
             <output>gen-src/epb/homes/</output>
             <xsl>xsl/storablelocalhome.xsl</xsl>
             <suffix>localhome</suffix>
             <prefix />
         </plugin>
     </plugins>
 </conf>
Personal tools