3 simple things I love about Propel

When updating almost all my own projects to Propel 1.6.4 over the last weeks, I decided to keep a list of the things that made me happy about Propel in general:

  • primaryString
    What?
    In your database schema, you can mark a column as the string that represents the data in a table. For instance: the name column in a product table.
    Why?
    2 reasons mainly: the fact that the __toString() of your record object will return this (makes your code look nice when you do echo $product instead of echo $product->getName()) and the link to the slugable behavior (automatic generation of URL-nice representations of that string)
  • findOneOrCreate()
    What?
    When you use one of the Query classes, you can end it with ->findOneOrCreate() to get either the record matching your query or a new one.

    Why?
    Most of the time, this is the only difference between a “add new” action and a “edit” action (e.g. when submitting a form to edit product details). Again, cleans up your code.
  • Inheritance
    What?
    Imagine you have categories for your products and a product can have multiple categories, but in the end, these categories just add a description and a value to your product data. Something like product color and product size. You could create separate tables for them or work with one general category table with a “type” column. But you can have the best of both worlds with Single Table Inheritance. And it only gets more beautiful with the other inheritance types.
    Why?
    In the example above, it generates separate Color and Size classes while in the back, the database just has one category table.

This is a quick summary, but I hope it makes clear why Propel is a pleasure to work with.


Tags: ,

 
 
 

2 Responses to “3 simple things I love about Propel”

  1. Gravatar of Darryl Darryl
    18. February 2013 at 10:01

    Hi, nice article and a quick question.
    For the findOneOrCreate(); example, if a record is not found, where does one set the new field data?

  2. Gravatar of Sam Sam
    18. February 2013 at 11:02

    The OrCreate part creates a new instance just like “new ModelName()” would.
    You whould just need to do ->save() at the end (but that goes for the update scenario too).

Leave a Reply