Original PDF Flash format model-symfony-uses,-by-default,-propel-as-the-orm-and-creole-as-...  


Model Symfony Uses, By Default, Propel As The Orm And Creole As ...

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
PostgreSQL
SQLITE
The schema is used to build the model classes of the ORM layer through the
BIGINT = 2
NUMERIC = 14
command-line task: $ symfony propel-build-model
Oracle
SMALLINT = 3
BLOB = 15
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
propel.ini
TEXT = 17
BINARY = 20
/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
DATA MODEL CLASSES
lib/model/
TIMESTAMP = 12
propel.database.createUrl = mysql://localhost/
propel.database.url = mysql://localhost/myproject
Article.php
Comment.php
...
ArticlePeer.php
CommentPeer.php
Special Date Columns
Inherit from the Base ones. When the propel-build-model task is called on
databases.yml
/myproject/config
created_at
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';
host: mydataserver
updated each time the record itself is
class Article extends BaseArticle{
username: myusername
updated
}
password: mypassword
It inherits all the methods of the BaseArticle class, but a modification in
Object Class Methods
the model will not affect it.
all:
propel:
The accessors and mutators use a
class: sfPropelDatabase
Article.php
OBJECT CLASSES
camelCase variant of the column names,
param:
Comment.php
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
Mutators:
CommentPeer.php
set[MyColumnName]($value)
You can define distinct settings for the
Contain static methods to operate on the tables. Provide a way to retrieve
To set one field:
prod, dev, and test environments, or any
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)
The methods of the Peer classes will be called with a :: (for static method
per application, in
To set several fields at one time:
call) instead of the usual -> (for instance method call)
$myobject->fromArray(array(
apps/myapp/config/databases.yml
'myColumn1’ => 'Some content',
Retrieving Records by Primary Key
'myColumn2' => 'Some content’,
TRANSACTIONS
$myobject=myTablePeer::retrieveByPk(7);
));
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);
Retrieving Records with Criteria
isNew()
// update interested_users in question table
$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
$myobject->isModified();
return $ret;
}
METADATA CLASSES
lib/model/map/
delete()
catch (Exception $e){
delete records from the database
$con->rollback();
$myobject->delete();
ArticleMapBuilder.php
CommentMapBuilder.php
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">
propel:
<table name="blog_article" phpName="Article">
blog_article:
<column name="id" type="integer" required="true" primaryKey="true"autoIncrement="true" />
_attributes: { phpName: Article }
<column name="title" type="varchar" size="255" />

id:
<column name="content" type="longvarchar" />
r
e
l

title: varchar(255)
l
<column name="created_at" type="timestamp" />
b
l
o
g
_
a
r
t
i
c
l
e
id
title (0)
content (0)
created_at (0)
t
u
m
content: longvarchar
m </table>
c
.
y
created_at:
.
x <table name="blog_comment" phpName="Comment">
a
a
blog_comment:
<column name="id" type="integer" required="true" primaryKey="true"autoIncrement="true" />
m
m

s
t
r
u
_attributes: { phpName: Comment }
<column name="article_id" type="integer" />
e
e
l
e
h
id:
h <foreign-key foreignTable="blog_article">
b
id
s
c
article_id:
s
c <reference local="article_id" foreign="id"/>
t
a
e
n
t
m
article_id (FK)
author: varchar(255)
</foreign-key>
content: longvarchar
<column name="author" type="varchar" size="255" />
author (0)
created_at:
<column name="content" type="longvarchar" />
content (0)
<column name="created_at" type="timestamp" />
created_at (0)
b
l
o
g
_
c
o
m
</table>
</database>
http://andreiabohner.wordpress.com
This cheat-sheet is not an official part of the symfony documentation

Document Outline