Archive for 'Code'

CakePHP Shell Not Seeing MySQL with MAMP Pro MAC

MAMP is a great product. It makes using the MAMP stack on MAC very easy. I liked it so much I actually purchased MAMP Pro. Installing was a breeze, configuration is simple, and it is up and running in no time. However, for some reason, when I am trying to connect to MySQL from a CakePHP shell, the database connection cannot be found.
Read more

Single Login For Multiple User Types with CakePHP

I started working on a project that requires a single login for multiple user types. Because of the way my mind organizes things, I was having difficulty getting my mind around multiple models pointing to the same table in the database. It just felt messy. But I found this to be the best solution. I can provide single user login and keep the user functionality separated.

In this case, I have managers and tenants both pointing to the users table. I simply add a ‘type’ column to the user table to track what type of user it is. Then in my callback methods on the models, I set the conditions for the beforeFind and the beforeSave within each model to it’s corresponding type. This keeps both the data it displays and the data it saves in check and accurate. Here is what my manager model looks like.

<?php
class Manager extends AppModel {

 var $name = 'Manager';
 var $useTable = 'users';

 // only return users where type = Manager
 function beforeFind($queryData) {
$queryData['conditions']['Manager.type'] = 'Manager';
return $queryData;
 }

 // Ensure the Type is set to Manager before saving this record
 function beforeSave() {
$this->data['Manager']['type'] = 'Manager';
return true;
 }

}

?>

The same would apply to the tenants model. This works like a charm. It helps to provide excellent separation between user types without writing a lot of extra code to manage and check for the differences in user types.

Happy coding!

Open Source CakePHP Components

CakePHPI am getting more focused in my engineering career. Lately I have been feeling a strong desire to contribute to the open source community. Of course by open source community I mean contributing code and making it available to anyone who wants to use it… for free. As such, I decided to pick something I am passionate about. I have decided to create components, helpers, and other bits of code that are usable with the CakePHP framework. I have also decided to make these snippets of code available on GitHub.
Read more

Domain Validation Class Updated

Around three weeks ago, I sent an email to iana.org about their TLD file. (http://data.iana.org/TLD/tlds-alpha-by-domain.txt) This is the file that contains a list of all the valid top level domains (TLDs) on the Internet.  Here is a snippet from the email I sent requesting how to best access this file for my domains class:
Read more

Hidding Table Row While Preserving Colspan

Someone asked me today how to write the CSS for a <TR> so that the <TD colspan”2″> would still maintain the colspan when the display was changed from none to show. It turns out the solution is easier than you might think. He actually came up with the solution, but I took it a step further and updated it so include a toggle. Now you can repeatedly change the row from hidden to visible. Keep reading to see the solution.
Read more

Source Code Management with Git

Linus Torvalds is the man who started Linux. Known in it’s early stages as the Open Source Unix. Since then, it has evolved into something even bigger than I think he even imagined. Maintaining the source for a project that large can be time consuming and difficult. Linus said that he would maintain revision control with tar balls and it was easier than using CVS. But he needed something that would work in a distributed fashion and allowed for easy merging. But there were no open source options available.

This, GIT was created. It is designed from the ground up to be a true distributed repository. Think of torrent source coding. There is no one central location, but everyone has the master branch at any one time.

I have been a big fan of subversion, but after listening to this talk, given by Linus at Google, I am considering switching to GIT. And to think, I recently paid $60 for a Subversion application to run on my mac. Ouch!

Real World Email Validation for PHP

Learn PHPHave you ever gone to a website that asks for your email? You really want to get in to the portion of their website that has the information you want, but the only way they let you in is by giving them an email address. So you are faced with a moral dilemma. Do you give them your real email address, do you give them your friends email address, or do you make one up? If you are like me, you probably make one up like a@b.com or noway@example.com. Most of the time this will get you in where you want to go without the hassle of being bombarded with all of their junk mail. Now I don’t worry about giving them my really email since I use gmail. I can just filter it. usually google filters it for me, so I am good.
Read more

Parsing Comma Separated Value (CSV) Files with PHP

Learn PHPI hate trying to parse CSV with PHP. I get PO’d ASAP because everyone should be using TSV instead. I know, it can be as difficult to comprehend all of the acronyms as it is to figure out the comma separated values. Not impossible, just very time consuming.

I have had multiple occasions where someone gives me a file and asks me to parse it. The issue is its in CSV format. CSV Sucks! The problem is when you get someone trying to be cute so they put a comma (,) in one of the records.

So let me back up. CSV started out as a basic format to make data portable. A CSV file is basically a database in a way. Each line in the file represents a record. Each field within the record is delimited by a comma. Or so it is suppose to work. So typical CSV looks like this:
Read more

Domain Name Validation Class for PHP

Learn PHPBy the time I finish writing this, there will probably be 10 new domain validation routines uploaded somewhere on the Internet.  The only problem is, the majority of them are incomplete. Incomplete would be the equivalent to wrong, depending on what you are looking for in a validation routine. The problem? Most developers don’t read the requirements / specifications. The majority of developers do not take time to RTFM (read the freaking manual) for anything. What makes you think we will spend time reading the ever so boring RFC documents?

Domain validation is a common issue as evidenced by the more than 42,000,000 results on Google when you search for domain name validation php. But who can you trust to do it right? If I were to ask you what makes a valid domain name, do you know? Do you know how long a domain name can be? Do you even really know what a domain name is? Continue reading, and I bet you will be enlightened.
Read more

PHP Phone Validation Class

A few days ago, I wrote a post about validating NANPA regulated phone numbers. So late last night, I figured since I have already done the research and figured out the work, why not build a class and put it online for everyone? So that’s what I did.

During the process, I realized how easy it would be to apply the “optional” filters I discussed in my previous post. I document them on the Google Code wiki where my open source repository is.
Read more