sfPropelHelperPlugin - 0.8.0

This plugin makes it easier to do (non-default-)joins and hydrations.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

INTRODUCTION

This plugin makes it easier to do (non-default-)joins. Instead of the limited doSelectJoin(xxx) Peer-Methods you are now free to do any join (between any related tables and with any depth) and let the result get hydrated automatically.

So called ObjectPaths have been introduced to define objects, with their relations. ( Similar PropertyPaths have been introduced to define properties of (related)objects)

This helper extends the functionality of Propel, while keeping it completely backwards compatible.

INSTALLATION

NOTE: be sure to have connection pooling enabled for your propel database (in your database.yml) or else you will get more results than desired with right-joins (one-to-many relations)

In order to make use of this helper you should enable the peer and object builders provided by this plugin in the builder map. you should change your config/propel.ini and set

propel.builder.peer.class = plugins.sfPropelHelperPlugin.lib.builder.SfPeerBuilderDS propel.builder.object.class = plugins.sfPropelHelperPlugin.lib.builder.SfObjectBuilderDS

run symfony propel:build-model and you have extended base peer and object classes.

There also is an important bug fix for Propel provided with this plugin, that makes it possible to define select columns before you have provide table-aliasses.

To enable this patch copy two files to your propel folder: cp plugins/sfPropelHelperPlugin/patch/result\ after\ patch/* lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel/util/

(this was created at the time of Symfony 1.2.4)

I would like to get these improvements included into Propel, but it is somewhat hard to get into contact with the Propel community, since everything you send gets marked as spam...

So far the difficult part.

EXAMPLE

schema.yml:

propel:
  photo:
    id:
    album_id:
    name:     varchar(50)

  album:
    id:
    album_id:
    name:     varchar(50)

Lets see that in action.class.php:

$this->getContext()->getConfiguration()->loadHelpers('sfPropelPropertyPath');

$criteria = new Criteria();
$objectPaths = array('Photo', 'Photo.Album'); // this can be reduced to array('Photo.Album');

$criteria = addJoinsAndSelectColumns($criteria, $objectPaths);
$photos = hydrate($criteria, $objectPaths, $connection = null);

foreach ($photos as $photo)
{
  echo $photo->getAlbum()->getName()." -> ".$photo->getName()."<br>\n";
}

Obviously the big advantage of this plugin shows when you have complex joins to be performed! The propel-model builder is extended to provide you with all the default joins. These get defined in the basePeer-classes like this (from the BasePhotoPeer):

static public function getRelations()
{
  return array (
    'Album' => array (
      'relatedClass' => 'Album',
      'oneToMany' => false,
      'associateMethod' => 'addPhoto',
      'leftKeys' => array (
        0 => PhotoPeer::ALBUM_ID,
      ),
      'rightKeys' => array (
        0 => AlbumPeer::ID,
      ),
      'joinType' => 'LEFT JOIN',
    ),
  );
}

You can extend this method in the Peer class and define custom relations from one class to the other.

LICENSE

see LICENSE file