Propel

Overview

Propel is a free, open-source object-relational mapping toolkit written in PHP. It is also an integral part of the PHP framework Symfony and was the default ORM up to, and including version 1.2.


Important Propel commands

vendor/bin/propel init

  • Initialize propel installation

vendor/bin/propel reverse

  • Create a new schema.xml with the updated database information (inside generated-reversed-database/ directory)

vendor/bin/propel model:build

  • Rebuild the models according to schema.xml


Many-to-many

To create the many to many relationship make sure the conjuction table has

isCrossRef="true"

in their table schema


Validate functionality

The schema.xml file lives in data/models/ directory, and has the structure of the database. One of the most important additions to schema.xml is the validate behavior: 

  <table name="library" idMethod="native" phpName="Library">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
    <column name="name" phpName="Name" type="VARCHAR" size="128" required="true"/>
    <unique name="name">
      <unique-column name="name"/>
    </unique>
    <vendor type="mysql">
      <parameter name="Engine" value="InnoDB"/>
    </vendor>
    <behavior name="validate">
      <parameter name="rule1" value="{column: name, validator: NotNull}"/>
      <parameter name="rule4" value="{column: name, validator: Unique}"/>
    </behavior>
  </table>

The validate behavior must go after the vendor section and before the end of the table which you're adding the validation rules. Once the validation rules are set in the schema.xml, save the file and run vendor/bin/propel model:build to rebuild the models according to the modified schema, and then run composer dump-autoload -o to load up the new models. The user model will now have new functions, such as validate and getValidationFailures (validate returns true if all rules were followed, and getValidationFailures returns an array of errors if validate failed). If you want to know more about validate you can look at http://propelorm.org/documentation/behaviors/validate.html.


Propel notes

When running vendor/bin/propel init make sure you have your apache server on, or propel won't be able to establish a connection to the database; an example run of vendor/bin/propel init can be found here. Once you initialize Propel, two new folders will be created in the current directory (generated-conf/ and generated-sql/), move them into the data/ directory to keep the directory structure consistent. To learn more about propel and it's important functions look at http://propelorm.org/documentation/.

Comments (0)

Search Here