Changeset 18560
- Timestamp:
- 05/22/09 22:26:20 (4 years ago)
- Files:
-
- plugins/sfDoctrineActAsRattablePlugin/trunk/README (modified) (1 diff)
- plugins/sfDoctrineActAsRattablePlugin/trunk/config/doctrine/schema.yml (modified) (1 diff)
- plugins/sfDoctrineActAsRattablePlugin/trunk/lib/Rattable.class.php (modified) (4 diffs)
- plugins/sfDoctrineActAsRattablePlugin/trunk/lib/RattableTemplate.class.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfDoctrineActAsRattablePlugin/trunk/README
r18417 r18560 88 88 The plugin implements the following methods to the object: 89 89 90 * getRat ingsDetails()91 * get AvgRatings()90 * getRates() 91 * getRating() 92 92 * addRate(RategObject $rateObject) 93 93 * removeRatings() plugins/sfDoctrineActAsRattablePlugin/trunk/config/doctrine/schema.yml
r18389 r18560 1 rating:2 columns:3 rate: integer4 model_class: string(255)5 model_id: integer6 rated_by: integerplugins/sfDoctrineActAsRattablePlugin/trunk/lib/Rattable.class.php
r18417 r18560 7 7 class Doctrine_Rattable extends Doctrine_Record_Generator 8 8 { 9 protected $_options = array(9 protected $_options = array( 10 10 'className' => '%CLASS%Rate', 11 ' fields' => array(),11 'tableName' => false, 12 12 'generateFiles' => false, 13 13 'table' => false, … … 15 15 'children' => array(), 16 16 'options' => array(), 17 'criterias' => 'rate',17 'criterias' => array('rate'), 18 18 'max_rate' => 5, 19 'rounding_rate' => 1, 19 20 'with_comment' => true, 20 ); 21 'user' => array('class' => 'sfGuardUser', 22 'type' => 'integer', 23 'size' => 4), 24 ); 25 21 26 22 27 /** … … 26 31 * @return void 27 32 */ 28 public function __construct($options)29 {30 $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);31 }33 public function __construct($options) 34 { 35 $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options); 36 } 32 37 33 public function buildRelation()34 {35 $this->buildForeignRelation('Rate');36 $this->buildLocalRelation();37 }38 public function buildRelation() 39 { 40 $this->buildForeignRelation('Rate'); 41 $this->buildLocalRelation(); 42 } 38 43 39 44 /** … … 43 48 * @return void 44 49 */ 45 public function setTableDefinition() 50 public function setTableDefinition() 51 { 52 if(is_int($this->_options['rounding_rate'])) 46 53 { 47 $options = array('className' => $this->_options['className']); 54 $type = 'integer'; 55 $length = 4; 56 } 57 else 58 { 59 $type = 'float'; 60 $length = null; 61 } 48 62 49 if(is_int($this->_options['rounding_rate'])) 50 { 51 $type = 'integer'; 52 $length = 4; 53 } 54 else 55 { 56 $type = 'float'; 57 $length = null; 58 } 63 $options['range'] = array(1 => $this->_options['max_rate']); 59 64 60 $options['range'] = array(0 => $this->_options['max_rate']); 65 foreach ($this->_options['criterias'] as $fieldName) 66 { 67 $this->hasColumn($fieldName, $type, $length, $options); 68 } 69 unset($options['range']); 61 70 62 foreach ($this->_options['criterias'] as $fieldName)63 {64 $this->hasColumn($fieldName, $type, $length, $options);65 }71 if($this->_options['with_comment']) 72 { 73 $this->hasColumn('comment', 'string', 4000, $options); 74 } 66 75 76 if($this->_options['user']) 77 { 78 $user = $this->_options['user']; 79 $this->hasColumn(Doctrine_Inflector::tableize($user['class']) . '_id', $user['type'], $user['size'], array('primary' => true)); 80 } 81 else 82 { 83 $this->hasColumn(Doctrine_Inflector::tableize($this->_options['table']->getComponentName()) . '_id', 'integer', null, array('primary' => true)); 84 $this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true)); 85 } 86 } 67 87 68 $options = $this->_options['options']; 88 public function buildLocalRelation() 89 { 90 // relation to the main object 91 $options['foreign'] = $this->_options['table']->getIdentifier(); 92 $options['local'] = ($this->_options['user']) ? 'id' : Doctrine_Inflector::tableize($this->_options['table']->getComponentName()) . '_id'; 93 $options['type'] = Doctrine_Relation::ONE; 94 $options['onDelete'] = 'CASCADE'; 95 $options['onUpdate'] = 'CASCADE'; 96 $this->_table->getRelationParser()->bind($this->_options['table']->getComponentName(), $options); 69 97 70 if($this->_options['with_comment'])71 {72 $this->hasColumn('comment', 'string', 4000, $options);73 }98 // relation to the user 99 if($this->_options['user']) 100 { 101 $user = $this->_options['user']; 74 102 103 $table = Doctrine::getTable($user['class']); 104 $options['foreign'] = $table->getIdentifier(); 105 $options['local'] = Doctrine_Inflector::tableize($user['class']) . '_id'; 106 $options['type'] = Doctrine_Relation::ONE; 75 107 76 // // Rewrite any relations to our original table 77 // $originalName = $this->_options['table']->getClassnameToReturn(); 78 // $relations = $this->_options['table']->getRelationParser()->getPendingRelations(); 79 // foreach($relations as $table => $relation) { 80 // if ($table != $this->_table->getTableName() ) { 81 // // check that the localColumn is part of the moved col 82 // if (isset($relation['local']) && in_array($relation['local'], $this->_options['fields'])) { 83 // // found one, let's rewrite it 84 // $this->_options['table']->getRelationParser()->unsetPendingRelations($table); 85 // 86 // // and bind the rewritten one 87 // $this->_table->getRelationParser()->bind($table, $relation); 88 // 89 // // now try to get the reverse relation, to rewrite it 90 // $rp = Doctrine::getTable($table)->getRelationParser(); 91 // $others = $rp->getPendingRelation($originalName); 92 // if (isset($others)) { 93 // $others['class'] = $this->_table->getClassnameToReturn(); 94 // $others['alias'] = $this->_table->getClassnameToReturn(); 95 // $rp->unsetPendingRelations($originalName); 96 // $rp->bind($this->_table->getClassnameToReturn() ,$others); 97 // } 98 // } 99 // } 100 // } 108 $this->_table->getRelationParser()->bind($table->getComponentName() . ' as User', $options); 101 109 } 110 } 102 111 } plugins/sfDoctrineActAsRattablePlugin/trunk/lib/RattableTemplate.class.php
r18417 r18560 1 1 <?php 2 2 3 3 class Rattable extends Doctrine_Template 4 4 { … … 6 6 * __construct 7 7 * 8 * @param string $array 8 * @param string $array 9 9 * @return void 10 10 */ 11 public function __construct(array $options = array())12 {13 parent::__construct($options);14 $this->_plugin = new Doctrine_Rattable($this->_options);15 }11 public function __construct(array $options = array()) 12 { 13 parent::__construct($options); 14 $this->_plugin = new Doctrine_Rattable($this->_options); 15 } 16 16 17 17 /** … … 20 20 * @return void 21 21 */ 22 public function setUp()23 {24 $this->_plugin->initialize($this->_table);25 }22 public function setUp() 23 { 24 $this->_plugin->initialize($this->_table); 25 } 26 26 27 27 /** … … 30 30 * @return void 31 31 */ 32 public function getRattable() 33 { 34 return $this->_plugin; 35 } 32 public function getRattable() 33 { 34 return $this->_plugin; 35 } 36 37 public function getRates() 38 { 39 return array(); 40 } 41 42 public function getRating() 43 { 44 return array(); 45 } 46 47 public function getRateCount() 48 { 49 return 0; 50 } 51 52 public function addRate() 53 { 54 return true; 55 } 56 57 public function removeRatings() 58 { 59 return false; 60 } 36 61 }