Code snippets for symfony 1.x


Refine Tags

Snippets tagged "object model"

Retrieve model objects with custom SQL query

$sql = 'select * from ( select * from book order by weight desc limit 5 ) as T order by popularity';
$connection = Propel::getConnection();
$statement = $connection->createStatement(  );
$result = $statement->executeQuery( $sql , ResultSet::FETCHMODE_NUM);
return BookPeer::populateObjects( $result );
by Yuriy Smirnov on 2007-08-07, tagged custom  model  object  propel  sql 

Populate Object Filter

If you're like me, you hate having to populate your objects (from the model) with parameters from the request. Wouldn't it be nice to specify a little nugget of configuration, and then never worry about it again? Consider the following, which is tedious and boring:

Old Example

class mymoduleActions extends sfActions
  public function executeUpdate()
    $employee = new Employee();
    $this->employee = $employee;

And the list goes on. Imagine, however, that you have the following in your module filters.yml file:

  class: myPopulateObjectFilter
    use_database:  on
    model_object:  employee
    model_class:   Employee
    exclude:       [ssn]
      first_name:  example default name

If you had a filter that automatically created an object, and based upon the previous yaml, populated that object with data from the request (after a user hit submit on a form containing this information), then subsequently set that object for retrieval as a request attribute, you'd be a pretty happy person right?

New Example

class mymoduleActions extends sfActions
  public function executeUpdate()
    $this->employee = $this->getRequest()->getAttribute('employee');

Of course, you'd be free to do whatever you want with the results of the prepopulated object.

A great application would be an alternative to the fillin form filter. If you have your template set up to populate inputs from an object from the model, this filter can populate that object for you, and you can simply pass it on to the template.

The Code

 * myPopulateObjectFilter
 * Uses configuration in filters.yml to create an object,
 * populate it with data from the request,
 * and set a request attribute with the result.
 * @author  Stephen Riesenberg
class myPopulateObjectFilter extends sfFilter
        $defaults       = null,
        $controller     = null,
        $request        = null;
    public function initialize($context, $parameters = array())
        parent::initialize($context, $parameters);
        // get defaults from filters.yml (if any) and create new parameter holder to store them
        $this->defaults = new sfParameterHolder();
        $this->defaults->add($this->getParameter('defaults', array()));
        // get controller and request
        $this->controller   = $this->getContext()->getController();
        $this->request      = $this->getContext()->getRequest();
    public function execute($filterChain)
        // get request variable list
        $vars = $this->request->getParameterHolder()->getNames();
        $exclude = array_merge($this->getParameter('exclude', array()), array('module', 'action'));
        $vars = array_diff($vars, $exclude);
        $funcs = array();
        foreach ($vars as $var)
            $funcs[$var] = 'set'.ucfirst(sfInflector::camelize($var));
        // get model_object and model_class
        $object = $this->getParameter('model_object');
        $class = $this->getParameter('model_class');
        // fetch from the database or create the object to fill in
        if ($this->getParameter('use_database', false) && null !== ($id = $this->request->getParameter('id')))
            $peer_class = $class . "Peer";
            $record = $peer_class::retrieveByPk($id);
            $record = new $class();
        // something like: array('id' => 'setId', 'my_field' => 'setMyField', ...)
        foreach ($funcs as $var => $func)
            if (is_callable(array($record, $func)))
        // set the updated record into a request attribute
        $this->request->setAttribute($object, $record);
        // execute next filter
    protected function getDefault($var)
        return $this->defaults->get($var);
    protected function getValue($var)
        return $this->request->getParameter($var) != '' ? $this->request->getParameter($var) : $this->getDefault($var);

NOTE: Place this in a file called myPopulateObjectFilter.class.php in the myproject/apps/myapp/lib/ directory.

by Stephen Riesenberg on 2007-01-06, tagged filter  model  object