Doctrine2 and Postgresql timestamp with millisecond issue

I’ve been doing a lot of work with Doctrine 2 and finding some issues when dealing with PostgreSQL and Doctrine which I’ll be blogging about. I’ve ignored this particular issue for some time and finally decided to address it.

The issue is declaring a Doctrine entity property as datetime if the column in the db is storing the timestamp with milliseconds such as ’2012-01-01 10:12:35.542′. When you try to load that into your entity, Doctrine will give you an error. An example of how this can happen is declaring a column timestamp without time zone and using now() as the default value.

Here is an example of a Doctrine entity property declared as a datetime

/**
* @Column(type="datetime")
*/

protected $discontinued;

And what the column sql declaration looks like

discontinued timestamp without time zone NOT NULL;

There are three solutions that work, one is to configure Doctrine to not use the default DateTime classes, or you can declare your datetime properties as strings in your entities and convert them to datetime yourself if needed and finally the other approach is to update your db to not store milliseconds. I considered all approaches are decided that it would be better and more efficient to update the db than to write some custom DateTime classes or treat dates as string. If you would like to tackle this issue by creating your own custom DateTime classes then follow this link for more information.

I used the following query to identity all the timestamp columns in my db table:

SELECT table_name, column_name FROM information_schema.COLUMNS WHERE table_catalog = ‘gatweb2′ AND table_schema = ‘public’ AND udt_name LIKE ‘timestamp%’ ORDER BY table_name ASC;

Noticed I used like timestampe% instead of = timestamp. This is because you could have timestamptz (timestamp with time zone) columns as well. Once I had the list, I saved the results, did some search/replace magic to come up with alter statements for each table and ran the queries. I spent a little bit of time looking at cursors to see if I could write a cursor to loop through the list and alter each table but it was taking too long research and search/replaced was quick. This is what the alter queries look like for each table:

ALTER TABLE "contact" ALTER "date" SET DATA type timestamp(0) without time zone;

By altering my timestamp columns to timestamp(0), it lets Postgres know to remove milliseconds from all values as well as to not store milliseconds in the future.

An issue I ran into when running all my alter statements was that some views were referencing some of the columns I was about to alter and Postgres didn’t like that. I did some research and it seems my only choice was to drop those particular views, alter the tables, then recreate the views. I created a big script that did all that, ran it, and now all my issues are gone and my unit test passed.

Tags: , , , ,

Slides from Automation with Phing presentation for Codeworks 2012 Atlanta

I had the pleasant opportunity to present at Codeworks this year. I’ve presented at the Atlanta PHP User Group a few times but this was my first time presenting at a conference. Overall I think it went well and I learned a lot from it. I picked Phing as a topic as we have been doing a lot with Phing in the last year at work. The presentation covers some of the improvements we’ve made to one of our applications by automating a few processes that we used to spend hours on. Here are the slides:

 

Tags: , , ,

Automate SVN Export to Site w/ Bash Script

So at work we are finalizing the setup of a new server environment. The site is in PHP and the code is all in SVN. We were trying to decide what process to use to export the SVN contents to the site and that’s where I decided to learn how to write a bash script. This is my first and with some help from Jess we created the following script. The script does the following:

  • Does an info on the remote repo to get the revision number
  • Checks against local revision number which is stored in a file
  • If the revision numbers don’t match, it does a diff on both revisions and creates an list with the files that were changed
  • It then loops through each file and exports it to the site
  • Finally it stores the new revision number in the file
Feel free to use this and tweak it for your needs. This is our first draft, at this point we’ll start cleaning it up and adding more functionality but it works. Make sure to add a cron job to run it every so often and enjoy.
#!/bin/bash
# need to figure out what to do on files that need to be deleted
TARGET_DIR=‘/path/to/site’
REPO="svn://path.to.svn/repo"
REVISION_FILE=‘.revision’

echo "Getting info from remote repo"
REMOTE_VERSION=$(svn info $REPO | grep Revision)
REMOTE_VERSION=${REMOTE_VERSION: -4} # need to update to not hardcode 4 spaces back
CURRENT_VERSION=$(more $REVISION_FILE)

echo "Current Revision: $CURRENT_VERSION"
echo "Remote Revision: $REMOTE_VERSION"

if [ "$REMOTE_VERSION" -eq "$CURRENT_VERSION" ]
then
        echo "No export needed"
        exit 0
fi

echo "Getting diffs between revisions"
difflines=`svn diff –summarize -r $CURRENT_VERSION:$REMOTE_VERSION $REPO 2>&1 | awk ‘{print $2}’`

URL_LENGTH=${#REPO}

for i in `echo $difflines`; do
   FILENAME=${i:$URL_LENGTH}
   echo "svn export ${i} ${TARGET_DIR}${FILENAME}"
   svn export ${i} ${TARGET_DIR}${FILENAME}
done

echo "Saving revision number"
echo ${REMOTE_VERSION} > $REVISION_FILE

Tags: , , , ,

Zend_Json_Server and how to call it via JSON-RPC 2.0

So I started playing with Zend_Json_Server and was having a hard time trying to figure out how to call the server from a client. Finally I checked the JSON-RPC 2.0 spec and read a very important detail that I had not realized:

The Request is expressed as a single JSON Object

This was the key to my problem. There are 4 parameters that can be sent with each JSON-RPC 2.0 request but I was sending each as a post var which is not what Zend_Json_Server expects. It simply wants one json encoded object with all the parameters inside. The 4 available parameters are:

  1. jsonrpc – the version you are using; I’m using 2.0.
  2. method – the name of the method you want to call in the server.
  3. params – object of parameters your method needs. If you don’t need any, don’t send this param.
  4. id – an identifier (anything you want) that will be sent to and from the server for this request.

I’m using Zend_Http_Client() to make the request and here is an example:

$params = array(
        ‘jsonrpc’ => ’2.0′,
        ‘method’ => ‘find’,
        ‘params’ => array(’326691′),
        ‘id’ => ‘test’
);

$http = new Zend_Http_Client();
$http->setUri(‘http://localhost/path/to/jsonrpc/server.php’);
$http->setMethod(Zend_Http_Client::POST);
$http->setRawData(json_encode($params));

echo $http->request()->getBody();

Read the rest of this entry »

Tags: , , , ,

Facebook Graph API App Easy w/ PHP SDK

NOTE: This post was written using Facebook’s PHP SDK version 2.1.2. Since this post was made, the PHP SDK has changed and some of the process that are explained below may have changed as well. At some point I’ll have to revisit this post and update it but at this time just keep in mind of the above.

As promised, here is a post (similar to my Twitter API post) on using the Facebook API. There are many reason why one would want to access the Facebook API – maybe to create a mobile app that lets you post photos to your Facebook albums, or maybe you just want to show your last few Facebook status updates on your blog; what ever the reason may be, Facebooks Graph API mixed in with their PHP SDK makes it really easy to accomplish this.

Objectives

  • Setup our environment
  • Register an app on Facebook
  • Understand the authentication process and extended parameters
  • Understand Graph API
  • Retrieve our latest status updates
  • Add a new status update
  • Retrieve our photos from our albums
  • Add a new photo Read the rest of this entry »

Tags: , , , , , ,

Twitter API, OAuth Authentication, and Zend_Oauth Tutorial

I recently had to work on a project that required me to interact with the Twitter API. I had done this before so I wasn’t expecting anything different until I remembered that Twitter had changed their API to start using OAuth for authentication. If you are not familiar with OAuth, it’s a secure way of authenticating without requiring a user to submit their username and password to third-parties – you can read more about it at OAuth. There are lots of resources online that talk about this in detail but I wasn’t able to find one that explained the entire process in a way that made sense. Hopefully this post will give you everything you need to get started with the Twitter API. I’m going to go through the steps required to make this work without using the entire zend framework.

Objective

This tutorial will go step-by-step in explaining how to create a small PHP application that can interact with the Twitter API. Our goal is to:

  • Authenticate
  • Display our latest tweets
  • Post new tweets from PHP
  • Display the last few times our account was mentioned

The only assumptions at this point (other than knowing PHP) is that you have a twitter account and the zend framework library downloaded. We won’t be using the entire framework, just some of the files as standalone modules.

Registering An App

The first step in being able to communicate with the Twitter API is to register an app in their system so you can receive all the necessary keys to authenticate with OAuth. Go to dev.twitter.com and log in with your Twitter account. Now click on ‘register an app’ (if that link is not visible then click on ‘your apps’ on the top right and then click on ‘register an app’ in the next page). These are the values I put in the form on the next page for my app. Feel free to follow along. I’ll explain the important inputs.

Application: Joey’s Blog Example
Description: Twitter PHP App
Application Website: http://www.joeyrivera.com
Organization: None
Application Type: Browser
Callback URL: http://www.joeyrivera.com/twitter/callback.php
Default Access Type: Read & Write Read the rest of this entry »

Tags: , , , , ,

My car is for sale! 2005 Subaru WRX STi, 51,300 miles for $21,500 – SOLD!

Here is the link to autotrader with all the info: My car for sale. It’s a great car and I had a blast with it but I have bought a replacement, a Z06. The suby has a lot of mods, you can read about them all in my blog under ‘my-car‘ – pushed 450whp with methanol. Contact me here or at autotrader for more questions. Here are some pictures of both:

Read the rest of this entry »

Tags: , , ,

How to build an Adobe AIR Badge

NOTE: This article was written with AIR 1.5 in mind. I can’t say if the following information will work as intended on AIR 2. If you already know how to build you own Adobe AIR badge, you won’t find much new information here. This post is more to inform users that you can in fact create your own AIR badge if you didn’t already know. When I first started working with AIR, I learned about the badge but as far as I was concerned, the badge was a black box who’s functionality was designed to remain mysterious.  The badge I used was the one Grant Skinner created and you can get more information from Adobe on how to use it. For a long time this badge worked well but finally the time has come where I need to make it behave a bit differently so I decided to do a bit of research.

After doing a few Google searches, I found a link to Adobe’s site with all the technical information on how to create your own AIR badge! I was completely surprised that I never even thought about the possibility that I could write my own badge… not sure what I was thinking. Nothing is more fun that reinventing the wheel right? After looking at the specifications, I realized it wasn’t too bad and started to plan out a logical flow on how my badge was supposed to work.

Requirements

My current badge has the following issue: a user installs my app through the badge, every time after that point that the user comes back to the badge, they have to ‘install’ the app again even thought it was already installed. Instead, I want the badge to sense if the application is installed and if so, launch it (I haven’t spend much time looking into Grants Badger app but I think it’ll let you do this as well if used correctly… I think). The logic for the new badge should be:

  1. Check if AIR is installed (If not, install)
  2. Check if my app is installed (If not install)
  3. Launch my app

Setup

I’ll be using Flash CS3 since that’s what I have available to me for this tutorial and I am making the assumption you already know how to use Flash, understand ActionScript 3, and have an AIR app to test. Open Flash and create a new AS3 file and save it in some folder. Now attach a class called ‘Badge’ to your file that extends MovieClip. Your class should look like the following:

package
{
        import flash.display.MovieClip;

        public class Badge extends MovieClip
        {
                public function Badge():void
                {
                        trace(‘hi’);
                }
        }
}

I added a trace in the constructor so I can make sure my class is working. When you test your badge you should see ‘hi’ in your output window. Now we are ready to continue. Read the rest of this entry »

Tags: , , ,

What have I been up to recently…

Last month was an action packed month for me. I had a few events going on and lots of work to get done.

Atlanta PHP User Group

We had a really cool presentation on load testing using JMeter and Xdebug by Brian DeShong. Load testing and profiling are very important to help identify possible bottlenecks in your application/server before reaching a breaking point. JMeter allows a user to setup different scenarios to run with varying loads (number of users, ramp up time). It’s pretty fancy with all the options you can do such as submit variables to pages to simulate login ins to an application and tracking sessions. Xdebug lets us analyze logs to see what was going on in your application to easily spot which part of the execution process took the most amount of time. At that point, you can look at your code to see how to improve that particular process.

At work we have an outside firm doing our testing/load testing so it was nice learning about JMeter so eventually we can start doing some of our own load tests for things we need done asap. I currently use Zend Studio’s profiler to profile my applications so I probably won’t be using Xdebug any time soon.

TEK-X

I asked around our local user group what conference they would recommend if they could only attend one PHP conference and this was the one I heard the most good things about. This was my first time attending a PHP conference and it was WELL worth it. The sessions were great with three tracks daily to choose from. The conference was small enough where you could spend time with the presenters interacting and asking questions. I met a bunch of new people which made the whole experience that much better. There was always something to do during presentations as well as after hours.

This is the web site for TEX-K if you want more info or want to take a look at the schedule. Some of my favorite were the two Flex sessions by Keith Casey since I want to start doing more Flex and the Lost Art of Simplicty by Josh Holmes was interesting as well. It was really nice having a bunch of Adobe and Microsoft people there. They were all great and helpful. We had been having some server hiccups at work and the guys at Microsoft were extremely helpful in trying to help us figure things out. We also learned from various people (both the MS and PHP crowd) that we (at work) should start using IIS instead of Apache for Windows. Microsoft has been spending resources in working with the PHP team to make sure things run smoother with PHP on IIS for Windows than before.

Two thumbs up for Microsoft and Adobe for being there and supporting the community (and for the drinks and dinners :p) . I’ll definitely try to go next year again.

Flash Builder for PHP

I took a one day-8 hour course on using Flash Builder and wow… I’m sold. After working on Flash for so many years, Flex is just too amazing to ignore. The class was taught by Adobe certified Jeanette Stallons and it was a small class which was great for us since we could get a lot more time for questions and one on one time. She did a great job keeping our attention and was very easy to follow. This was a hands on class so we each had our own computer to work the examples with.

The class started with us learning about the different Adobe applications, the difference between Flash/Flex, and reasoning behind changing the name Flex builder to Flash builder. Then we went through the basics of the IDE and how to setup a project and what the different folders and files are for. Next is when things got fun. We started creating an application that communicates with the database through PHP. We did all the code manually to better appreciate the last lesson which was creating the exact same application in an automated fashion with literally only a few lines of code at most. That was the coolest part of this class, how easy it is to use the wizards (that actually spit out good code!) to be able to just click and drag and have everything working correctly in such a short amount of time.

I already downloaded the two month trial from the Adobe’s site and have been playing around with Flash Builder at work and at home.

Google AdWords API v13 to v2009

A few years ago I wrote an application that heavily uses the Google Adwords API. It worked without me touching it for years until recently Google changed their Adwords API and my app completely stopped working. This was my fault of course since many months ago Google emailed me letting me know this was going to happen but at the time I figured there wouldn’t be much difference between both versions so I didn’t pay much attention to it… boy was I wrong.

The new API v2009 is very different since you call things differently now and many of the old services I was using no longer exist in the new version. I spent some time trying to rework my code to use the new calls but that was not working well for me. It just seemed as if I was wasting time instead of just starting over with a clean rewrite. Fortunately for me, after doing some research, I found that Google had already created a client library for PHP that already did all the heavy lifting code to interact with the services. I downloaded the library, ran a couple tests and everything was working perfectly. All I had to do was rewrite the application using the client library and everything is running smoothly again.

Thanks Google! You saved me a lot of time but don’t change your API again :P

My Birthday

Not much to mention here other than it was my birthday last month as well so had dinners with parents and drinks with friends. I have been running a lot lately so I bought myself a nice new watch with my birthday money: Garmin 405. I saw this watch on sale on slickdeals.net for under $200 and I couldn’t resist. I’ve used it a couple times already and it’s great. The GPS seems to be working flawlessly for me so far and all the features it brings are neat to play with. My biggest sellers were: GPS to track my distance and pace and the heart rate monitor.

Peachtree Road Race, here I come!

Two thumbs up for the wifey!

So my wife submitted a design for an eco-friendly bag contest for Kroger late last year. There were over 46,000 entries nation wide and she ended up winning second place!!! Great job sweets!

Starting next month (April), if you shop at Kroger, you may be able to purchase her bag from your local Kroger store. Here is an article that just came out today from a local magazine:

http://www.mdjonline.com/view/full_story/6794712/article-Marietta-graphic-designer-takes-2nd-in-Kroger-s-reusable-bag-competition?instance=home_news_1st_right

Feel free to visit Ashley’s blog and let her know she did a great job: Ashley’s Blog

Tags: , ,