Tag Archives: ORM
Doctrine: Magic Finders
Doctrine offers some magic finders for your Doctrine models that allow you to find a record by any column that is present in the model. This is helpful for simply finding a user by their username, or finding a group by the name of it. Normally this would require writing a Doctrine_Query instance and storing this somewhere so it can be reused. That is no longer needed for simple situations like that.
The basic pattern for the finder methods are as follows: findBy%s($value) or findOneBy%s($value). The %s can be a column name or a relation alias. If you give a column name you must give the value you are looking for. If you specify a relationship alias, you can either pass an instance of the relation class to find, or give the actual primary key value.
First lets retrieve the UserTable instance to work with:
// test.php
// …
$userTable = Doctrine_Core::getTable(‘User’);
Now we can easily find a User record by its primary key by using the find() method:
// test.php
// …
$user = $userTable->find(1);
Now if you want to find a single user by their username you can use the following magic finder:
// test.php
// …
$user = $userTable->findOneByUsername(‘jonwage’);
You can also easily find records by using the relationships between records. Because User has many Phonenumbers we can find those Phonenumbers by passing the findBy**() method a User instance: Continue reading
Doctrine ORM : Transitive Persistence
Transitive Persistence
Doctrine offers both database and application level cascading operations. This section will explain in detail how to setup both application and database level cascades.
Application-Level Cascades
Since it can be quite cumbersome to save and delete individual objects, especially if you deal with an object graph, Doctrine provides application-level cascading of operations.
Save Cascades
You may already have noticed that save() operations are already cascaded to associated objects by default.
Delete Cascades
Doctrine provides a second application-level cascade style: delete. Unlike the save() cascade, the delete cascade needs to be turned on explicitly as can be seen in the following code snippet:
// models/User.php
class User extends BaseUser
{
// …
public function setUp()
{
parent::setup();
// …
$this->hasMany(‘Address as Addresses’, array(
‘local’ => ‘id’,
‘foreign’ => ‘user_id’,
‘cascade’ => array(‘delete’)
)
);
}
}
Here is the same example in YAML format. You can read more about YAML in the YAML Schema Files chapter:
—
# schema.yml
# …
User:
# …
relations:
# …
Addresses:
class: Address
local: id
foreign: user_id
cascade: [delete]
The cascade option is used to specify the operations that are cascaded to the related objects on the application-level.
lease note that the only currently supported value is delete, more options will be added in future releases of Doctrine.
In the example above, Doctrine would cascade the deletion of a User to it’s associated Addresses. The following describes the generic procedure when you delete a record through $record->delete():
1. Doctrine looks at the relations to see if there are any deletion cascades it needs to apply. If there are no deletion cascades, go to 3).
2. For each relation that has a delete cascade specified, Doctrine verifies that the objects that are the target of the cascade are loaded. That usually means that Doctrine fetches the related objects from the database if they’re not yet loaded.(Exception: many-valued associations are always re-fetched from the database, to make sure all objects are loaded). For each associated object, proceed with step 1).
3. Doctrine orders all deletions and executes them in the most efficient way, maintaining referential integrity.
From this description one thing should be instantly clear: Application-level cascades happen on the object-level, meaning operations are cascaded from one object to another and in order to do that the participating objects need to be available.
This has some important implications:
Application-level delete cascades don’t perform well on many-valued associations when there are a lot of objects in the related collection (that is because they need to be fetched from the database, the actual deletion is pretty efficient).
Application-level delete cascades do not skip the object lifecycle as database-level cascades do (see next chapter). Therefore all registered event listeners and other callback methods are properly executed in an application-level cascade.
Database-Level Cascades
Some cascading operations can be done much more efficiently at the database level. The best example is the delete cascade.
Database-level delete cascades are generally preferrable over application-level delete cascades except:
Your database does not support database-level cascades (i.e. when using MySql with MYISAM tables).
You have listeners that listen on the object lifecycle and you want them to get invoked.
Database-level delete cascades are applied on the foreign key constraint. Therefore they’re specified on that side of the relation that owns the foreign key. Picking up the example from above, the definition of a database-level cascade would look as follows:
// models/Address.php
class Address extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn(‘user_id’, ‘integer’);
$this->hasColumn(‘address’, ‘string’, 255);
$this->hasColumn(‘country’, ‘string’, 255);
$this->hasColumn(‘city’, ‘string’, 255);
$this->hasColumn(‘state’, ‘string’, 2);
$this->hasColumn(‘postal_code’, ‘string’, 25);
}
public function setUp()
{
$this->hasOne(‘User’, array(
‘local’ => ‘user_id’,
‘foreign’ => ‘id’,
‘onDelete’ => ‘CASCADE’
)
);
}
}
Here is the same example in YAML format. You can read more about YAML in the YAML Schema Files chapter:
—
# schema.yml
# …
Address:
columns:
user_id: integer
address: string(255)
country: string(255)
city: string(255)
state: string(2)
postal_code: string(25)
relations:
User:
local: user_id
foreign: id
onDelete: CASCADE
The onDelete option is translated to proper DDL/DML statements when Doctrine creates your tables.
Note that ‘onDelete’ => ‘CASCADE’ is specified on the Address class, since the Address owns the foreign key (user_id) and database-level cascades are applied on the foreign key.
Currently, the only two supported database-level cascade styles are for onDelete and onUpdate. Both are specified on the side that owns the foreign key and applied to your database schema when Doctrine creates your tables. Continue reading
Doctrine YAML Schema Files: Abbreviated Syntax
Doctrine offers the ability to specify schema in an abbreviated syntax. A lot of the schema parameters have values they default to, this allows us to abbreviate the syntax and let Doctrine just use its defaults. Below is an example of schema taking advantage of all the abbreviations.
The detect_relations option will attempt to guess relationships based on column names. In the example below Doctrine knows that User has one Contact and will automatically define the relationship between the models.
---
detect_relations: true
User: Continue reading
Using Namespaces in Propel 1.5
Propel 1.5 does not cease to please the pace of development and introduction of new features, just a few weeks ago, Francois Zaninotto(project leader Propel) has published a new opportunity to edit the nested forms using mergeRelation and embedRelation and the other day to use Namespaces in the generation of models.
Propel 1.5 allows the use of Namespaces in files describing your model if you are using in the php version 5.3
Add the use of models is very simple:
Xml version = "1.0" encoding = "UTF-8" standalone = "no"?>


