SitePoint Sponsor

User Tag List

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

    What should these models look like?

    My link object has methods for returning the sql statements needed for creating/updating/deleting itself,
    but my menu object actually performs these queries. Is it ok to have your objects contain this type of data, or should it be separated off into another object, like a data mapper?
    Should the link object contain flags to determine whether its new or updated, or should this be done with some type of link registry?

    In MVC what should my link and menu models look like?

    Thanks for any input


    PHP Code:

    // Link Model

    class Link{
            
        var 
    $id;
        var 
    $domain_id;
        var 
    $page_id;
        var 
    $parent_id;
        var 
    $status;
        var 
    $stacking_order;
        var 
    $header;
        var    
    $style;
        var    
    $target;
        var    
    $scheme;
        var    
    $name;
        var    
    $url;
        
        var 
    $modifiedFlag null;
        var 
    $newFlag null;
            
        function 
    Link($input){
            
            static 
    $index=0;
            
    $index++;
            
            
    $this->id $input['id'];
            
            if(!
    $input['id']){
                
    $this->id $index;
                
    $this->newFlag 1;
            }
            
            
    $this->page_id             $input['page_id'];
            
    $this->domain_id         $input['domain_id'];
            
    $this->parent_id         $input['parent_id'];
            
    $this->status             $input['status'];
            
    $this->stacking_order     $input['stacking_order'];
            
    $this->header             $input['header'];
            
    $this->style             $input['style'];
            
    $this->target             $input['target'];
            
    $this->scheme             $input['scheme'];
            
    $this->name             $input['name'];
            
    $this->url                 $input['url'];
        }
        
        
        
    // setters
        
    function setID($id){
            
    $this->id $id;
        }
        function 
    setPageID($id){
            
    $this->modifiedFlag 1;
            
    $this->page_id $id;
        }
        function 
    setParentID($id){
            
    $this->modifiedFlag 1;
            
    $this->parent_id $id;
        }
        function 
    setStatus($status){
            
    $this->modifiedFlag 1;
            
    $this->status $status;
        }
        function 
    setStackingOrder($number){
            
    $this->modifiedFlag 1;
            
    $this->stacking_order $number;
        }    
        function 
    setHeader($header){
            
    $this->modifiedFlag 1;
            
    $this->header $header;
        }    
        function 
    setUrl($url){
            
    $this->modifiedFlag 1;
            
    $this->url $url;
        }    
        function 
    setStyle($style){
            
    $this->modifiedFlag 1;
            
    $this->style $style;
        }    
        function 
    setTarget($target){
            
    $this->modifiedFlag 1;
            
    $this->target $target;
        }    
        function 
    setScheme($scheme){
            
    $this->modifiedFlag 1;
            
    $this->scheme $scheme;
        }    
        function 
    setName($name){
            
    $this->modifiedFlag 1;
            
    $this->name =& $name;
        }

        
    // getters
        
    function getID(){
            return 
    $this->id;
        }
        function 
    getName(){
            return 
    $this->name;
        }
        function 
    getPageID(){
            return 
    $this->page_id;
        }
        function 
    getParentID(){
            return 
    $this->parent_id;
        }
        function 
    getStatus(){
            return 
    $this->status;
        }
        function 
    getStackingOrder(){
            return 
    $this->stacking_order;
        }
        function 
    checkForHeader(){
            return 
    $this->header;
        }
        function 
    getUrl(){
            return 
    $this->url;
        }    
        function 
    getStyle(){
            return 
    $this->style;
        }    
        function 
    getTarget(){
            return 
    $this->target;
        }    
        function 
    getScheme(){
            return 
    $this->scheme;
        }    

        function 
    getConstructedLink()
        {
            
    $ahref  "<a ";
            if(
    $this->style)
                
    $ahref .= "class=\"".$this->style."\" ";
            
    $ahref .= "href=\"";
            if(
    $this->scheme)    
                
    $ahref .= $this->scheme."://";
            else     
                
    $ahref .= "http://";
            
    $ahref .= $this->url."\" ";
            if(
    $this->target)
                
    $ahref .= "target=\"".$this->target."\">";
            else 
                
    $ahref .= "target=\"_self\">";
            
    $ahref .= $this->name;
            
    $ahref .= "</a>";
            
            return 
    $ahref;
        }
        
        function 
    isModified()
        {
            return 
    $this->modifiedFlag;    
        }
        
        function 
    isNew()
        {
            return 
    $this->newFlag;    
        }
        
        function 
    create()
        {
            
    $sql  "INSERT INTO links ";
            
    $sql .= "(id, domain_id, page_id, parent_id, status, stacking_order, header, style, target, scheme, name, url) ";
            
    $sql .= "VALUES ('', '$this->domain_id', '$this->page_id', '$this->parent_id', '$this->status', '$this->stacking_order', '$this->header', '$this->style', '$this->target', '$this->scheme', '$this->name', '$this->url')";
            return 
    $sql;
        }
        
        function 
    update()
        {
            
    $sql  "UPDATE links SET ";
            
    $sql .= "domain_id='$this->domain_id', ";
            
    $sql .= "page_id='$this->page_id', ";
            
    $sql .= "parent_id='$this->parent_id', ";
            
    $sql .= "status='$this->status', ";
            
    $sql .= "stacking_order='$this->stacking_order', ";
            
    $sql .= "header='$this->header', ";
            
    $sql .= "style='$this->style', ";
            
    $sql .= "target='$this->target', ";
            
    $sql .= "scheme='$this->scheme', ";
            
    $sql .= "name='$this->name', ";
            
    $sql .= "url='$this->url' ";
            
    $sql .= "WHERE id='$this->id'";
            return 
    $sql;
        }
        
        function 
    delete()
        {
            
    $sql  "DELETE FROM links WHERE id='$this->id'";
            return 
    $sql;
        }
        
        function 
    commit($sql)
        {
            echo 
    $sql."\n";
            
    $this->modifiedFlag null;
            
    $this->newFlag null;
        }
    }


    // Menu Model

    class Menu{
        var 
    $menuLinks = array();
        
        function 
    Menu(){}
        
        function 
    getLinks()
        {
            return 
    $this->menuLinks;
        }
        
        function 
    addToMenu($link){
            
    $this->menuLinks[] = $link;
        }
        
        function 
    findById($id)
        {
            foreach(
    $this->menuLinks as $index=>$link)
            {
                if(
    $link->getID() == $id)
                    return 
    $this->menuLinks[$index];
            }
        }
        
        function 
    findByName($name)
        {
            foreach(
    $this->menuLinks as $index=>$link)
            {
                if(
    $link->getName() == $name)
                    return 
    $this->menuLinks[$index];
            }
        }
        
        function 
    deleteFromMenu($link_id){
            foreach(
    $this->menuLinks as $id=>$link)
            {
                if(
    $link_id == $link->getID())
                {
                    unset(
    $this->menuLinks[$id]);
                }
            }
        }
        
        function 
    commit()
        {
            foreach(
    $this->menuLinks as $link)
            {
                if(
    $link->isModified())
                    
    $link->commit($link->update());
                if(
    $link->isNew())
                    
    $link->commit($link->create());
            }        
        }
        
    }


    // USAGE

    $leftMenu = & new Menu;

    $q mysql_query('SELECT * FROM links');

    while(
    $result mysql_fetch_array($q))
    {
        
    $leftMenu->addToMenu( new Link($result) );
    }

    // edit some links
    $a = &$leftMenu->findById(2);
    $a->setName('a new link');

    $b = &$leftMenu->findByName('Link 1');
    $b->setPageID(25);
    $b->setName('Revised link 1');

    // add a new link
    $myLink = array('domain_id'=>1
                    
    'page_id'=>1
                    
    'parent_id'=>3,
                    
    'location'=>'left',
                    
    'name'=>'test link',
                    
    'url'=>'test.html',
                    
    'style'=>null,
                    
    'target'=>null,
                    
    'scheme'=>null,
                    
    'status'=>1,
                    
    'stacking_order'=>1
                    
    'header'=>false );
                        
    $leftMenu->addToMenu(new Link($myLink));

    // commit changes and creates to database
    $leftMenu->commit(); 

  2. #2
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once a menu is created, how often does it get updated? Do different users have the ability to create/manage different menus (e.g. portal customization)? It looks like the Link object is handling both data access and data presentation (e.g. building the HTML output). If it were a true "model" (in MVC terms) it would not include any information on how to construct HTML from the link data (e.g. the model has no knowledge of the view). Instead, it would expose the link data in some well-defined matter and a "view" would be responsible for rendering the HTML.

    JT

  3. #3
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once a menu is created, how often does it get updated?
    That depends on the user

    Do different users have the ability to create/manage different menus (e.g. portal customization)?
    Yes

    This is for a cms, and basically what im trying to do is give the user the ability to create or modify a menu in "memory" via sessions, so they can preview the menu before committing it to a database.

    Since the object here is an html link, should it not return an html link with one of its methods. I understand what you're saying about having the view create html, but in this case it seems logical that since the object itself represents html that it should return html. Then again, I might be wrong.

    Thanks for your input

  4. #4
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it ok for an object that is basically a representation of a database table row to return the sql for creating/editing/deleting itself from the database? It seems right to me, but I don't know.

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeShank
    Is it ok for an object that is basically a representation of a database table row to return the sql for creating/editing/deleting itself from the database? It seems right to me, but I don't know.
    Yes. What you are describing is a pattern called Row Data Gateway (PoEAA). A Row Data Gateway is simply an object that represents a row in a database table. It has methods to access the data that it contains as well as methods to add, update, and delete. If the Link object you are referring to is going to be a Row Data Gateway, then it shouldn't contain HTML or any information about presentation. A Row Data Gateway is very low-level (only one step above the actual database). It only knows about one database table and that's it.

    JT

  6. #6
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would the link object be considered Active Record according to (PoEAA)?

    Active Record
    An Object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data

    would Link::getConstructedLink() be adding domain logic on the data?

  7. #7
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeShank
    Would the link object be considered Active Record according to (PoEAA)?

    Active Record
    An Object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data

    would Link::getConstructedLink() be adding domain logic on the data?
    I wouldn't consider getConstructedLink domain logic. I would consider it presentation logic.

    JT

  8. #8
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    In cases like this, within a View layer I INCLUDE a file which holds the actual HTML it's self, and replace embedded tags (within the HTML) where you need to put the data.

    This way you are not mixing layers. It looks like in some cases though it may be over the top, as for example, you only INCLUDE for an A HREF link, but it works so why not huh ?

  9. #9
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, i was gonna say sometimes it feels like overkill, but I guess the real point is the separation.

  10. #10
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Widow Maker


    In cases like this, within a View layer I INCLUDE a file which holds the actual HTML it's self, and replace embedded tags (within the HTML) where you need to put the data.

    This way you are not mixing layers. It looks like in some cases though it may be over the top, as for example, you only INCLUDE for an A HREF link, but it works so why not huh ?
    Widow Maker,

    I'm having trouble seeing how this is a view layer. Are you referring specfically to the getConstructedLink code or the Link class in general? IMHO, if it contains SQL, it's not the view layer.

    JT

  11. #11
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IMHO, if it contains SQL, it's not the view layer.
    Yes, I agree with you on this point I'm not at my own box just now so I can't post any script, though I have posted it before in this forum.

    I wasn't referring specifically to the
    PHP Code:
    function getConstructedLink() {
    ...
    ...

    It's self, though to the method that I use to seperate concerns ? On the example of the above class method, to return the A HREF with it's links, etc without mixing layering, I would need to keep the bare bones A HREF as a seperate file, and INCLUDE it on each iteration required - one or more links required - and replace given tags with required data for each iteration

    Hope this makes more sense


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
  •