Symfony Model Php5

ADVERTISEMENT

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:
Creole Column Types
DATABASES SUPPORTED (Creole)
MODEL CLASSES
MySQL
SQLServer
BOOLEAN = 1
VARBINARY = 13
The schema is used to build the model classes of the ORM layer through the
PostgreSQL
BIGINT = 2
NUMERIC = 14
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
DATA MODEL CLASSES
lib/model/
propel.database.createUrl
= mysql://localhost/
propel.database.url
= mysql://localhost/myproject
Article.php
Comment.php
Special Date Columns
...
ArticlePeer.php
CommentPeer.php
Inherit from the Base ones. When the propel-build-model task is called on
databases.yml
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
add custom methods and properties to the model objects.
prod:
record was created
propel:
Example: here is the content of the newly created Article.php file:
updated_at
param:
require_once 'model/om/BaseArticle.php';
updated each time the record itself is
host:
mydataserver
class Article extends BaseArticle{
updated
username:
myusername
}
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:
The accessors and mutators use a
propel:
Article.php
camelCase variant of the column names,
class:
sfPropelDatabase
OBJECT CLASSES
Comment.php
so the getTitle() method retrieves the
param:
Represent a record in the database. They give access to the columns of a
value of the title column.
phptype:
mysql
hostspec:
localhost
record and to related records.
Accessors:
database:
blog
get[MyColumnName]()
Object Class Constructor - new
username:
login
Retrieve the column value
To create a new object:
password:
passwd
$myobject->getMyColumn();
$myobject = new MyTable();
port:
80
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.
fromArray($array)
This configuration can also be overridden
The methods of the Peer classes will be called with a :: (for static method
To set several fields at one time:
per application, in
call) instead of the usual -> (for instance method call)
apps/myapp/config/databases.yml
$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()
select multiple objects based on their primary keys:
try{
save the data into the database
$myobject=myTablePeer::retrieveByPKs($arrayOfPrimaryKeys);
$con->begin();
$myobject->save();
$ret = parent::save($con);
Retrieving Records with Criteria
isNew()
// update interested_users in question table
check if an object is new
$question = $this->getQuestion();
$c = new Criteria();
$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
deserves saving
$con->commit();
//$comments is an array of objects of class Comment
$myobject->isModified();
return $ret;
}
delete()
METADATA CLASSES
lib/model/map/
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:
id
<table name="blog_article" phpName="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" />
title:
varchar(255)
<column name="created_at" type="timestamp" />
created_at (0)
content:
longvarchar
</table>
created_at:
<table name="blog_comment" phpName="Comment">
<column name="id" type="integer" required="true" primaryKey="true"autoIncrement="true" />
blog_comment:
<column name="article_id" type="integer" />
_attributes: { phpName: Comment }
<foreign-key foreignTable="blog_article">
id:
id
<reference local="article_id" foreign="id"/>
article_id:
</foreign-key>
article_id (FK)
author:
varchar(255)
<column name="author" type="varchar" size="255" />
content:
longvarchar
author (0)
<column name="content" type="longvarchar" />
created_at:
content (0)
<column name="created_at" type="timestamp" />
created_at (0)
</table>
</database>
This cheat-sheet is not an official part of the symfony documentation

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go