Symfony generators – automatic wildcards on filters with plugins

I was just working with a user list using an admin generator, and received a suggestion that username searches should automatically include wildcards in the searches (front and back), so it would not be necessary to add the asterisk. The problem was that the list was based on the sfGuardUser plugin, and we all know that modifying the contents of a plugin can be bad news.

The way to do this without modifying the plugin, is to override the action that is being called, and then call the parent method. In this case, the action that was processing the form submission was sfGuardUserActions->executeList(). So in apps/yourapp/modules/sfGuardUser/actions/actions.class.php add the following:

  public function executeList()
    //Add fields to auto-wildcard to this list
    $wildcardFields = array("username");
    $filters = $this->getRequestParameter("filters")
    $originalFilters = $filters;
    if ($filters)
      foreach ($wildcardFields as $fieldName)
        // Only add the wildcards if the user is not already using them
        if (strpos($filters[$fieldName], "*") === false)
          $filters[$fieldName]  = "*".$filters[$fieldName]."*";
      $this->getRequest()->getParameterHolder()->set("filters", $filters);
    // Set the filters back to their original values before the template is rendered
    $this->filters = $originalFilters;

Now the request parameters for the selected fields are automatically prepended and appended with an asterisk before calling the parent function of the same name to continue with the request. Once the parent has finished, we then set the filters back to their original value, otherwise the asterisks we added would now appear in the fields when the page is presented.

An enhancement to this would be to specify the auto-wildcards field in the .yml file, rather than specifying it here in the action.

How to write complex queries using Propel

This is a monster subject, and I spent a long time scratching my head over a few things yesterday, until I came across this site:

It’s developed by a Symfony user, and allows you to type in sql statements using nested braces for prioritising. The form will then return a nicely formatted block of Propel code.

It’s not perfect, and it doesn’t cover everything just yet, but it’s great for getting some ideas about the structure and how to use various criteria – it’s also spot on if you are trying to learn Propel and already know how to structure SQL statements.

Discuss this tool and/or contribute on the Symfony Forum

Using a combination of tools like this, and taking a look at the generated files in lib/model/om should give you a good push, and a bit of a shortcut to more complex queries than you may be used to – rather than having to trawl the Propel documentation.