Nola Stowe's blog

Data Access Objects - Rails Style

Most other DAOs you have to create some configuration file, whether xml or ini files or created manually. Example, to setup Pear's DB_DataObject, you must create an ini file and run a script "createTables.php" each time your database changes. Not so with RoR! its automagically created and updated for you. It does ALL the basic CRUD for you. Just have to specify the relationships (if any).

Say you have tables:

CREATE TABLE people (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id)
);


CREATE TABLE companies (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id)
);

Naming Convention
By the way, RoR has a naming convention for tables. You name the table the plural version of the word. The primary key has to be ID in each table, foreign keys are _id. EGADS! It almost looks like English, huh? An average half-way intelligent person could even read it and have a clue of what’s going on. Even pointy haired bosses. Though, in traditional fashion, some people have to complain about having to name their tables a certain way. (violin music playing) There is a way to turn off "plural/singular" in the settings. So if you just have to be difficult, here ya go.

Data Access Objects - or- Why CRUD really gets on my nerves

Why I hate CRUD.

BTW: I’m in a bit of, as my husband says "pissy" mood (to which I protest I’m NOT PISSY!!! so it turns into sort of a game). So I’m being a bit sarcastic in this post.

No, not the piles of mouse crud and newspaper shredding you find under a china cabinet when you move your grandparents after having lived in the same house 46+ years EGADS!!!!!!!!!! I’m talking about:

C reate

R replace

U pdate

D elete

When you have an application with sql statements scattered here and there, and then you need to change a fieldname, or table name, or something. What do you have to do? Make sure you update all sql statements that reference that field/table through out the masses of code. What’s more, if you typo’ed a field name, or forgot to add a comma (I’ve done that) then you have to find it and fix it. Wouldn’t it be nice to have all sql queries in one place?

Whats a DAO??

As described by Martin Fowler:
"An object that wraps a row in a database table or view, encapsulates
the database access, and adds domain logic on that data."

This comes up so often in web development, get input from a form, validate, save in database. Select data from database, display. Blah blah blah. This is a “common task” so there is a “common solution” called a Design Pattern, for those of you new to the concept. This particular one is called Data Access Objects (DAOs). There are a number of DAO packages out there or if you like, or you can make your own.

Typically they look like this (the is psudeo-code, only looks suspiciously like php because I think in php!):


Class Person {
var $id;
var $name;
var $age;
var $companyId;

function create($name, $age) {
// sql to create new database entry
}

function update($name, $age) {
// sql to update entry
}

function getCompany() {
// retrieves company information, possibly returning a company DAO
}

function getById($id) {
// sql to retrieve record with id= $id and load values into members
}

function getByName($name) {
// sql to retrieve record with name=$name and load values into members name and age
}

function getAll() {
// retrieve all records
}
}

PEAR’s DB_DataObject
Works in conjuction with PearDB, probably one of the most popular database abstraction packages out there. (some confuse DAO with Database Abstraction, they are different!) Allows linking with multiple tables. Pretty nice.

DotProject has a nice DAO system, using a class CDpObject. They use ADODB as their db abstraction, but enclose it in an easy to use wrapper.

SugarCRM has a “Sugar Bean” that looks like a DAO system, but I haven’t taken more than a casual glance at it and said “wow..cooool beans”

(TIP: Good way to learn programming – look at Open Source projects and see how they do it! then evaluate to see if its good)

The most annoying is when, you find you need a DAO and are unable (reasons I can’t explain even if I wanted to) to use a DAO package at my job and it just drives you NUTS to write your own when some already exist (hence the reason for my frustration). Some people may enjoy reinventing the wheel, not me. I have more important things to do… I will delvuge somethings in a few days that I've learned about Ruby and DAOs... very cool stuff indeed.

Meanwhile, you can read more about this topic:

Any other links you know of that might be helpful? add a comment!

Thanks, I feel better now. :)

Ruby for the PHP Programmer ...

This will be a series of my findings in the syntax and language of Ruby from a PHP perspective.

Displaying and formatting output is one of the first things you need to know when learning a new language.

PHP:

echo Look it up at PHP.net
echo value [,values];
This is not a function, so ( ) is not needed. It is a language construct. Hmm, something I just learned. it will concat the variable for you and display. This might come in handy, sometimes is a pain to always do print $var1 . " " . $var2; Sometimes those periods are easy to forget!

Example:

$first = "Nola";
$last = "Stowe";
echo $first, ' ', $last, "<br>";

print Look it up at PHP.net
print value;
Also not a function but a language construct, so () not needed here as well.

Example:

$first = "Nola";
$last = "Stowe";
print $first." ". $last."<br>";
// or
print "$first $last <br>"";

results in:
Nola Stowe

For more on the differences of print and echo, read: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

In most cases, I usually use print.

Also availble in php, but I never rarely use:
printf (string format, [, mixed args]); uses a formatting string
sprintf (formats as printf, but reutrns a string rather than printing)
vsprintf (like sprintf, but accepts an array of values instead of a variable number)
vprintf (like printf, but accepts an array of values instead of a variable number)

Ruby:
(which BTW, does not require ; at end of line, local vars don't have $ those are reserved for globals)


first = "Nola"
last = "Stowe"


print first, ' ', last, "<br>"

prints contents of variable test followed by test2

results in:
Nola Stowe

However, it appears some special variables (lookie! a $!!! that means they are global)


$, = " " #displayed between variables in print
$\ = "<br>" #displayed at end of the line

Will set some defaults for the print statement, so once those are set, all you need to do is:


print first,last

To get the same result. Cool, huh?

Also in Ruby I found puts, and putc. I need to do some more investigation on those. Perhaps a reader can shed some light?

So you can see, PHP's echo is most like Ruby's print. That should help us die hard php programmers who normally use print exclusively.
Also in Ruby, which seem to work like the php versions:
printf
sprintf

<?="Nola's Introduction"?>

Greetings fellow developers! I hope to use this blog to share some things I’ve learned and generate some good discussion. Feel free to point out my flaws and add your thoughts. Although I am stubborn, I do admit when I am wrong… if you succeed in convincing me that is! ;)

Where I Started
I got my first computer when an Uncle gave me his TRS-80 and we hooked it up to a small tv. We plugged in cartridges and played games. In the summer I turned 13, I found a BASIC programming book, learned how to do simple programs, save files to cassette tape and use the 6 inch wide thermal paper printer. I made the screen flash random colors and make math games for my siblings. That was the end of my summers playing outside.

From there, I progressed to a Tandy 1000sx. I discovered Print Shop, and spent hours making big blocky graphics. In fact, I used the printer so much, one day it started smoking! Luckily, it was 1 month before the warranty was up, and I hauled it back to Radio Shack. When I got a color monitor and Photo Shop Deluxe, I was ecstatic. I don’t think I slept for a week. I could make my own fonts and produce fairly crisp graphics. I bought some more Basic programming books and made more simple applications.

Later on, I dabbled in C/C++ and VB. I discovered online BBSs when I was 16. I saw all the shareware available and was very disheartened. Surely nobody wanted anything I could ever write. I still programmed but didn’t have any goals of making anything grandiose applications. I remember a friend of mine going to college around 1996 and asked if I would like a “Homepage.” What’s that I say? He said a page about you for the Internet! I was like, ummm..no. Not having a clue about the Internet. When I got on the Intranet myself about age 18 using my Dads university account, I found out for myself what it was all about using Lynx, Zmodem downloads and gopher!

I thought I would like to do Desktop Publishing, so I took a class then went out to find a job. Nobody wanted to hire some crazy girl marching in saying she knew how to do DTP. So I went to college and majored in Computer Information Systems and minored in Design Studio. I better understood programming languages now, design concepts and databases and found that I was good in web development, since it can include programming AND design. I found my niche in the programming world!

Where I am Now
I work for an international engineering firm, doing mainly intranet programming with PHP and MySQL. On the side, I do contract work for Casey Software on various web development projects and some projects on my own. I have a dedicated webserver where I manage around 14 sites for friends, family and a few businesses. I am interested in OS projects like DotProject, SugarCRM, Smarty, Gallery and plan on being involved in new development where I can assist.

Where I want to go
Never content to just know what I know, I am always learning something and trying new things. I dabble in .NET from time to time, play with VB and Java. I spend most of my time with things like perl (which I know some, but not very well), python and ruby. One of my quests is to write a php class worth of being includedin PEAR.

So thanks "Al-Gore" for "creating" the Internet… it pays the bills now :)