SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Row Data Gateway & MVC

    Here is an attempt at a Row Data Gateway, first I'm curious as to if this is correct, and second how you would use this in an MVC framework?
    Say for the "view" where you want a list of links to edit or delete, would you loop thru the array of objects returned by the LinkFinder::findAll() method?
    for example:

    PHP Code:
    class LinkView{
      ...
      function 
    listLinks()
      {
         
    $links = &LinkFinder::findAll();
         foreach(
    $links as $link)
         {
            echo 
    $link->getLink()"<br>";
            
    // edit and delete links here
         
    }
      }

    and have the edit and delete links resemble this: ...index.php?link_id=2
    which would bring up a form:

    PHP Code:
    class LinkView{
      ...
      function 
    linkForm()
      {
         
    $id $_GET['link_id'];
         
    $link = &LinkFinder::findById($id);
         
    // echo form here
         // ... value="$link->getLink()"
      
    }

    and then to process the form:

    PHP Code:
    class LinkModel{
      ...
      function 
    processForm()
      {
         
    $id $_POST['link_id'];
         
    $link $_POST['link'];

         
    $l = &LinkFinder::findById($id);
         
    $l->setLinkId($id);
         
    $l->setLink($link);
         
    $l->update();
      }

    Here are the gateway and finder classes
    PHP Code:
    class LinkGateway{
        
        var 
    $table "links";
        
        var 
    $link_id NULL;
        var 
    $link NULL;

        function 
    LinkGateway($rowArray=NULL)
        {
            
    //    Initialize and set attribute values
            //    if array is passed

            
    if($rowArray)
            {
                foreach(
    $rowArray as $key=>$value)
                {
                    
    $this->$key $value;
                }
            }
        }

        
    /*
        //    Getters and Setters
        */
        
    function setLink($link){
            
    $this->link $link;
        }

        function 
    getLink(){
            return 
    $this->link;
        }

        function 
    setLinkId($id){
            
    $this->link_id $id;
        }

        function 
    getLinkId(){
            return 
    $this->link_id;
        }

        
    /*
        //    insert/update/delete
        */
        
    function insert()
        {
            
    $sql  "INSERT INTO $this->table \n";
            
    $sql .= "(link_id, link) VALUES('$this->link_id', '$this->link')\n";
            
            echo 
    $sql;
        }    
        
        function 
    update()
        {
            
    $sql  "UPDATE $this->table \n";
            
    $sql .= "SET link='$this->link' WHERE link_id='$this->link_id'";
            
            echo 
    $sql;
        }
        
        function 
    delete()
        {
            
    $sql  "DELETE FROM $this->table WHERE link_id='$this->link_id'";
            
            echo 
    $sql;
        }

    }

    class 
    LinkFinder{
        
        function 
    findById($id)
        {
            
    $q =& new DAO;
            
    $q->queryType("SELECT");
            
    $q->database('my_database');
            
    $q->table("links");
            
    $q->query("SELECT * FROM links WHERE link_id='$id'");
            
    $q->execute();
            
    $row $q->fetchArray();
            
    $linkObject = &new LinkGateway($row);
            return 
    $linkObject;
        }

        function 
    findAll()
        {
            
    $q =& new DAO;
            
    $q->queryType("SELECT");
            
    $q->database('my_database');
            
    $q->table("links");
            
    $q->query("SELECT * FROM links");
            
    $q->execute();
            
            while(
    $row $q->fetchArray())
            {
                
    $linkObjects[] = &new LinkGateway($row);
            }
            return 
    $linkObjects;
        }
        

    Last edited by MikeShank; Jan 1, 2004 at 08:48.

  2. #2
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, scratch the second part of my question. How about the first question, do I have a proper row data gateway here?

  3. #3
    SitePoint Zealot ZangBunny's Avatar
    Join Date
    Jul 2003
    Location
    Mainz, Germany
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks pretty row-data-gateway-ish from where I'm standing.

    One thing though: Your LinkFinder uses a class called "DAO" for database access, but the way it is used doesn't look like a DAO but more like some kind of abstraction for the database. After all, using a DAO here wouldn't make much sense, since a "DAO" is just another name for a Table Data Gateway, which would make you Row Data Gateway kinda pointless.

    (What kind of database are you using anyway? I never heard of any where you just echo your SQL to make a query... )
    Things that try to look like Things, sometimes
    look more like Things than Things. - Granny Weatherwax

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql  "INSERT INTO $this->table \n"
            
    $sql .= "(link_id, link) VALUES('$this->link_id', '$this->link_timestamp')\n"
             
            echo 
    $sql
    Ahem.

    Happy New Year to all btw; Best of luck for 2004

  5. #5
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "DAO" class here really just returns either a row, array, or object from a mysql query. I'm kind of new to OOP and don't remember where I borrowed that from. Obviously it should not be called "DAO" maybe just "Query"?

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeShank
    The "DAO" class here really just returns either a row, array, or object from a mysql query. I'm kind of new to OOP and don't remember where I borrowed that from. Obviously it should not be called "DAO" maybe just "Query"?
    If it's really just database abstraction, something like ResultSet or QueryResult might be appropriate, or maybe DbConn. Regardless, you shouldn't need to set query type, table or really even database (although that one's very arguable). To get a query result, an object only needs a database connection and a query string. If you haven't already, you might take a look at the eclipse library, particularly the Database and QueryResult objects.

    The connection and result handler classes sit at the lowest level. On top of those are the data source patterns like RowDataGateway and DataMapper, which act as translators and often as transfer objects to marshal data between persistent storage and model objects. So the MVC architecture sits on top of the data source and abstraction layers -- at least that's the image in my head. I'm hardly the resident MVC expert, but that's the way I do it and it works for me.
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •