![]() |
|
sfPropelActAsPolymorphicBehaviorPlugin - 0.0.3Enable multi-column foreign keys. |
|
The sfPropelActAsPolymorphicBehaviorPlugin is a symfony plugin that provides support for polymorphic keys in Propel objects.
This plugin can be used to store data that each reference a record from any number of tables. For example, you could create one comments table that uses a polymorphic key to reference both the table and the primary key of the foreign record. The example below demonstrates this.
schema.xml.Please post to this forum thread or ping me in the IRC channel (kriswallsmith) if you have any thoughts.
Install the plugin
./symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsPolymorphicBehaviorPlugin
If you haven't already, enable behaviors in propel.ini and rebuild your model.
propel.builder.addBehaviors = true
./symfony propel-build-model
Activate the behavior for one of your Propel models:
[php]
// lib/model/Post.php class Post extends BasePost { }
$hasOneKeys = array('author' => array('foreign_model' => PostPeer::AUTHOR_TYPE, 'foreign_id' => PostPeer::AUTHOR_ID)); $hasManyKeys = array('tags' => array('foreign_model' => TagPeer::SUBJECT_TYPE, 'foreign_id' => TagPeer::SUBJECT_ID), 'comments' => array('foreign_model' => CommentPeer::SUBJECT_TYPE, 'foreign_id' => CommentPeer::SUBJECT_ID)); sfPropelBehavior::add('Post', array('sfPropelActAsPolymorphic' => array('has_one' => $hasOneKeys, 'has_many' => $hasManyKeys))); sfPropelActAsPolymorphicBehavior::mixinCustomMethods('Post');
The call to sfPropelBehavior::add() adds the following methods to your class:
getPolymorphicHasOneReference(string $keyName[Connection $con](,))setPolymorphicHasOneReference(string $keyName, mixed $foreignObject[Connection $con](,))clearPolymorphicHasOneReference(string $keyName[Connection $con](,))getPolymorphicHasManyReferences(string $keyName[Criteria $c[, Connection $con]](,))addPolymorphicHasManyReference(string $keyName, BaseObject $foreignObject[Connection $con](,))clearPolymorphicHasManyReferences(string $keyName[Criteria $c[, bool $doDelete[, Connection $con]]](,))deletePolymorphicHasManyReferences(string $keyName[Criteria $c[, Connection $con]](,))countPolymorphicHasManyReferences(string $keyName[Criteria $c[, bool $distinct[, Connection $con]]](,))Furthermore, calling sfPropelActAsPolymorphicBehavior::mixinCustomMethods() adds a number of custom-named methods to your class, based on the names of your polymorphic keys.
For example, the has_one key author, above, would be accessible with the following methods:
getAuthor([$con](Connection))setAuthor(mixed $foreignObject[Connection $con](,))clearAuthor([$con](Connection))The has_many key tags, above, would be accessible with the following methods:
getTags([$c[, Connection $con]](Criteria))addTags(BaseObject $foreignObject[Connection $con](,)) (this could become addTag() later ... we'll see)clearTags([$c[, bool $doDelete[, Connection $con]]](Criteria))deleteTags([$c[, Connection $con]](Criteria))countTags([$c[, bool $distinct[, Connection $con]]](Criteria))has_many key or setting a has_one key, the foreign object must already be saved. This should be resolved in the next alpha release.Plugin now supports has_many relationships and mixes in custom methods based on the names of your keys.
Initial public release.
setPolymorphicHasOneReference.Kris Wallsmith