Development

Changeset 8435

You must first sign up to be able to contribute.

Changeset 8435

Show
Ignore:
Timestamp:
04/13/08 13:33:31 (5 months ago)
Author:
naholyr
Message:

[sfAdvancedAdminGenerator] Added support for self-referencing many-to-many
relations.

Syntax is :

field_name:

type: self_reference_admin_double_list
params: through_class=RelationClass?.Column

Helpers are "self_reference_admin_*_list" (double, check, or select).
The syntax for "through_class" is :
- RelationClass? is the many-to-many relation class
- Column is the column referencing to the "source" object (the other foreign
key is therefore the "destination")

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfAdvancedAdminGeneratorPlugin/branches/naholyr/data/generator/sfAdvancedAdmin/default/template/actions/actions.class.php

    r8312 r8435  
    246246 
    247247$class = $this->getClassName(); 
    248 $related_class = sfPropelManyToMany::getRelatedClass($class, $through_class); 
    249 $related_table = constant($related_class.'Peer::TABLE_NAME'); 
    250 $middle_table = constant($through_class.'Peer::TABLE_NAME'); 
    251 $this_table = constant($class.'Peer::TABLE_NAME'); 
    252  
    253 $related_column = sfPropelManyToMany::getRelatedColumn($class, $through_class); 
    254 $column = sfPropelManyToMany::getColumn($class, $through_class); 
     248$related_class = sfAdvancedManyToMany::getRelatedClass($class, $through_class); 
     249$related_table = $this->getTableName($related_class); 
     250$middle_table = $this->getTableName($through_class); 
     251$this_table = $this->getTableName($class); 
     252 
     253$related_column = sfAdvancedManyToMany::getRelatedColumn($class, $through_class); 
     254$column = sfAdvancedManyToMany::getColumn($class, $through_class); 
     255 
     256if (false !== strpos($through_class, '.')) { 
     257  list($through_class, $source_column) = explode('.', $through_class, 2); 
     258
    255259 
    256260?> 
    257 <?php if ($input_type == 'admin_double_list' || $input_type == 'admin_check_list' || $input_type == 'admin_select_list'): ?> 
     261<?php if (preg_match('/^(self_reference_)?admin_(double|check|select)_list$/', $input_type)): ?> 
    258262<?php if ($credentials): $credentials = str_replace("\n", ' ', var_export($credentials, true)) ?> 
    259263        if ($this->getUser()->hasCredential(<?php echo $credentials ?>)) 
  • plugins/sfAdvancedAdminGeneratorPlugin/branches/naholyr/lib/helper/AdvancedAdminHelper.php

    r8312 r8435  
    3333  echo input_hidden_tag($input_name); 
    3434} 
    35 ?> 
     35 
     36function object_self_reference_admin_double_list($object, $method, $options = array()) 
     37
     38  $callback = '_self_reference_many_to_many_object_list'; 
     39   
     40  return object_admin_double_list($object, $method, $options, $callback); 
     41
     42 
     43function object_self_reference_admin_select_list($object, $method, $options = array()) 
     44
     45  $callback = '_self_reference_many_to_many_object_list'; 
     46   
     47  return object_admin_select_list($object, $method, $options, $callback); 
     48
     49 
     50function object_self_reference_admin_check_list($object, $method, $options = array()) 
     51
     52  $callback = '_self_reference_many_to_many_object_list'; 
     53   
     54  return object_admin_check_list($object, $method, $options, $callback); 
     55
     56 
     57function _self_reference_many_to_many_object_list($object, $method, $options) 
     58
     59  $full_through_class = _get_option($options, 'through_class'); 
     60  list($through_class, $source_column) = explode('.', $full_through_class, 2); 
     61   
     62  $class = get_class($object); 
     63  $relation_method = 'get' . $through_class . 'sRelatedBy' . $class . 'Id'; 
     64  $related_column = sfAdvancedManyToMany::getRelatedColumn($class, $full_through_class); 
     65  $related_column_name = $related_column->getColumnName();  
     66  $related_method = 'get' . $class . 'RelatedBy' . $related_column->getPhpName(); 
     67   
     68  $criteria = new Criteria; 
     69  $criteria->add(constant($class . 'Peer::ID'), $object->getId(), Criteria::NOT_EQUAL); 
     70  $criteria->addJoin(constant($class . 'Peer::ID'), constant($through_class . 'Peer::' . $related_column_name)); 
     71   
     72  $objects = call_user_func(array($class . 'Peer', 'doSelect'), $criteria); 
     73  $objects_associated = array_map(create_function('$o', 'return $o->' . $related_method . '();'), call_user_func(array($object, $relation_method))); 
     74  $ids = array_map(create_function('$o', 'return $o->getPrimaryKey();'), $objects_associated); 
     75 
     76  return array($objects, $objects_associated, $ids); 
     77
  • plugins/sfAdvancedAdminGeneratorPlugin/branches/naholyr/lib/sfAdvancedAdminGenerator.class.php

    r8312 r8435  
    2222class sfAdvancedAdminGenerator extends sfPropelAdminGenerator 
    2323{ 
     24   
     25  /** 
     26   * Returns table name from "extended" class name (supports the Class.Column syntax 
     27   * used for self-referencing many-to-many relations). 
     28   * 
     29   * @param string $class_name 
     30   * @return string 
     31   */ 
     32  public function getTableName($class_name) 
     33  { 
     34    if (false !== strpos($class_name, '.')) { 
     35      list($class_name, $ref_column) = explode('.', $class_name, 2); 
     36    } 
     37     
     38    return constant($class_name . 'Peer::TABLE_NAME'); 
     39  } 
     40   
    2441  /** 
    2542   * Initializes the current sfGenerator instance.