Changeset 25085
- Timestamp:
- 12/08/09 16:51:37 (3 years ago)
- Files:
-
- plugins/pplActAsSerializablePlugin/trunk/lib/pplSerializable.class.php (moved) (moved from plugins/pplActAsSerializablePlugin/trunk/lib/Serializable.class.php) (4 diffs)
- plugins/pplActAsSerializablePlugin/trunk/lib/pplSerializableListener.class.php (moved) (moved from plugins/pplActAsSerializablePlugin/trunk/lib/SerializableListener.class.php) (1 diff)
- plugins/pplActAsSerializablePlugin/trunk/lib/pplSerializableTemplate.class.php (moved) (moved from plugins/pplActAsSerializablePlugin/trunk/lib/SerializableTemplate.class.php) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/pplActAsSerializablePlugin/trunk/lib/pplSerializable.class.php
r25069 r25085 5 5 * 6 6 */ 7 class Doctrine_ Serializable extends Doctrine_Record_Generator7 class Doctrine_pplSerializable extends Doctrine_Record_Generator 8 8 { 9 9 protected $_options = array( 10 'className' => 'Serial', 11 'tableName' => false, 12 'generateFiles' => false, 13 'table' => false, 14 'pluginTable' => false, 15 'children' => array(), 16 'options' => array(), 17 'parameter' => 'year', 18 'format' => '0000', 10 'className' => 'pplSerial', 11 'tableName' => false, 12 'generateFiles' => false, 13 'table' => false, 14 'pluginTable' => false, 15 'children' => array(), 16 'options' => array(), 17 'format' => '0000', 19 18 ); 20 19 … … 33 32 public function buildRelation() 34 33 { 35 $this->buildForeignRelation(' Serial');36 $this->buildLocalRelation();34 $this->buildForeignRelation('pplSerial'); 35 //$this->buildLocalRelation(); 37 36 } 38 37 … … 45 44 public function setTableDefinition() 46 45 { 47 $this->_options['parameter'] = ($this->_options['parameter'] != 'year') ? 'custom' : $this->_options['parameter'];48 49 if($this->_options['format'])50 {51 52 }53 54 46 $this->hasColumn('id', 'integer', 4, array( 55 47 'unsigned' => true, … … 58 50 )); 59 51 60 $this->hasColumn('related_model', 'string(100)'); 61 $this->hasColumn('parameter', 'string(100)'); 52 $this->hasColumn('model_class', 'string', 100); 53 $this->hasColumn('ppl_year', 'string', 2); 54 $this->hasColumn('serial', 'integer', 4, array('unsigned' => true)); 62 55 63 $this->hasColumn('serial', 'integer', 4, array('unsigned' => true)); 56 $this->index('model_class_parameter', array( 57 'fields' => array('model_class', 'ppl_year'), 58 'type' => 'unique', 59 )); 64 60 } 65 61 66 public function buildLocalRelation()67 {68 // relation to the main object69 $options['foreign'] = $this->_options['table']->getIdentifier();70 $options['local'] = $this->getSerialObjectFk();71 $options['type'] = Doctrine_Relation::ONE;72 $options['onDelete'] = 'CASCADE';73 $options['onUpdate'] = 'CASCADE';74 $this->_table->getRelationParser()->bind($this->_options['table']->getComponentName(), $options);75 }76 77 public function getSerialObjectFk()78 {79 return Doctrine_Inflector::tableize($this->_options['table']->getComponentName()) . '_id';80 }81 62 } plugins/pplActAsSerializablePlugin/trunk/lib/pplSerializableListener.class.php
r25041 r25085 1 1 <?php 2 2 3 class SerializableListener extends Doctrine_Record_Listener3 class pplSerializableListener extends Doctrine_Record_Listener 4 4 { 5 /** 6 * Instance of Doctrine_Auditlog 7 * 8 * @var Doctrine_AuditLog 9 */ 10 protected $_serializable; 5 public function preInsert(Doctrine_Event $event) 6 { 7 $invoker = $event->getInvoker(); 8 $class_name = $invoker->getSerializable()->getOption('className'); 11 9 10 $c = new $class_name; 11 $c->merge(array( 12 'model_class' => get_class($invoker), 13 'ppl_year' => $invoker->getCurrentYear(), 14 'serial' => $invoker->getNext(), 15 )); 12 16 13 public function __construct(Doctrine_Serializable $serializable) 14 { 15 $this->_serializable = $serializable; 16 } 17 18 public function preSave(Doctrine_Event $event) 19 { 20 17 $c->save(); 21 18 } 22 19 } plugins/pplActAsSerializablePlugin/trunk/lib/pplSerializableTemplate.class.php
r25068 r25085 1 1 <?php 2 2 3 class Serializable extends Doctrine_Template3 class pplSerializable extends Doctrine_Template 4 4 { 5 5 /** … … 9 9 * @return void 10 10 */ 11 public function __construct(array $options = array())12 {13 parent::__construct($options);14 $this->_plugin = new Doctrine_Serializable($this->_options);15 }11 public function __construct(array $options = array()) 12 { 13 parent::__construct($options); 14 $this->_plugin = new Doctrine_pplSerializable($this->_options); 15 } 16 16 17 /**18 * Initialize the Rattable plugin for the template19 *20 * @return void21 */22 public function setUp()23 {24 $this->_plugin->initialize($this->_table);25 $this->addListener(new sfDoctrineRecordListener());26 }17 /** 18 * Initialize the Rattable plugin for the template 19 * 20 * @return void 21 */ 22 public function setUp() 23 { 24 $this->_plugin->initialize($this->_table); 25 $this->addListener(new pplSerializableListener()); 26 } 27 27 28 /**29 * Get the plugin instance for the Rattable template30 *31 * @return void32 */33 public function getSerializable()34 {35 return $this->_plugin;36 }28 /** 29 * Get the plugin instance for the Rattable template 30 * 31 * @return Doctrine_Serializable 32 */ 33 public function getSerializable() 34 { 35 return $this->_plugin; 36 } 37 37 38 /** 39 * 40 * @return int formated serial 41 */ 42 public function getSerial() 43 { 44 45 } 38 /** 39 * 40 * @return string formated serial 41 */ 42 public function getFormatedSerial() 43 { 44 $nb = new sfNumberFormat(); 45 return $nb->format($this->getNext(), $this->getFormat()); 46 } 46 47 47 protected function getNext() 48 { 49 50 } 48 /** 49 * @return int next serial 50 */ 51 public function getNext() 52 { 53 if($this->getInvoker()->exists()) 54 { 55 throw new Exception('You can only get Serial on new objects'); 56 } 51 57 58 return $this->getSerial()+1; 59 } 52 60 53 protected function resetNeeded() 54 { 55 56 } 61 public function getSerial() 62 { 63 $model_class = get_class($this->getInvoker()); 64 $year = $this->getCurrentYear(); 65 66 return Doctrine_Query::create() 67 ->select('ps.serial') 68 ->from('pplSerial ps') 69 ->where('ps.model_class = ? AND ps.ppl_year = ?', array($model_class, $year)) 70 ->limit(1) 71 ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR); 72 } 73 74 public function getCurrentYear() 75 { 76 return date('y', time()); 77 } 78 79 public function getYear() 80 { 81 $model_class = get_class($this->getInvoker()); 82 83 return Doctrine_Query::create() 84 ->select('MAX(ppl_year)') 85 ->from('pplSerial ps') 86 ->where('ps.model_class = ?', array($model_class)) 87 ->limit(1) 88 ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR); 89 } 90 91 /** 92 * @return boolean 93 */ 94 public function resetNeeded() 95 { 96 return $this->getYear() < $this->getCurrentYear(); 97 } 98 99 protected function getFormat() 100 { 101 return $this->getSerializable()->getOption('format'); 102 } 57 103 58 104 }