SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    From database to templates

    Hello,

    This is the first time I post in this forum and I'd like to thank everybody for the threads about MVC, DAO, PHP templates and the like... It has been a great help for me! I have been programming in PHP for about one year and with these advices I'm changing my way of scripting.

    So, with what I've read I have many question and here is the first one :

    I'm curently working on a standard way to transform database results into an array that would be easy to work with in templates.
    Here's a simple example :

    2 simple tables :
    Code:
    categories (id, name)
    articles   (id, name, cat_id)
    A view that joins the two tables

    Code:
    CREATE VIEW v_articles AS
    SELECT
     cat.id   AS cat_id,
     cat.name AS cat_name,
     art.id   AS art_id,
     art.name AS art_name
    FROM
     categories cat 
      LEFT OUTER JOIN
     articles art
      ON (cat.id = art.cat_id)
    ORDER BY
     cat_name, art_name;
    A sample result for select * from v_articles :
    Code:
    |cat_id | cat_name | art_id | art_name |
    +-------+----------+--------+----------+
    |1      |'letters' | 1      | 'a'      |
    |1      |'letters' | 2      | 'b'      |
    |2      |'numbers' | 3      | '1'      |
    |2      |'numbers' | 4      | '2'      |
    |2      |'numbers' | 5      | '3'      |
    |3      |'special' | 6      | '('      |
    |3      |'special' | 7      | '}'      |
    |4      |'other'   | NULL   | NULL     |
    +-------+----------+--------+----------+
    Here is my problem, I'd like to write a function that takes this result and convert it in an array that could be easily used by templates

    PHP Code:
    function break_results ($result_set$a_breaking
    usage example :

    PHP Code:
    $a_breaking = array('cat_id''cat_name','articles' => array('art_id''art_name') );
    $categories break_results($res$a_breaking); 
    and the categories array will be

    PHP Code:
    array (
      
    => array ( 'cat_id'   => 1,
                   
    'cat_name' => 'letters'
                   
    'articles' => array ( => array ('art_id' => 1'art_name' => 'a'),
                                         
    => array ('art_id' => 2'art_name' => 'b') ) ),

      
    => array ( 'cat_id'   => 2,
                   
    'cat_name' => 'numbers'
                   
    'articles' => array ( => array ('art_id' => 3'art_name' => '1'),
                                         
    => array ('art_id' => 4'art_name' => '2'),
                                         
    => array ('art_id' => 5'art_name' => '3') ) ),

      
    => array ( 'cat_id'   => 3,
                   
    'cat_name' => 'special'
                   
    'articles' => array ( => array ('art_id' => 6'art_name' => '('),
                                         
    => array ('art_id' => 7'art_name' => '}') ) ),

      
    => array ( 'cat_id'   => 4,
                   
    'cat_name' => 'other',
                   
    'articles' => null )
    ); 
    This way I can easily work with this array in my templates

    PHP Code:
    <?php if ( ! is_array($categories) ) : /* no category */ ?>

    <H2>No category</H2>

    <?php else : /* at least one category */ ?>

    <H2>List of existing categories</H2>

    <?php   foreach ( $categories as $category ) : /* loop through categories */ ?>

    <H3><?php echo ($category['name']) ?></H3>

    <?php     if ( ! is_array($category['articles'] ) ) : /* no article in the category */ ?>

    <H4>No article in this category</H4>

    <?php     else : /* at least one article */ ?>
    <ul>

    <?php       foreach ( $category['articles'] as $article ) : /* loop through articles */ ?>
      <li><?php echo ($article['name'?>
    <?php       
    endforeach; /* end of loop through articles */ ?>

    </ul>

    <?php     endif; /* end of article block */ ?>

    <?php   endforeach; /* end loop through categories */ ?>

    <?php endif; /* end of category block */ ?>
    this is a simple example but my function call could be more complex :
    PHP Code:
    $a_breaking 
    array(
    'cat_id',
            
    'cat_name',
            
    'admins'  => array('adm_id',
                               
    'adm_name'),
            
    'articles' => array('art_id',
                                
    'art_name',
                                
    'users' => array('usr_id',
                                                 
    'usr_name'))); 
    Where admins may be people allowed to admin a category and users people who can view articles...

    I'm currently working on it and think this may be done using classes from the Eclipse Library like RowLoopManipulator, RowLoopManipulatorWatcher and QueryIterator, but I'm a bit puzzled for now...

    So if you have already written such function or have a better way to solve this problem I'm waiting for your opinion.

    Thanx for reading... and sorry for my english

    --c0y0t
    Last edited by c0y0t; Sep 29, 2003 at 06:47.

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [Off Topic]

    Near the end of your thread, you are assigning user(s) to an article yes ?

    Shouldn't that be the orther way around is the way I see it ? ...

    So called ''Real Life'' situ. As to your thread's question, have you read about DAOs ? To most folks who frequent this forum this'd be that the database resultset(s) are stored in an array as Objects whereby you'd then just need to use an Iterator to access each row ?

    Suggest you do a search for DAO and MVC first to see if you can gleam anything from the results to give you a better idea of what I mean with DAOs ?

  3. #3
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Near the end of your thread, you are assigning user(s) to an article yes ?
    It was a silly example...

    but if we have the following tables :

    Code:
    categories (id, name)
    articles (id, name, cat_id)
    users (id, name)
    cat_admins (cat_id, usr_id) //people who can admin a category
    art_viewers (art_id, usr_id) //people who can view an article
    and the following view that joins everything :

    Code:
    create view v_categories_and_people AS
    SELECT
      cat.id AS cat_id,
      cat.name AS cat_name,
      art.id as art_id
      adm.id as adm_id,
      adm.name as adm_name
      usr.id as usr_id,
      usr.name as usr_name
    FROM
      articles art INNER JOIN categories cat ON (art.cat_id = cat.id)
      INNER JOIN cat_admins ON (cat.id = cat_admin.cat_id)
      INNER JOIN users adm ON (adm.id = cat_admin.usr_id)
      INNER JOIN art_viewers ON (art.id = art_viewers.art_id)
      INNER JOIN users usr ON (usr.id = art_viewers.usr_id);
    If I want to show articles sorted by category and for each category its administrators and for each article people that can see it I use the following query :

    Code:
    SELECT * from v_categories_and_people
    ORDER BY cat_name, art_name, adm_name, usr_name;
    this way I can render such view :

    Code:
    o Furnitures
       + Administrators of furnitures :
          - Harry
          - Mike
       + Articles in furnitures :
          - Pen
              * Users that can see Pen :
                   % Bob
                   % Toto
          - Paper       
              * Users that can see Paper :
                   % Mary
                   % Toto
    You see, that's why I need to assign users to articles, and admin users to categories

    Shouldn't that be the orther way around is the way I see it ? ..
    I'm sorry but I don't get what you mean

    So called ''Real Life'' situ. As to your thread's question, have you read about DAOs ?
    What i'm doing isn't a DAO? Do you mean I have to make a DAO for categories, a DAO for articles, a DAO for users...
    That means requesting the database one time for categories, x times to list admins in each category, x times for articles in each categories and x time to list users of each article? I'm saying this because that's what I saw in such thread :
    http://www.sitepointforums.com/showp...04&postcount=2

    To most folks who frequent this forum this'd be that the database resultset(s) are stored in an array as Objects whereby you'd then just need to use an Iterator to access each row ?
    What will be the benefit if I use objects instead of my *big* array?
    - arrays are good in output from my DAO because I can easily manipulate them in my template (cf example in post#1)
    - arrays are good in input because that's the way they come from forms ( $_REQUEST )

    Maybe am I in the wrong way...

    Suggest you do a search for DAO and MVC first to see if you can gleam anything from the results to give you a better idea of what I mean with DAOs ?
    Yes, i'll keep on reading and anderstand what i'm missing... thanks for the reply

    --cOyOt

  4. #4
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's go back to my problem. I took my first example (articles and categories, or book and authors or whatever you like ...), a 1 to n* relashionship )

    I wrote this little script that works :

    PHP Code:
    error_reporting (E_ALL);

    require (
    'eclipse-3_1/RowLoopManipulator.php');
    require (
    'eclipse-3_1/Loop.php');

    class 
    Articles_by_category extends RowLoopManipulator
    {
      var 
    $a_result;

      function 
    Articles_by_category(&$a_result)
      {
        
    parent::RowLoopManipulator();
        
    $cat_watcher =& $this->addWatcher('cat_id');
        
    $cat_watcher->register('new_category');
        
    $this->a_result =& $a_result;
      }

      function 
    new_category(&$row$index)
      {
        
    $this->a_result[$row['cat_id']] = array('cat_id'   => $row['cat_id'],
                                                
    'cat_name' => $row['cat_name'],
                                                
    'articles' => null);
      }

      function 
    current(&$row$index)
      {
        
    parent::current($row$index);

        
    //if there is no article we can skip
        
    if ( $row['art_id'] == null )
        {
          return;
        }

        
    //if no article has been registered for this category creates an empty array
        
    if ( $this->a_result[$row['cat_id']]['articles'] == null )
        {
          
    $this->a_result[$row['cat_id']]['articles'] = array();
        }

        
    //add the current article to the category
         
    $this->a_result[$row['cat_id']]['articles'][$row['art_id']] = array('art_id'   => $row['art_id'],
                                                                             
    'art_name' => $row['art_name']);
      }

    I use it like this :

    PHP Code:
    $a_input = array (
      array(
    'cat_id' => 1'cat_name' =>'letters''art_id'=>1'art_name'=>'a'),
      array(
    'cat_id' => 1'cat_name' =>'letters''art_id'=>2'art_name'=>'b'),
      array(
    'cat_id' => 2'cat_name' =>'numbers''art_id'=>3'art_name'=>'1'),
      array(
    'cat_id' => 2'cat_name' =>'numbers''art_id'=>4'art_name'=>'2'),
      array(
    'cat_id' => 2'cat_name' =>'numbers''art_id'=>5'art_name'=>'3'),
      array(
    'cat_id' => 3'cat_name' =>'special''art_id'=>6'art_name'=>'('),
      array(
    'cat_id' => 3'cat_name' =>'special''art_id'=>7'art_name'=>'}'),
      array(
    'cat_id' => 4'cat_name' =>'other'  'art_id'=>null'art_name'=>null) );

    $categories = array();

    $art = new Articles_by_category(&$categories);

    Loop::run(new ArrayIterator($a_input), $art);

    include (
    'template.php'); //the template in post #1 
    What do you think about it?
    Of course it can be used with a QueryIterator on a QueryResult.

    Now I need to write something more general, any suggestion?

    thanks

    --c0y0t

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry but I don't get what you mean
    Doesn't matter; Your explamation makes more sense now.

    On the matter of DAOs, and your question of creating a seperate DAO for users, articles etc etc.

    What you have at the moment IMO is a series of database queries ? A DAO is slightly more as I see it though to your question you proberly do need a seperate DAO for

    Users
    Articles
    Categories

    For example, here is a peice of script I'm working on right now in fact to give you a better idea ?

    I think [going by views] you're using PostgreSQL ? Yet to use the database properly so I use mySQL for the time being

    Roles:

    PHP Code:
    # Model Layer

    class rolesModel 
            var 
    $ObjDao;
            var 
    $BoolResult;
            
            function 
    rolesModel(& $ObjDb) {
                
    $this -> ObjDao = & new rolesDao(& $ObjDb);
                
    $this -> BoolResult false;
            }
            
            function 
    fetch() {
                return 
    $this -> BoolResult;
            }
            
            function 
    capture($key) {
                
    # query database ...
                
    $rs $this -> ObjDao -> searchAllByKey($key);
                
    #
                
    if($rs -> rowCount() >= (int) 1) { // one or more roles
                    
    $this -> BoolResult true;
                    
    # process database results
                    
    $this -> ObjDao -> process($rs);
                    

                    
    return $this -> ObjDao -> fetch();
                }
            }
        }

    # Dao Layer

    class rolesDao 
            var 
    $ObjDb;
            var 
    $ArrayArchive;
            
            function 
    rolesDao(& $ObjDb) {
                
    $this -> ObjDb = & $ObjDb
            }
            
            function 
    fetch() {
                return 
    $this -> ArrayArchive;
            }
            
            function 
    process($resultset) {
                
    $this -> ArrayArchive = array();
                
    $iterator = & new queryIterator($resultset);
                
    #
                
    while($row $iterator -> getCurrent()) {
                    
    $iterator -> getNext();
                    
    #
                    
    $archive = & new rolesArchive;
                
                    
    $archive -> setId($row['roleId']);
                    
    $archive -> setName($row['roleName']);
                
                    
    $this -> ArrayArchive[] = & $archive;
                }
            }
            
            function 
    searchAllByKey($key) {
                
    # search roles only by user PK
                
    $sql "SELECT 
                    roles.roleId AS roleId, roles.roleName AS roleName, 
                    userroles.userId, userroles.roleId FROM 
                    roles, userroles WHERE 
                    userroles.userId = '"
    .$key."' AND 
                    userroles.roleId = roles.roleId ORDER BY roles.roleId"
    ;
        
                return 
    $this -> ObjDb -> fetch($sql);
            }
        }
        
        class 
    rolesArchive {
            var 
    $ArrayArchive;
            
            function 
    rolesArchive() {
                
    $this -> ArrayArchive = array();
            }
            
            function 
    setId($id) { $this -> ArrayArchive['id'] = $id; }
            function 
    setName($name) { $this -> ArrayArchive['name'] = $name; }
            
    #
            
    function getId() { return $this -> ArrayArchive['id']; }
            function 
    getName() { return $this -> ArrayArchive['name']; }
        } 
    Now notice that I put each row pulled from the database into an array ?

    ie Look at class rolesArchive for more details ?

    This way as I look at it is how a DAO works so I only need to use an iterator to access the data ?

    To access the data note I no longer need to refer to the name of a database tables field yes ? This is just one example, other members do their DAOs differently, some implement the MODEL Layer in with the DAO;

    I like to keep the 2 seperate for example... If you've only got the one row to pull from the database then you can refer to the data by it's object instead thus no need for an iterator ? ...

    User:

    PHP Code:

    # Model Layer

    class LogInModel {
            var 
    $ObjDao;
            var 
    $BoolResult;
            
            function 
    LogInModel(& $ObjDb) {
                
    $this -> ObjDao = & new LogInDao(& $ObjDb);
                
    $this -> BoolResult false;
            }
            
            function 
    fetch() {
                return 
    $this -> BoolResult;
            }
            
            function 
    validate($username$password) { 
                
    $rs $this -> ObjDao -> searchAllByEncryptedText($username$password); 
                
                
    #
                
    if($rs -> rowCount() == (int) 1) {
                    
    $this -> BoolResult true;
                    
    #
                    
    $this -> ObjDao -> process($rs);
                    
                    return 
    $this -> ObjDao -> fetch();
                }
            }
        }

    # Dao Layer

    class LogInDao {
            var 
    $ObjDb;
            var 
    $ObjArchive// this is an object; as apposed to an array yes ?
            
            
    function LogInDao(& $ObjDb) {
                
    $this -> ObjDb = & $ObjDb
            }
            
            function 
    fetch() {
                return 
    $this -> ObjArchive;
            }
            
            function 
    process($resultset) {
                
    $archive = & new LogInArchive;
                
    #
                # coyot -- notice how I get the one row ? No need to use an
                # iterator to access it
                
                
    $row $resultset -> getRow();
                
                
    $archive -> setId($row['logInId']);
                
    $archive -> setStatus($row['logInStatus']);
                
    $archive -> setSession($row['logInSession']);
                
    $archive -> setUsername($row['logInUsername']);
                
    $archive -> setPassword($row['logInPassword']);
                
                
    $this -> ObjArchive = & $archive;
            }
            
            function 
    searchAllByEncryptedText($user$pass) {
                
    $sql "SELECT logInId,
                    logInStatus,
                    logInSession,
                    logInUsername,
                    logInPassword 
                    FROM login WHERE logInUsername = '"
    .$user."' 
                    AND logInPassword = PASSWORD('"
    .$pass."')";
        
                return 
    $this -> ObjDb -> fetch($sql);
            }
        }
        
        class 
    LogInArchive 
            var 
    $ArrayArchive;
            
            function 
    logInArchive() {
                
    $this -> ArrayArchive = array();
            }
            
            function 
    setId($id) { $this -> ArrayArchive['id'] = $id; }
            function 
    setStatus($status) { $this -> ArrayArchive['status'] = $status; }
            function 
    setSession($sess) { $this -> ArrayArchive['session'] = $sess; }
            function 
    setUsername($user) { $this -> ArrayArchive['username'] = $user; }
            function 
    setPassword($pass) { $this -> ArrayArchive['password'] = $pass; }
            
    #
            
    function getId() { return $this -> ArrayArchive['id']; }
            function 
    getStatus() { return $this -> ArrayArchive['status']; }
            function 
    getSession() { return $this -> ArrayArchive['session']; }
            function 
    getUsername() { return $this -> ArrayArchive['username']; }
            function 
    getPassword() { return $this -> ArrayArchive['password']; }
        } 
    Hope this helps you ? If you need more help I can see what I can do...


  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I can understand this part properly...

    PHP Code:
    $categories = array(); 

    $art = new Articles_by_category(&$categories); 

    Loop::run(new ArrayIterator($a_input), $art); 
    1) The Category array is merely a method for the template to extract the data ?
    2) The object reference $art is a container for the array used by the template yes ?
    3) By this I mean than when you use Loop::run(..), the contents of the $a_input are put into the array $category ??

    Before I burn more brain cells can you correct me if I've got the above wrong in any way ?

    Cheers...

  7. #7
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks doctor
    I will read your replies later at home after work (it's 7pm here)

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok

  9. #9
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    So I can understand this part properly...

    PHP Code:
    $categories = array(); 

    $art = new Articles_by_category(&$categories); 

    Loop::run(new ArrayIterator($a_input), $art); 
    1) The Category array is merely a method for the template to extract the data ?
    It is an array holding the data to be parsed by the template. See my first post for the template.

    2) The object reference $art is a container for the array used by the template yes ?
    In fact it is a RowLoopManipulator making it possible to transform raw data coming from the database into something more convenient to manipulate in templates
    again :
    PHP Code:
    array ('cat_id' => 1'cat_name' => 'fruits''item_id' => 1'item_name' => 'apple');
    array (
    'cat_id' => 1'cat_name' => 'fruits''item_id' => 2'item_name' => 'pear');
    array (
    'cat_id' => 2'cat_name' => 'vegetables''item_id' => 3'item_name' => 'potatoe'); 
    becomes

    PHP Code:
    array (
      array(
    'cat_id'   => 1,
            
    'cat_name' => 'fruits',
            
    'items'    => array (array( 'item_id'   => 1,
                                        
    'item_name' => 'apple'),
                                 array( 
    'item_id'   => 2,
                                        
    'item_name' => 'pear')))
      array(
    'cat_id'   => 2,
            
    'cat_name' => 'vegetables',
            
    'items'    => array (array( 'item_id'   => 3,
                                        
    'item_name' => 'apple')))); 
    3) By this I mean than when you use Loop::run(..), the contents of the $a_input are put into the array $category ??
    Yes, in another form

    Cheers
    Last edited by c0y0t; Sep 29, 2003 at 13:35.

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    array holding the data to be parsed
    Kind of what I was meaning; Sorry, I should've used more presice wording... As for the other points it's obvious I'm way off...

    Umm... Anyway looking at this part you've posted...

    PHP Code:
    array ('cat_id' => 1'cat_name' => 'fruits''item_id' => 1'item_name' => 'apple'); 
    array (
    'cat_id' => 1'cat_name' => 'fruits''item_id' => 2'item_name' => 'pear'); 
    array (
    'cat_id' => 2'cat_name' => 'vegetables''item_id' => 3'item_name' => 'potatoe'); 
    becomes

    PHP Code:
    array ( 
      array(
    'cat_id'   => 1
            
    'cat_name' => 'fruits'
            
    'items'    => array (array( 'item_id'   => 1
                                        
    'item_name' => 'apple'), 
                                 array( 
    'item_id'   => 2
                                        
    'item_name' => 'pear'))) 
      array(
    'cat_id'   => 2
            
    'cat_name' => 'vegetables'
            
    'items'    => array (array( 'item_id'   => 3
                                        
    'item_name' => 'apple')))); 
    I think then you need to change the first array to the second array to be used by the template ?

    The data originally is pulled from one or more related tables which kind of prevents you using the first array ?

    Umm... Guessing again here, does the Articles_by_category re-arrange the data as required ?

  11. #11
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another way to solve the problem is to change the template to make it possible to process raw (and redundant) data :
    We send to the template a QueryIterator, for example. I can't get the eclipse api so there may be mistakes.

    (Not tested)

    PHP Code:
    <?php if ( ! $it->isValid() ) : /* no category */ ?>

    <H2>No category</H2>

    <?php else : /* at least one category */ ?>

    <H2>List of existing categories</H2>

    <?php   while ( $it->isValid() && ($row $it->getCurrent()) && ($cat_id $row['cat_id']) ) : /* loop through categories */ ?>

    <H3><?php echo ($row['cat_name']) ?></H3>

    <?php     if ( ($row['art_id'] == null) || ($it->next()) ) : /* no article in the category */ ?>

    <H4>No article in this category</H4>

    <?php     else : /* at least one article */ ?>
    <ul>

    <?php       do : /* loop through articles */ ?>
      <li><?php echo ($row['art_name'?>
    <?php       
    while ( $it->next && $it->isValid() && ($cat_id == $row['cat_id']) ); /* end of loop through articles */ ?>

    </ul>

    <?php     endif; /* end of article block */ ?>

    <?php   endforeach; /* end loop through categories */ ?>

    <?php endif; /* end of category block */ ?>
    But when we want to keep templates the simplest possible... here it becomes a bit complex.

    Dr, I'm going to reply to your posts... in a few minues

  12. #12
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was thinking of an Iterator myself; Been looking over at the Java site on this languages Iterators -

    Though you could use a query iterator you'd need to encapsulate the field names as I've done in the script I've posted tonight.

    On the matter of keeping Templates as simple as possible I agree Never easy though.

  13. #13
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    As for the other points it's obvious I'm way off...
    lol

    Umm... Anyway looking at this part you've posted...
    I think then you need to change the first array to the second array to be used by the template ?
    Yes, that's it...
    The data originally is pulled from one or more related tables which kind of prevents you using the first array ?
    It is easier to manage the second one because there is no redundancy through categories. Just compare the template in the first post with the above one...
    Umm... Guessing again here, does the Articles_by_category re-arrange the data as required ?
    Yes it does but I wrote it for a specific case and needed a function that works on more complicated queries (with 4 or 5 breaking points).
    But I think I will adopt the above template style, it seems more efficient... and I wouldn't have to write this **** function

    BTW, you, how do you render data?

  14. #14
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    I think [going by views] you're using PostgreSQL ? Yet to use the database properly so I use mySQL for the time being
    Don't wait anymore

    Ok, looking at your code :
    (1) In the "action" script you call the _capture_ method from the rolesModel class
    (2) The rolesModel class calls the _searchAllByKey_ Method of the rolesDAO class
    (3) The rolesModel class calls the process Method of the rolesDAO class passing the resultset in params
    (4) The rolesDao class fills an array with rolesArchive Objects
    (5) The rolesModel class gets this array of rolesArchive by calling the rolesDao process method
    (6) eventually in the action script you get the array of rolesArchive that ... you pass to the view?

    So why don't you call directly the searchAllByKey method of the roles DAO object? This method could directly return the rolesArchive array...

    Regarding the Archive class I thought about something simpler a few days ago (tired of writing all set and get methods :

    Not tested...

    PHP Code:
    class Archive
    {
      var 
    $_a_elements;

      function 
    Archive($a_keys)
      {
        if ( ! 
    is_array($a_keys) )
        {
          
    trigger_error ('blahblah'E_USER_EROR);
        }
        foreach (
    $a_keys as $key)
        {
          
    $this->_a_elements[$key] = null;
        }
      }

      function 
    exists($key)
      {
        return 
    array_key_exists($this->_a_elements$key);
      }

      function 
    get($key)
      {
        if ( ! 
    $this->exists($key) )
        {
          
    trigger_error ('blahblah'E_USER_EROR);
        }
        return 
    $this->_a_elements[$key];
      }

      function 
    set ($key$val)
      {
        if ( ! 
    $this->exists($key) )
        {
          
    trigger_error ('blahblah'E_USER_EROR);
        }
        
    $this->a_elements[$key] = $val    
      
    }

    and your rolesArchive class becomes :
    PHP Code:
    class rolesArchive {
        var 
    $ArrayArchive;
        
        function 
    rolesArchive() {
            
    parent::Archive('id''name');
        }

    or the loginArchive one :
    PHP Code:
    class LogInArchive 
        function 
    logInArchive() {
            
    parent::Archive('id''status''session''username''password');
        }

    What do you think about this?

    instead of
    PHP Code:
    $archive = & new rolesArchive;
    $archive -> setId($row['roleId']);
    $archive -> setName($row['roleName']); 
    it will be
    PHP Code:
    $archive = & new rolesArchive;
    $archive -> set ('id',   $row['roleId']);
    $archive -> set ('name'$row['roleName']); 
    And regarding Model and DAO I would rather make one class for both.
    Just because I don't get why I would separate them... if you can tell me why...

    To access the data note I no longer need to refer to the name of a database tables field yes ?
    Yes! I see tow reasons using the Archive class :
    - the one you give above
    - access data through objects instead of arrays : $roles->get('id') instead of $roles['id']
    - is there anonther reason?

    This is just one example, other members do their DAOs differently, some implement the MODEL Layer in with the DAO;
    I like to keep the 2 seperate for example... If you've only got the one row to pull from the database then you can refer to the data by it's object instead thus no need for an iterator ? ...
    I don't see the need of separate Model and DAO to do this. You can make your DAO return either an Archive array (getAll()) or an Archive object (getById($id))

    Last question, why is there a question mark at the end of most of your sentences

    Cheers

  15. #15
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do you render data?
    What do you mean ? Like, output it to the web page for example ? ...

    A sample please ?

    PHP Code:
    # part of some controller layer
    .
    .
    $this -> ObjDb = & $ObjDb;
                
    $this -> ArrayPckge = array();
                
    #
                
    $action $ObjAction['PAGE_ACTION']; 
                
    # redirect if user is not allowed to carry out this action
                
    $ObjAuth -> isAllowed($action);
                
                
    $view $action.'View';
                
    $this -> ObjView = new $view($ObjAuth -> fetchRoles(), $ObjAction['PAGE_TITLE']);
    .
    .

    # part of a view layer
    .
    .
    $secBase = new tableWidget($dom);
                
    $secBase -> addAttribute('width''224');
                
    $secBase -> addAttribute('align''left');
                
                
    $secBaseTr = new trWidget($dom);
                
    $secBaseTd = new tdWidget($dom);
                
    $secBaseTd -> addAttribute('width''100%');
                
    $secBaseTd -> addAttribute('align''center');
                
    $secBaseTd -> addAttribute('height'CORE_CELL_HEIGHT);
                
    $secBaseTd -> addAttribute('valign''center');
                
    $secBaseTd -> addNode('Administration Options');
                
    $secBaseTr -> addNode($secBaseTd -> fetch());
                
    $secBase -> addNode($secBaseTr -> fetch());
                
    #
                
    $secBaseTr = new trWidget($dom);
                
    $secBaseTd = new tdWidget($dom);
                
    $secBaseTd -> addAttribute('width''100%');
                
    $secBaseTd -> addAttribute('height'CORE_CELL_HEIGHT);
                
    $secBaseTd -> addNode('');
                
    $secBaseTr -> addNode($secBaseTd -> fetch());
                
    $secBase -> addNode($secBaseTr -> fetch());
                
                
    $secBaseTr = new trWidget($dom);
                
    $secBaseTd = new tdWidget($dom);
                
    $secBaseTd -> addAttribute('width''100%');
                
    $secBaseTd -> addAttribute('align''center');
                
    $secBaseTd -> addAttribute('height'CORE_CELL_HEIGHT);
                
    $secBaseTd -> addAttribute('valign''center');
                
    $secBaseTd -> addNode('Roles');
                
    $secBaseTr -> addNode($secBaseTd -> fetch());
                
    $secBase -> addNode($secBaseTr -> fetch());
                
    #
                
    $iterator = & new arrayIterator($roles);
                
                while(
    $row $iterator -> getCurrent()) {
                    
    $iterator -> getNext();
                    
    #
                    
    $secBaseTr = new trWidget($dom);
                    
    $secBaseTd = new tdWidget($dom);
                    
    $secBaseTd -> addAttribute('width''100%');
                    
    $secBaseTd -> addAttribute('align''center');
                    
    $secBaseTd -> addAttribute('height'CORE_CELL_HEIGHT);
                    
    $secBaseTd -> addAttribute('valign''center');
                    
    $secBaseTd -> addNode($row -> getName());
                    
    $secBaseTr -> addNode($secBaseTd -> fetch());
                    
    $secBase -> addNode($secBaseTr -> fetch());
                }
                
    #
                
    $secBaseTr = new trWidget($dom);
                
    $secBaseTd = new tdWidget($dom);
                
    $secBaseTd -> addAttribute('width''100%');
                
    $secBaseTd -> addAttribute('height'CORE_CELL_HEIGHT);
                
    $secBaseTd -> addNode('');
                
    $secBaseTr -> addNode($secBaseTd -> fetch());
                
    $secBase -> addNode($secBaseTr -> fetch());
                
                
    $secBaseTr = new trWidget($dom);
                
    $secBaseTd = new tdWidget($dom);
                
    $secBaseTd -> addAttribute('width''100%');
                
    $secBaseTd -> addAttribute('align''center');
                
    $secBaseTd -> addAttribute('height'CORE_CELL_HEIGHT);
                
    $secBaseTd -> addAttribute('valign''center');
                
    $secBaseTd -> addNode('Priveleges');
                
    $secBaseTr -> addNode($secBaseTd -> fetch());
                
    $secBase -> addNode($secBaseTr -> fetch());
                
    $parentNode -> addNode($secBase -> fetch());
    .

    Hope I've understood your question ?

  16. #16
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't wait anymore
    Yer, I know I'm actually realising the limitations of mySQL [as it is at the moment - 4.1.x] when compared to PostgreSQL and Oracle for example ?

    I do have a Windows build of PostgreSQL which I had running under Win2000Pro until it crashed

    Downloaded Oracle but that bombed due to some corruption and I wasn't happy either as it was a 528Mb download... 2 1/2 hours over broadband btw.

    I am seriously thinking of dumping mySQL very soon though

    ... you pass to the view?
    Yes... Going to look over your points you've noted and look at your snippet of script ?

    Maybe have a few questions for you myself then...

    Hope the script I've posted just now has helped ?

  17. #17
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very quickly...

    (1) In the "action" script you call the _capture_ method from the rolesModel class
    The capture() method is executed by a controller layer yes ? See below...

    PHP Code:
    class authoriseUser // a Controller Layer [user role & permissions based authentication]
            
    var $ObjRoles;
            var 
    $ArrayPerms;
            
            function 
    authoriseUser(& $ObjDb$IntKey) {
                
    $roles = & new rolesModel(& $ObjDb);
                
    $this -> ObjRoles $roles -> capture($IntKey);
                

    Though I'm still working on that; What I'd really like to do is have it so that all the required objects needed by a view are encapsulated [packaged] all ready for the view by another class ?

    This other class would be independent of either the controller or view layers then ? This I believe would simplify the passing of objects [data] to/from the controller and view...

    Still thinking on how best it can be done though

    -- EDIT --

    ... call directly the searchAllByKey method of the roles DAO object?
    From the View ... ?

  18. #18
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    What do you mean ? Like, output it to the web page for example ? ...
    Yeah that's it

    A sample please ?
    yes pliiiiiiiiizzz

    PHP Code:
    # part of some controller layer 
    The controler will be the next step so I skip this part at the moment

    PHP Code:
    # part of a view layer
    $secBase = new tableWidget($dom);
                
    $secBase -> addAttribute('width''224');
                
    $secBase -> addAttribute('align''left');
                
                
    $secBaseTr = new trWidget($dom);
                
    $secBaseTd = new tdWidget($dom); 
    Wow, do you want the "web designer" in my entreprise to kill me
    Mean, why did you chose to "render", uuh , output web pages this way?
    Do you output every page like this? no, silly question... Only tables?
    As far as I am concerned I like to write web pages in HTML...
    I did a bit of java and I'm fed up with things like GridBagLayout... Although with the time it can become very powerful...

    Hope I've understood your question ?
    hopefully you have
    thanks the quick reply

  19. #19
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just because I don't get why I would separate them... if you can tell me why...
    And

    - is there anonther reason?
    Phew... This is getting to be some thread huh ?

    Okay - As I see it you need to keep your Model Layer and your Database Access [ie a DAO Layer] seperate in the event that you need to change your database yes ?

    If designed properly - your DAOs that is - then you can migrate your application to another database simply by scripting a series of new DAOs as required to reproduce the SQLs required for your new database...

    Hope I'm making some sense since MVC is a difficult subject to approach, for me anyways.

    As for the other reason as you may have noticed is that DAOs allow far easier access to your data.

    Now, looking at your other parts of the script you've posted the reasoning behind you using arrays seams sound enough to me ?

    I'd think you'd best to continue with this idea as it is since I'm thinking your more happier working with arrays for the moment than objects.

    Umm... Why do I leave a question mark at the end of most sentences ?

    I'm always looking for answers I suppose.

  20. #20
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, do you want the "web designer" in my entreprise to kill me
    Mean, why did you chose to "render", uuh , output web pages this way?


    I do not like the idea of using Smarty type template engines and I don't like the idea either of embedding PHP [no matter how limited] within HTML...

    Though it's not just TABLEs... It's the whole page; Yes The whole page...

    I just like the idea of how you can build a page like this, swapping this 'element' for that 'element' etc etc.

    I think it's great anyways.

  21. #21
    SitePoint Member
    Join Date
    Sep 2003
    Location
    France, Orléans
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Very quickly...
    The capture() method is executed by a controller layer yes ? See below...
    yes, that's what I called the "action script". Because, at the moment, I've decided to take page controlers ( /users/login.php, /users/logout.php, /categories/list.php ...)
    Though I'm still working on that; What I'd really like to do is have it so that all the required objects needed by a view are encapsulated [packaged] all ready for the view by another class ?
    This other class would be independent of either the controller or view layers then ? This I believe would simplify the passing of objects [data] to/from the controller and view...
    Don't get what you mean, sorry... too late...

    Still thinking on how best it can be done though
    Yes, thinking, thinking... Ouch my brain!

    call directly the searchAllByKey method of the roles DAO object?
    From the View ... ?
    I meant from the controler...

    Bye for now, must mork tomorrow!

  22. #22
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yer, I need to grab something to eat and then catch up on some serious sleep...

  23. #23
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very, very quickly....

    I meant from the controler...
    As this would break the MVC Layers yes ?

    The DAO doesn't belong in the Controller Layer...


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
  •