You are on page 1of 1

Open-Source PHP5 MVC Framework

Agile Development

MODEL Symfony uses, by default, Propel as the ORM and Creole as the database abstraction layer.
More about propel: http://propel.phpdb.org/docs/user_guide/

DATABASES SUPPORTED (Creole) MODEL CLASSES Creole Column Types


MySQL SQLServer BOOLEAN = 1 VARBINARY = 13
The schema is used to build the model classes of the ORM layer through the BIGINT = 2 NUMERIC = 14
PostgreSQL SQLITE
command-line task: $ symfony propel-build-model SMALLINT = 3 BLOB = 15
Oracle
TINYINT = 4 CLOB = 16
BASE CLASSES lib/model/om/ INTEGER = 5 LONGVARCHAR = 17
CONNECT TO DATABASE CHAR = 6 DECIMAL = 18
BaseArticle.php BaseComment.php VARCHAR = 7 REAL = 19
BaseArticlePeer.php BaseCommentPeer.php TEXT = 17 BINARY = 20
propel.ini /myproject/config Base classes are the ones directly generated from the schema. Never FLOAT = 8 LONGVARBINARY = 21
propel.targetPackage = lib.model modify them, since every new build of the model will completely erase DOUBLE = 9 YEAR = 22
propel.packageObjectModel = true these files. DATE = 10 ARR = 23
propel.project = myproject TIME = 11 OTHER = -1
propel.database = mysql TIMESTAMP = 12
propel.database.createUrl = mysql://localhost/
DATA MODEL CLASSES lib/model/
propel.database.url = mysql://localhost/myproject Article.php Comment.php
... ArticlePeer.php CommentPeer.php Special Date Columns
databases.yml Inherit from the Base ones. When the propel-build-model task is called on created_at
/myproject/config
an existing model, these classes are not modified. So this is where you can store a timestamp of the date when the
prod: add custom methods and properties to the model objects. record was created
propel: Example: here is the content of the newly created Article.php file:
param: updated_at
require_once 'model/om/BaseArticle.php'; updated each time the record itself is
host: mydataserver class Article extends BaseArticle{
username: myusername updated
}
password: mypassword
It inherits all the methods of the BaseArticle class, but a modification in
the model will not affect it.
Object Class Methods
all:
propel: The accessors and mutators use a
class: sfPropelDatabase Article.php camelCase variant of the column names,
OBJECT CLASSES Comment.php
param: so the getTitle() method retrieves the
phptype: mysql Represent a record in the database. They give access to the columns of a value of the title column.
hostspec: localhost record and to related records. Accessors:
database: blog get[MyColumnName]()
username: login Object Class Constructor - new
Retrieve the column value
password: passwd To create a new object: $myobject->getMyColumn();
port: 80 $myobject = new MyTable();
getByName($name)
encoding: utf8
ArticlePeer.php
persistent: true PEER CLASSES CommentPeer.php Mutators:
You can define distinct settings for the set[MyColumnName]($value)
Contain static methods to operate on the tables. Provide a way to retrieve
prod, dev, and test environments, or any To set one field:
records from the tables. Their methods usually return an object or a $myobject->setMyColumn(”value”);
other environment in your application. collection of objects of the related object class.
This configuration can also be overridden fromArray($array)
per application, in The methods of the Peer classes will be called with a :: (for static method To set several fields at one time:
apps/myapp/config/databases.yml call) instead of the usual -> (for instance method call) $myobject->fromArray(array(
'myColumn1’ => 'Some content',
Retrieving Records by Primary Key 'myColumn2' => 'Some content’,
$myobject=myTablePeer::retrieveByPk(7); ));
TRANSACTIONS
retriving by primary key that consist of more than one column: setByName($name, $value)
public function save($con = null){ $myobject=myTablePeer::retrieveByPk(1, 12);
$con = Propel::getConnection(); save()
try{ select multiple objects based on their primary keys: save the data into the database
$con->begin(); $myobject=myTablePeer::retrieveByPKs($arrayOfPrimaryKeys); $myobject->save();
$ret = parent::save($con);
// update interested_users in question table Retrieving Records with Criteria isNew()
$question = $this->getQuestion(); $c = new Criteria(); check if an object is new
$myobject->isNew();
$interested_users=$question->getInterestedUsers(); $c->add(CommentPeer::AUTHOR, 'Steve');
$question->setInterestedUsers($interested_users+1); $c->addAscendingOrderByColumn(CommentPeer::DATE); isModified()
$question->save($con); $comments = CommentPeer::doSelect($c); check if an object has been modified and
$con->commit(); //$comments is an array of objects of class Comment deserves saving
return $ret; $myobject->isModified();
} delete()
catch (Exception $e){ METADATA CLASSES lib/model/map/
delete records from the database
$con->rollback(); ArticleMapBuilder.php CommentMapBuilder.php $myobject->delete();
throw $e;
} Contains metadata information about the table that is needed for the isDeleted()
} runtime environment. check if an object is deleted in database
$myobject->isDeleted();

DATABASE SCHEMA (sample) myproject/config/

<?xml version="1.0" encoding="UTF-8"?>


<database name="propel" defaultIdMethod="native" noXsd="true" package="lib.model">
id propel: <table name="blog_article" phpName="Article">
blog_article

blog_article: <column name="id" type="integer" required="true" primaryKey="true"autoIncrement="true" />


title (0) _attributes: { phpName: Article } <column name="title" type="varchar" size="255" />
content (0) id: <column name="content" type="longvarchar" />
table structure

created_at (0) title: varchar(255) <column name="created_at" type="timestamp" />


schema.xml
schema.yml

content: longvarchar </table>


created_at: <table name="blog_comment" phpName="Comment">
blog_comment: <column name="id" type="integer" required="true" primaryKey="true"autoIncrement="true" />
_attributes: { phpName: Comment } <column name="article_id" type="integer" />
id: <foreign-key foreignTable="blog_article">
id <reference local="article_id" foreign="id"/>
blog_comment

article_id:
article_id (FK) author: varchar(255) </foreign-key>
content: longvarchar <column name="author" type="varchar" size="255" />
author (0) <column name="content" type="longvarchar" />
content (0) created_at:
<column name="created_at" type="timestamp" />
created_at (0) </table>
</database>

http://andreiabohner.wordpress.com This cheat-sheet is not an official part of the symfony documentation

You might also like