sfPropelActAsSortableBehaviorPlugin group patch

February 20th, 2009 by Carlos Barros

I guess that most Symfony developers should know this Symfony plugin, but for those who not, here’s an small quote:

“The sfPropelActAsSortableBehaviorPlugin is a symfony plugin that provides a new Propel behavior.Model classes with this behavior enabled become sortable, which means that they have new methods to deal with a position attribute.”

This Symfony plugin is really useful as it can save you a lot of code if you need to sort your objects, but in a recent project I worked on, I noticed this plugin lacks one functionality: it can’t handle “sorting groups” within the same object. For instance, suppose you have an Product and Category tables, and that each product belongs to a single category. Then, suppose you want to be able to sort your products so you can control how they show up in your pages. Using sfPropelActAsSortableBehaviorPlugin you can only sort the entire Product table, and that’s not really what we want. We need to be able to sort products within each category.
I guess this problem is really common, and as I had to handle this in a recent project, I decided to write a patch for this plugin, and I’d like to share it. Before one ask me why I didn’t submit it as a official ticket, the reason is that there is already a patch submitted to handle this issue, from almost a year ago, that was not released yet, and I bet most people (including me) is not aware of, so I’m writing this post to tell about this for other users, and maybe make Kris Wallsmith (current leader) release his version, that in fact seem to be more powerful than mine, as it support creating sorting groups using more than one column.

So, how does it work?? sfPropelActAsSortableBehaviorPlugin is really simple to use, all you need is a Integer column in your model, and then activate the new behavior for you class using something like this:

sfPropelBehavior::add('Item', array('act_as_sortable' => array('column' => 'rank')));

After applying this patch, the only thing you need to do is to add one more element to the above array:

sfPropelBehavior::add('Item', array('act_as_sortable' => array('column' => 'rank','group' => 'category_id')));

And that’s it. New sfPropelActAsSortableBehaviorPlugin will use “category_id” as a “sorting group”.

You can now use all methods provided by the new behavior to sort your objects, just like you used to do without the patch. Also, if you don’t have a group category, just omit it from behavior initialization, and it will work as if u didn’t apply this patch.

Find below the full patch:

Read the rest of this entry »

Using embedFormForEach in Symfony, Part II

January 1st, 2009 by Carlos Barros

In my last post I talked about embedFormForEach method, that will embed a form inside another N times, and in the end of that article, I promised a new post on this subject to show how to use it to dynamically add/remove/sort cars, and here it is. As usual, I put live an example here (I integrated this new form on the old city picker form). In this example, when you create (or edit) an user, you will see a new control called Cars, and a green plus icon. If you click that icon, it will add a new “car form” into the user form. You can add as many cars as you want. Each new form has a set of controls that can be used to sort and remove cars from the form. Let’s take a look on how it all works.

Read the rest of this entry »

Using embedFormForEach in Symfony

December 1st, 2008 by Carlos Barros

The new Form sub-framework, introduced in Symfony 1.1, is really great, and the more I work with it, the more I like it. One of the nicest feature of form sub-framework is the ability to embed a form inside another one, using the embedForm method. Also, symfony provides another method, called embedFormForEach that will embed a form inside another one N times. At first glance this method doesn’t seem to be very useful, but I found it really useful in a project I worked a few weeks ago. Consider the following scenario: you want a form to ask users to input information about all cars they have (make and model). In this form you have a select box (this can be an input box too, but I’m using a select box in this example) where users specify how many cars they have and then it will show X fields where users can input cars model and make (X = number of cars user selected), using ajax to load fields. You can see an example here.

Read the rest of this entry »

SOAP WebService in Symfony

November 16th, 2008 by Carlos Barros

One interesting topic on web development is webservice development. There are several techniques to implement a webservice out there, and today I’ll talk about one technique that I worked in the recent past that I really like: SOAP. As per wikipedia:

SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) as its message format and usually relies on other Application Layer protocols, most notably Remote Procedure Call (RPC) and HTTP for message negotiation and transmission. SOAP forms the foundation layer of the web services protocol stack providing a basic messaging framework upon which abstract layers can be built.

The plan for this tutorial is to build a complete set of webservice methods to interact with the citypicker, built in a previous post. For this, I’ll use a great symfony plugin called ckWebService. This plugin enables the developer to expose your actions as a SOAP webservices. Another great functionality is the built-in WSDL generator, that parses module’s doc comment in order to identify which actions should be exposed and it’s input/output parameters.

Read the rest of this entry »

Conditional validator in Symfony, another approach

November 5th, 2008 by Carlos Barros

Today morning I was reading the Symfony blog, more specifically A week of symfony #96 and then I came across a tutorial talking about conditional validators.This is very usefull, I find myself using this technique very often, so I decided to write about another techinique I use sometimes to implement a conditional validation, but for a different scenario. Imagine the following: you have a contact form where the users specify how they want to be contacted, email or phone, throught a select box. Then you have two input boxes where the users can enter both email and phone. If the user opts to be contacted via email, email field becomes mandatory and phone number optional, and the opposite happens if the user opts to be contacted via phone. So, how to implement such a form in symfony?

Read the rest of this entry »

Joomla like theme for symfony admin generator

October 29th, 2008 by Carlos Barros

When I first started working with symfony admin generator, I searched for some nice themes that I could use in my project, and for my surprise, I didn’t find anything. Searching a bit more I came across a blog post on SymfonyLab (http://www.symfonylab.com/backend-generator-theme/) asking about themes. In the post the author pointed an joomla like theme (http://www.symfony-project.org/forum/index.php/m/16515), created by a user called Draven, that I really liked, but it’s outdated. I’m not really a joomla fan, but I must admin I like their backend design, so I decided to do a new implementation of this theme. It’s based on Draven’s implementation, in fact the css is almost the same, with some changes here there, and some small layout changes as well (specially in the edit page). Just one note that’s worth saying, it’s not really a THEME, but just a css, some layout templates and a collection of images, that changes the look of generated admin module. Here are some screenshots:

Also you can see it in action here (use anything as username/password).

Read the rest of this entry »

Country/State/City picker in Symfony 1.1

October 26th, 2008 by Carlos Barros

Have you ever seen those forms where you select a country, then it populates a list of states, then you pick a state and it populate a list of cities? I’ve seem many of these and I really like them. In one of my past projects I was asked to add a similar form to the system, using symfony, and I’ll show how I accomplished this, making use of the excellent new symfony 1.1 form system. For those who want to see what it looks like before keep on reading, I put very simple sample online in this link.

Read the rest of this entry »