Code snippets for symfony 1.x

Navigation

myPropelTools

<?php
/**
 * Tools for Propel model classes.
 *
 * @author ksn135 <serg [at] kalachev [dot] ru>
 * @package symfony
 * @version 0.1
 * @static 
 */
class myPropelTools
{ 
  public static function isPropelModelClass($class_name)
  { 
    try {
      $r_cls = new ReflectionClass( $class_name );
      $r_cls_peer = new ReflectionClass( $class_name.'Peer' );
      $r_base_cls = new ReflectionClass( 'Base'.$class_name );
      $r_base_cls_peer = new ReflectionClass( 'Base'.$class_name.'Peer' );
    }
    catch( Exception $e )
    {
      return false;
    }
    return true;
  } 
 
  public static function invokePeerMethod( $class_name, $method ) {
    if( ! self::isPropelModelClass($class_name) ) throw new Exception( "Class $class_name is not valid Propel Model class" );
    $args = @array_slice(func_get_args(), 2);
    return call_user_func_array( array($class_name.'Peer', $method), $args );
  }
 
  public static function getFieldName( $class_name, $field_name, $output_type = BasePeer::TYPE_PHPNAME )
  {
    return self::invokePeerMethod( $class_name, 'translateFieldName',
      $field_name, BasePeer::TYPE_FIELDNAME, $output_type );
  }
 
  public static function getColumnName( $class_name, $field_name )
  {
    list( $table, $column ) = explode('.', self::getFieldName( $class_name, $field_name, BasePeer::TYPE_COLNAME), 2);
    return $column;
  }
 
  public static function setValue( &$object, $field_name, $value ) {
    return self::invokeMethodName( $object, 'set', $field_name, $value );
  }
 
  public static function getValue( &$object, $field_name ) {
    return self::invokeMethodName( $object, 'get', $field_name );
  }
 
// criteria
  public static function getCriteria( $class_name )
  {
    if( ! self::isPropelModelClass($class_name) ) throw new Exception( "Class $class_name is not valid Propel Model class" );
    return new Criteria(constant($class_name.'Peer::DATABASE_NAME'));
  }
 
  public static function criteriaAddSelectColumn( &$criteria, $class_name, $field_name )
  {
    if( ! self::isPropelModelClass($class_name) ) throw new Exception( "Class $class_name is not valid Propel Model class" );
    return $criteria->addSelectColumn( constant($class_name.'Peer::'. self::getColumnName( $class_name, $field_name ) ) );
  }
 
  public static function criteriaAdd( &$criteria, $class_name, $field_name, $value = null, $comparison = null)
  {
    if( ! self::isPropelModelClass($class_name) ) throw new Exception( "Class $class_name is not valid Propel Model class" );
    return $criteria->add( constant($class_name.'Peer::'. self::getColumnName( $class_name, $field_name ) ), $value, $comparison );
  }
 
  public static function getRowSet( $class_name, $select_fields = null, $conditon_fields = null, $method = 'doSelectRS' )
  {
    $c = self::getCriteria( $class_name );
    if( $select_fields )
    {
      $fields = (is_array($select_fields)) ? $select_fields : array($select_fields);
      foreach( $fields as $field_name ) self::criteriaAddSelectColumn( $c, $class_name, $field_name);
    }
    if( $conditon_fields )
    {
      if( is_array( $conditon_fields ) && count($conditon_fields) )
      {
         $conditons = (is_array($conditon_fields[0]))? $conditon_fields : array( $conditon_fields );
      }
      else
        throw new Exception( "Condition parametr must be a not empty array (1 or 2 dimensional)" );
      foreach( $conditons as $key => $conditon )
      {
        $field_name = null;
        $value = null;
        $comparison = null;
        switch( count($conditon) )
        {
          case 1:
            $field_name = $condition;
            break;
          case 2:
            list( $field_name, $value ) = $conditon;
            break;
          case 3:
            list( $field_name, $value, $comparison ) = $conditon;
            break;
          default:
            throw new Exception( "Too many elements in condition #$key specified" );
        }
        self::criteriaAdd( $c, $class_name, $field_name, $value, $comparison );
      }
    }
    return self::invokePeerMethod( $class_name, $method, $c);
  }
 
// private methods
  private static function getMethodName( $class_name, $mode, $field_name ) {
    return $mode . self::getFieldName( $class_name, $field_name );
  }
 
  private static function invokeMethodName( &$object, $mode, $field_name, $value = '' ) {
    $methodName = self::getMethodName(get_class($object), $mode, $field_name);
    return ($mode == 'set') ? $object->$methodName($value) : $object->$methodName();
  }
 
}

save it into /<proj>/lib/myPropelTools.class.php and clear your cache

by Serg Kalachev on 2007-03-02, tagged propel 

Comments on this snippet

gravatar icon
#1 Loïc Vernet on 2007-03-14 at 05:28

I Serg, i have also done a class with additional method for Propel, perhaps it could be great to do a plugin of it.

PS: You should comment all your methods, a little exemple for each would be also very nice... :)

gravatar icon
#2 Serg Kalachev on 2007-03-15 at 05:22

[b]Loïc Vernet[/b] If you have your own class share with community -- post it here. Actually I have no time for this right now. May be later I comment it, make examples and release as plugin.