Search code examples
ooppropel

Propel - best practice for implementing a method - shall I save the object inside or outside the method


I am using Propel, a PHP ORM.

I really like it. I love to be able to write this code:

$offer->setTitle('20% off everything')->save();

But what about if I want to create a increaseImpressions method?.

Would you use it like this:

$offer->increaseImpressions()

or like this:

$offer->increaseImpressions()->save()

?

In other words, would you save the object inside or outside the increaseImpressions method? Can you also please tell me why?

I guess this is a question about OOP.

Thanks, Dan


Solution

  • I would suggest the the second example:

    $offer->increaseImpressions()->save()
    

    Essentially, the increaseImpressions method is just another setter. Calling save outside the increaseImpressions method keeps it consistent with the rest of the setter methods.