Laravel 4 database date fields

When reading date fields from MySQL , the date format (by default) is YYYY-MM-DD.  However, I needed to convert the field to MM-DD-YYYY when using the jQuery datepicker.  Additionally, when writing the date to the database, I needed to convert it from MM-DD-YYYY back to YYYY-MM-DD.  My initial solution was to write a couple functions in the Model to perform the conversions.  That worked until I wanted to properly do mass assignment (meaning I didn’t have to specify each field when saving).

Enter Eloquent attribute mutators.  In Laravel 3, the use of getters/setters allowed similar flexibility.  The “behind-the-scenes” magic allows an attribute (field) be altered without having to specifically make function calls within your code.  Let’s get straight to the code.

I’ve got a Student table and has a birthdate field.  Within the Student Model, I have two functions:

public function setBirthdateAttribute($birthdate) {
    if ($birthdate) {
        $this->attributes['birthdate'] = date('Y-m-d',(strtotime($birthdate)));
    } else {
        $this->attributes['birthdate'] = '';
    }
}

public function getBirthdateAttribute() {
    $tmpdate = $this->attributes['birthdate'];
    if ($tmpdate == "0000-00-00" || $tmpdate == "") {
        return "";
    } else {
        return date('m/d/Y',strtotime($tmpdate));
    }
}

With these two functions, anytime I reference the birthdate field, it automatically performs the appropriate formatting conversion. It’s that simple!

So how do you use it in your code? Not that much different. Naming the functions is as simple as prepending the field name with “get” or “set”, use the camel naming method to name your field (uppercase the first letter, lowercase the rest) and add “Attribute” to the end of the name. The “set” is altering the value when assigning value to the field, and “get” is used when pulling the current value of the field.

I am no expert and will gladly accept constructive criticism and correction. We’re all learning here so be nice. Please let me know if this helps you and if you have any additional tips!

Blessings!

Posted in development, laravel 4, php | Tagged , , | 10 Comments

The unfriendly side of separating business logic

A project that I’ve been designing in my spare time has been through a number of iterations and the latest has been the separation of the business and data layers.  All was going well until I went back to group certain data calls into a transaction.  Guess what?  Transactions and separating business/data logic don’t go together.

One option is to use TransactionScope, which would probably work, but introduces overhead I’m not willing to accept.  Another theoretical option is to manage the data connection globally.  This app is a service and is highly database driven, so keeping an open connection isn’t necessarily a bad thing.  Last option would be to scrap the logic separation which would bring back the maintenance monster that I’ve got accustomed to not having around with logic separation.

Anyway, if someone has some other options I’m willing to listen.

UPDATE: Well, after a few hours of brewing, I found the solution.  Stored Procedures.  I’ve not used stored procedures extensively, but this is the perfect solution.  Off to coding…

Posted in .NET, development, Uncategorized | Leave a comment

WebUI for PHP release 1.2a

This new release works well with Serviio v1.0.  Go to the wiki for more details.

Posted in php, serviio | Leave a comment

Serviio – WebUI in PHP

Giving 100% credit to the original author “Acidumirae”, I made an attempt at updating his 0.5.2.2 code to work with 0.6.  I trust that he will continue development and look forward to his official build.  This is simply a patched version to give him additional time to finish up.

serviio WebUI for PHP 0.6 beta

There are a couple things that I’d still like to do:

– Work on the Show Status option

– Change Online Sources section to a tab

– Change Online Sources check boxes to radio buttons.

 

Hope this helps someone in need.

mark

Posted in development, php, serviio | Tagged , | Leave a comment

Making use of this site

I’ve been using my www.moneybaron.org site as my WP site, but I see that may go away later.  I doubt this site will go away anytime soon, so I’m going to start switching over to this site.

Since I’m heavy in HL7 development, I’m probably going to focus heavily on interfaces.  Hope to be active.

This message will self-destruct in 2 posts….. :)

Posted in development, hl7 | Tagged , | Leave a comment