![]() |
|
Snippets |
|
Propel can create Models with autoset created_at and Updated_at field, but dont include autoset for Created_by and Updated_by field, because this fields are most aplication dependent. We can set Propel to build models with autosetting Created_by and Updated_by fields (and any other fields) extending SfObjectBuilder. In the forum message 7535 Tamcy give us a good example. Here I post a SFObjectBuilder extension class (in this example I check if using SfGuardPlugin for user Class).
<?php /* I recomend put this class out Symfony Folder. * If you put this class in some project folder * you can use include_once 'symfony/addon/propel/builder/SfObjectBuilder.php' * @author Boris Duin */ require_once 'sfObjectBuilder.php'; class SfObjectAdvBuilder extends SfObjectBuilder { */Extend Method addSave protected function addSave(&$script) { $tmp = ''; parent::addSave($tmp); $date_script = ''; $user_updated = false; $user_created = false; foreach ($this->getTable()->getColumns() as $col) { $clo = strtolower($col->getName()); if (!$user_created && $clo == 'created_by') { $user_created = true; $date_script .= " if (\$this->isNew() && !\$this->isColumnModified('created_by')) { \$user = sfContext::getInstance()->getUser(); if (\$user instanceof sfGuardSecurityUser) \$this->setCreatedby(\$user->getUsername()) } "; } else if (!$user_updated && $clo == 'updated_by') { $user_updated = true; $date_script .= " if (\$this->isModified() && !\$this->isColumnModified('updated_by')) { \$user = sfContext::getInstance()->getUser(); if (\$user instanceof sfGuardSecurityUser) \$this->setUpdatedby(\$user->getUsername()) } "; } } $tmp = preg_replace('/{/', '{'.$date_script, $tmp, 1); $script .= $tmp; } }
Note: I write this class for synfony 0.9.x versión. Yesterday I was refactoring this class for 1.0.x version, but I leave this in my house, :(. To set propel to use this class you must edit config/propel.ini. Replace this line
propel.builder.object.class = symfony.addon.propel.builder.SfObjectBuilder
for
propel.builder.object.class = route.to.my.class.SfObjectAdvBuilder
Example: If you put this class in your Project/Lib folder you can set the line:
propel.builder.object.class = ${propel.output.dir}.lib.SfObjectAdvBuilder
Now, when you run symfony propel-build-model, propel will use this class and add autoset code for created_by and Updated_by field. Here an example of models class generated by propel (symfony 0.9.x):
public function save($con = null) { if ($this->isNew() && !$this->isColumnModified('created_by')) { $user = sfContext::getInstance()->getUser(); if ($user instanceof sfGuardSecurityUser) $this->setCreatedby($user->getUsername()); } if ($this->isNew() && !$this->isColumnModified('created_at')) { $this->setCreatedAt(time()); } if ($this->isModified() && !$this->isColumnModified('updated_by')) { $user = sfContext::getInstance()->getUser(); if ($user instanceof sfGuardSecurityUser) $this->setUpdatedby($user->getUsername()); } if ($this->isModified() && !$this->isColumnModified('updated_at')) { $this->setUpdatedAt(time()); }
I will investigate how to create a Symfony plugin using maybe Behavior like other Symfony-propel plugins
You can pass aditional parameters to input_auto_complete_tag helper. By default, autocompleter pass only object-referer value. For aditional parameter you need use 'with' in $completion_options array. in view layer:
< <?php echo object_input_tag($model, 'getFilter', array ('size' => 20, 'control_name' => 'filter',));?> ?php echo input_auto_complete_tag('field_to_search','','module/autocomplete', array('autocomplete'=>'off'),array('use_style'=>true,'with'=> " value+'&filter='+$('filter').value"))?>
you must put '" value' (with whitespace) because javascript fail (a little symfony bug). Aditional parameters must go after value.
in control layer:
public function executeAutocomplete(){ $search=$this->getRequestParameter('field_to_search'); $filter=$this->getRequestParameter('filter'); .... }
now you can use field_to_search and filter to construct any query in model layer ans show this in AutocompleteSuccess.php view or another view that you define. this was tested in synfony version > 0.9. Check your version, and review your javascripthelper helper to verify if your input_auto_complete_tag helper supports 'with' option