Help with PHP DataObjects tutorial

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tezcat
Forum Newbie
Posts: 1
Joined: Mon Mar 26, 2007 9:15 am

Help with PHP DataObjects tutorial

Post by Tezcat »

Hi, don't know if this is the right place to post this or not. I can't seem to post a reply to the tutorial section.

Anyway, I stumbled across Maugrim_the_Reaper's tutorial PHP DataObjects: Simplifying Business Logic without SQL and really liked it.

I just have a question, maybe more later, on one function in the DataAccess class - getRow(). I understand how it works, but I'm just wondering what's the reason for clearing all the data values in the DataObject class if no records are found?

Code: Select all

<?php

        // ...

        function getRow(&$do) {
                $conditions = $this->getSetFields($do);
                $sql = 'SELECT * FROM ' . $do->getTableName() . ' WHERE ' . implode(' AND ', $conditions['fields']);
                $result = $this->db->SelectLimit($sql, 1, -1, $conditions['values']);
                if(!$result)
                {
                        trigger_error($sql . '<br /><br />' . get_class($this) . ': ' . $this->db->ErrorMsg(), E_USER_ERROR);
                }
                if(!empty( $result->fields ))
                {
                        $do->import( $result->fields );
                        $do->setExists();
                }
                else
                {
                        $do->clearData();
                }
                $result->Close();
        }

        // ...
?>
What if, say for instance, I set the name and email of the user in the User object and then want to find if that user exists in the database to see if that name and email are available (this would be for something like registration). If I use getRow() and that user doesn't exist, it will clear all the data I entered. Is this the correct way of using getRow()?

Thanks,
Nathan.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I have a feeling it's to "reset the form" as it were. It would be trivial to forget a field and get stuck trying to select a row that doesn't exist because of it. - chances are better you'd be in a loop trying to find a matching record by reducing / changing the matching data set.

@maugrim: just read the tutorial myself and I'm blown away -- great job! I especially like your implementation of Data Access Objects - I'll build that into kQuery now :-) (I was using... gulp... globals :-()
Post Reply