SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 44
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question A Pagination Class on a DAO System

    Hello,

    I have a Animals DAO class that I'd like to apply a pagination class to it.

    Between this two classes, there will be a composition relation (more precisely, an association one). My question is:

    Is a Pagination that "has a" Animal. OR Is a Animal that "has a" pagination?
    Should we create on Class Pagination a property named $_animal OR, should we create on class Animal a property named $_pagination?

    I'm inclined to accept the second one, since, if I put the property $_animal on my Pagination class, I will end up, on the Pagination Class, with so many properties as pagination objects... (_animals, _news, _vets...) :s

    Please note that neither of those options mentioned could also be a possibility. I'm just trying to figure out, how to properly relate a DAO and a Pagination Class.
    Would a Decorator Pattern be a third hypothesis here?



    Any help please?
    Márcio
    Last edited by oikram; Jul 19, 2009 at 07:18.

  2. #2
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    Is a Pagination that "has a" Animal. OR Is a Animal that "has a" pagination? Should we create on Class Pagination a property named $_animal OR, should we create on class Animal a property named $_pagination?
    Uh... neither? Pagination and Animals really don't have anything to do with each other. Rather than coupling these two unrelated classes, I would suggest making your Pagination class generic, so that it can work with any DAO-- or even arrays.

    My suggestion would be to figure out what methods you need in order to paginate something. getItems($offset,$size) and getNumItems() would be a good start. Then, because your DAOs don't implement this interface, you'll need a wrapper class to translate between the DAO interface and the Pagination interface.

    Eventually, should look something like this:

    PHP Code:
    $dao = new AnimalDAO($db);
    $pager = new Pagination(new Pagination_Adapter_Dao($dao)); 

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you'll need a wrapper class to translate between the DAO interface and the Pagination interface.
    I really have no clue about what a wrapper class is, what id does and how to implement it.

    But ok. I will forget that Association Relation.

    Is a nonsense to to apply a Decorator Pattern between the pagination class and the specific DAO class?


    Regards,
    Márcio

  4. #4
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    Is a nonsense to to apply a Decorator Pattern between the pagination class and the specific DAO class?
    Wrapper = Decorator Pattern.

    PHP Code:
    class Pagination_Adapter_Dao {

       public function 
    __construct($dao) {
          
    $this->dao $dao;
       }

       public function 
    getItems($offset$limit) {
          return 
    $this->dao->findAll($offset$limit);
       }

       public function 
    getNumItems() {
          return 
    $this->dao->countAll();
       }


    Also, if you want to see a full featured example of this, check out the "Skeleton Pagination" link in my signature. Feel free to use the code, if it works for you.

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    Wrapper = Decorator Pattern.
    I'm sooo newbie.



    Ok. So, I will now read:
    http://www.devshed.com/c/a/PHP/An-In...tern-with-PHP/

    Quote Originally Posted by allspiritseve View Post
    Also, if you want to see a full featured example of this, check out the "Skeleton Pagination" link in my signature. Feel free to use the code, if it works for you.
    Hmm... I have. But it seems "wow hight level" for me right now.

    Anyway, thanks a lot for the info, in the next few days I will try to produce some code, and put it here for consideration.


    Thanks a lot,
    Márcio

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One last question before I dig in:

    Once it's done, could I apply this pagination class on a generic DAO crud class, hence, avoiding apply this class on each DAO ?

    Regards,
    Márcio

  7. #7
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean not having to use the wrapper? As long as your generic DAO class has the correct methods, you won't need the wrapper. I find it useful though, as I don't find the Pagination methods particularly expressive on a DAO interface.

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ow... so either we use a wrapper or we use a general DAO Crud class ?
    If we want to use a wrapper, should we have a DAO per table ?

    Regards,
    Márcio

  9. #9
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use a wrapper with a generic DAO class. The wrapper (or Decorator) only needs to be used if the interface for your datasource (DAO, in this case) is not the same as the interface the Pagination class uses. Think of it as translating the DAO into something the Pagination class can understand.

  10. #10
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was thinking on a pagination class that would be in the middle.
    between the General DAO Class and the User Interface. (a.k.a viewer?).

    The base idea was quite simple: Create a pagination class that will have:
    a) independent visual customization;
    b) pagination business logic on the pagination class.
    c) Some informations needed for the pagination logic to work, should come from DAO classes.

    So I was thinking on:
    Creating a pagination class logic on a pagination class, that will be partially consumed by DAO values.
    Add a Decorator Object to that class that will deal with the generation of links and the formating of those links.

    But seems that I'm not having a good planning hm?

    Gash, now I'm lost! :s You don't need to reply if you believe you need to teach me all a word on this.
    I need to clarify this...


    Regards,
    Márcio

  11. #11
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you're just getting caught up in the terminology. I can isolate three classes from what you just posted.

    A view class (handles displaying the Pagination):
    Quote Originally Posted by oikram View Post
    a) independent visual customization;
    A pagination class (handles pagination logic)
    Quote Originally Posted by oikram View Post
    b) pagination business logic on the pagination class.
    A datasource (handles data)
    Quote Originally Posted by oikram View Post
    General DAO Class
    Now, the wrapper I was discussing just translates the methods on the DAO so the pagination class can understand them. You should end up with layers of classes like this:

    View > Pagination > [wrapper] > DAO.

  12. #12
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks. that helps clear things a little around here.

    With or without wrapper, I will give it a try.

    Regards,
    Márcio

  13. #13
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again,

    I have this on animalsPDO.class.php:

    PHP Code:
    listAnimals()
    {
      try
      {
        
    $stmt $this->_dbh->prepare(SELECT FROM animals LIMIT :limit OFFSET :offset);
        
    $stmt->bindParam(':limit'WhatShouldIPutHere?, PDO::PARAM_INT);
        
    $stmt->bindParam(':offset'WhatShouldIPutHere?, PDO::PARAM_INT);

        
    $stmt->execute();
    }
    catch ...
    ...

      }

    What should we add the limits and offset values on this method ?

    I believe we cannot grab them directly from a pagination object right, like $mypaginationObject->getLimit() and $mypaginationObject->getOffset() ?

    Should we have on the DAO two aditional methods: setLimit and setOffset ?

    But this forces me to have Limit and Offset properties on my DAO classes, so this couldn't be an answer neither.


    OR, now it's the time to use a decorator class, that will be a pagination class that will access a DAO method to return the offset and limit, and, without touching the DAO, play with that methods and use them to calculate the pagination stuff ?

    If this last hypothesis is true, my question will be:
    What kind of method should we have on our DAO to return the offset and limit values ? Does this method correspond to the findAll() applied on your examples above?


    Regards,
    Márcio

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    PHP Code:
    listAnimals($limit,$offset)

    {

      try

      {

        
    $stmt $this->_dbh->prepare(SELECT FROM animals LIMIT :limit,:offset);

        
    $stmt->bindParam(':limit',$limitPDO::PARAM_INT);

        
    $stmt->bindParam(':offset',$offsetPDO::PARAM_INT);



        
    $stmt->execute();

    }

    catch ...

    ...



      }



  15. #15
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    You could do something like this. I would personally though factor each one out into separate methods. The problem there though is repetition. This is one of the problems you run into when using a DAO.

    PHP Code:
    listAnimals($limit=null,$offset=null) {

        
    $sql 'SELECT * FROM animals';
        
        if(!
    is_null($limit) && !is_null($offset)) {
        
            
    $stmt $this->_dbh->prepare($sql.' LIMIT :limit,:offset');
            
    $stmt->bindParam(':limit',$limitPDO::PARAM_INT);
               
    $stmt->bindParam(':offset',$offsetPDO::PARAM_INT);        
        
        } else if(!
    is_null($limit)) {
        
            
    $stmt $this->_dbh->prepare($sql.' LIMIT :limit');
            
    $stmt->bindParam(':limit',$limitPDO::PARAM_INT);
        
        } else {
        
            
    $stmt $this->_dbh->prepare($sql);
        
        }


    Where as, if you were using a generic Mapper or ActiveRecord:

    PHP Code:
    Animal::find();
    Animal::find(array('limit'=>10);
    Animal::find(array('limit'=>10,'offset'=>10)); 

    PHP Code:
    $animal->get();
    $animal->get(array('limit'=>10));
    $animal->get(array('limit'=>10,'offset'=>10)); 
    Eliminating all forms of repetition regardless of whether there is a offset, limit, etc.

    This is how I handle pagination and the associated query.

    PHP Code:
    /*
    * counts total animals returns integer
    */
    $total Animal::count();

    /*
    * Create pagination instance that handle calculations based on number per page, total and current page. If the current page
    * exceeds the number of pages the current page becomes the last page.
    */
    $page 1;
    $perPage 20;
    $pagination = new Pagination($perPage,$total,$page);

    /*
    * embed the offset and limit in query
    */
    $animals Animal::find(array('limit'=>$pagination->getCount(),'offset'=>$pagination->getOffset())); 
    The pagination instance is then passed to the view and two methods can be called to build the control interface. This gets a bit complex, but the substring {page} will be replaced by the page number of the associated control. So the variable $route is the base url. The makeMenu and makeDropDown methods use that url and replace {page} with actual page for the control… if that makes sense.

    The call to makeUrl() of the $path variable replaces the current page variable named path with {page}. So if the current page url was: /animals/1 and the second item was the page the url returned would be: /animals/{page}.

    PHP Code:
    $route $path->makeUrl(array('page'=>'{page}'));

    echo 
    $pagination->makeMenu($route);
    echo 
    $pagination->makeDropDown($route,'pagination-drop-down'); 

  16. #16
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi oddz,

    I know that we must follow the DRY the much we can. I will...

    But, let's say that, for now, I don't mind about repeating myself.
    Because, it's also a way to keep some concepts on our head and, since I have no more then several months on PHP and programming in general, it will not hurt. :-)

    Another factor is the moral factor. I'm trying to do this pagination for months now. I really need to see it working asap.

    So, if I look to the DAO example you have provided, I see that the $limit and the $offset will be treated as params of a listAnimals method.

    Two questions dought:
    The $limit and $offset, where will they come from? From the same page where we instantiate the pagination?

    Something like:
    PHP Code:
    $animal = new AnimalsDAO();
    $pagination = new Pagination();
    $animal-> listAnimal($drop_down_limit_per_page$pagination->getOffset); 
    ?

    But where will the decorator pattern be applied ?

    I it's a too dummy question, rest assured that I will try to clarify some things tomorrow and post again. :S


    Regards,
    Márcio

  17. #17
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    $limit and offset would be calculated before passing them as arguments to listAnimal().

    Calculating the limit and offset is outside of the DAO responsibilities. Thus, why I use a separate separate Pagination class responsible to calculating the offset and limit based on three parameters.

    1. number of listings per page
    2. total number of listings
    3. current page


    This is all the info needed to calculate the appropriate limit and offset to be embedded into a select statement.

    Putting that aside the physical algorithm to calculate a offset and limit is main responsibility of the Pagination class within my application. I'm sure you can search for a algorithm to accomplish that one your own. However, if you can't I could share my own with you just to give you an idea of what is involved.

    Quote Originally Posted by oikram
    But where will the decorator pattern be applied ?
    Any attempt to use a decorator would be a hack unless the AnimalsDAO class implements a interface. A decorator implies that classes are interchangable. There isn't a concrete way to assure that relationship unless both share the same interface.

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    All the non essential view related stuff has been stripped. You can play around with this and see how it works.

    PHP Code:
    <?php
    class Pagination {
        
        
    // total number of records per page
        
    private $_count;
        
        
    // offset from beginning
        
    private $_offset;
        
        
    // current page
        
    private $_page;
        
        
    // total number of records with pagination
        
    private $_total;
        
        public function 
    __construct($pCount,$pTotal,$pPage) {
            
    $this->_count $pCount?$pCount:10;
            
    $this->_total $pTotal;
            
    $this->_page $pPage?$pPage:1;
            
    $this->_currentPage();
            
    $this->_currentOffset();
        }
        
        private function 
    _currentOffset() {
            
            
    $page $this->_page;
            
    $this->_offset $this->_page==1?0:$this->_count*(--$page);
            
        }
        
        private function 
    _currentPage() {
            
            
    // send user to last page if they have gone past the total number of pages in the path variable
            
    if($this->_page>$this->pages()) {
                
    $this->_page $this->pages();
            }
            
        }
        
        
    // number of pages
        
    public function pages() {
            
    /*
             * if the count excedes the the total number than set 
             * the default to 1 page. There are less records than
             * will fit on one whole page.
             */
             
             
    return $this->_count>=$this->_total?1:ceil($this->_total/$this->_count);
        }
        
        public function 
    getOffset() {
            return 
    $this->_offset;
        }
        
        public function 
    getCount() {
            return 
    $this->_count;
        }
        
        public function 
    getPage() {
            return 
    $this->_page;
        }
        
        public function 
    getTotal() {
            return 
    $this->_total;
        }
        
    }

    $perPage = isset($_GET['record'])?$_GET['record']:10;
    $total = isset($_GET['total'])?$_GET['total']:300;
    $page = isset($_GET['page'])?$_GET['page']:1;

    $pagination = new Pagination($perPage,$total,$page);

    echo 
    '<ul>';
    echo 
    '<li><strong>offset:</strong>&nbsp;',$pagination->getOffset(),'</li>';
    echo 
    '<li><strong>limit:</strong>&nbsp;',$pagination->getCount(),'</li>';
    echo 
    '</ul>';

  19. #19
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your time and patience, so:

    Quote Originally Posted by oddz View Post
    1. number of listings per page
    2. total number of listings
    3. current page


    This is all the info needed to calculate the appropriate limit and offset to be embedded into a select statement.
    Yes. Please see if I'm correct on this workflow:

    1)
    The number of listings per page, will come from:
    a) by default from the pagination class.
    b) and also the viewer page, since the user could also choose what will the limit be.

    to:
    The pagination class for the offset calculation.
    And to the :limit clause on our DAO SQL Query.



    2)
    The Total number of listings:
    Will come from our DAO count method.

    And should go to the pagination class to be used for the offset calculation.



    3)
    And finally, the current page number, will come from a properly verified $_GET['page'] query string value. So I would say that:
    The $_GET['page'] number will come from our viewer page.

    to:
    a) be properly verified on our pagination class and
    b) also used on the $offset calculation.


    4)
    At the end, the pagination class should calculate the offset, and return that offset to be used on our sql query as offset on the DAO.

    Is this workflow well viewed?

    What is still not clear to me is, how can we properly:
    a) pass the count values from our DAO to our pagination (for the offset calculatation) and,
    b) pass the offset calculated on our pagination to be used on listAnimal() method on our DAO?


    Regards,
    M&#225;rcio
    Last edited by oikram; Jul 28, 2009 at 03:48. Reason: update the last question

  20. #20
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please siteforum gurus. I'm so stuck.
    Please throw me anything. Maybe it moves.

  21. #21
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Conceptually:

    PHP Code:
    /*
    * Create instance of DAO
    */
    $dao AnimalsDAO;

    /*
    * retreive the total number of animals
    */
    $total $dao->count();

    /*
    * Set the number of listings per page.
    * This could either be hard coded into the 
    * application or be user controlled via a getable
    * variable. Either one would work. In this case
    * though it is hard coded as 10 items per page.
    */
    $perPage 10

    /*
    * Get the page from the $_GET array of the page. If
    * the 'page' key doesn't exists or is null default to the first
    * page.
    */
    $page = isset($_GET['page'])?$_GET['page']:1;

    /*
    * Calculate the offset based on the number fo items
    * per page, total and current page number. In this case
    * a instance of pagination will be created that encapsulates
    * this responsibility into a single object.
    */
    $pagination = new Pagination($perPage,$total,$page);

    /* 
    * Pass the calculated 1.) limit and 2.) offset as an 
    /* argument to the listAnimals() method of the DAO.
    */
    $animals $dao->listAnimals($pagination->getCount(),$pagination->getOffset()); 
    Then your listAnimals method would probably end up looking something like the below.

    PHP Code:
    listAnimals($limit=null,$offset=null) {

        
    $sql 'SELECT * FROM animals';
        
        if(!
    is_null($limit) && !is_null($offset)) {
        
            
    $stmt $this->_dbh->prepare($sql.' LIMIT :limit,:offset');
            
    $stmt->bindParam(':limit',$limitPDO::PARAM_INT);
               
    $stmt->bindParam(':offset',$offsetPDO::PARAM_INT);        
        
        } else if(!
    is_null($limit)) {
        
            
    $stmt $this->_dbh->prepare($sql.' LIMIT :limit');
            
    $stmt->bindParam(':limit',$limitPDO::PARAM_INT);
        
        } else {
        
            
    $stmt $this->_dbh->prepare($sql);
        
        }


    The count method may be implemented in a similar fashion to the below.

    PHP Code:

    count
    () {

        
    $sql 'SELECT count(*) FROM animals';

        
    // ... run query and return integer result



  22. #22
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... I've been studying since your post two days ago... let's see how dummy I am :-)

    The use of conditionals on listAnimals methods seems a very nice Idea. I was thinking on creating on method to list with LIMIT, another method to list without LIMIT, another method to list with LIMIT and WHERE, another method to list only with WHERE... I'm newbie. PERIOD.
    So, thanks a lot for that snipped, it helps on solving the issue and more.

    Please take a look on this first sketch of a pagination (helper?) class:
    (I have 7 or 8 class examples on my lap, I'm trying to understand and implement. this one is based on phpro pagination class here:
    http://www.phpro.org/tutorials/Pagin...P-and-PDO.html

    Here's the sketch:
    PHP Code:
    <?php


    class Pagination {
        
        
    //If I use getter, I must define properties here I suppose?...
        
    private $limit;
        private 
    $offet;
        
        public static function 
    Pagination ($total_records$limit$page)
        
        {
            
            
    $total_pages ceil($total_records $limit);
            
            
    //certifies that $page could have any value greater or equal to 1
            
    $page max($page1);
            
    //certifies that the same $page could have any value greater or equal to the value defined on $total_pages
            
    $page min($page$total_pages);
            
            
    $offset = ($page -1) * $limit;
            
            
    //We can save the values on a object of stdClass...
            //but what are the benefits? Can anyone explain me this?
            //I mean, since we are inside a class, why do we need to
            //create a new object of another class inside this one and save
            //the param values there? Couldn't we just put those values as properties of this Pagination class?
            
            //Let's instanciate the class on $pagi_obj:
            
    $pagi_obj= new stdClass;
            
            
    //let's reference the values obtained on stdClass properties:
            
    $pagi_obj->total_pages $total_pages;
            
    $pagi_obj->offset $offset;
            
    $pagi_obj->limit $limit;
            
    $pagi_obj->page;
            
            
    //When we instantiante Pagination, we want to return this object...
            
    return $pagi_obj;
            
        }  
        
        
    //do we need this methods here to be called later on:
        //$animals = $dao->listAnimals($pagination->getLimit(), $pagination->getOffset());
        //or can we simple do:
        //$animals = $dao->listAnimals($pagination::$limit, $pagination::$offset);
        //eliminating the need of this two methods ?
        
        
    public function getLimit()
        {
            
    //this will never work. If I have no class property called limit.
            
    return $this->limit;    
        }
        
        public function 
    getOffset()
        {
            
    //this will never work. If I have no class property called offset.
            
    return $this->offset;
        }
        
    }
    //end of class
    In the comments I have my questions, but, for better reading here they are again:

    Why stdClass ?
    what are the benefits? Can anyone explain me this?
    I mean, since we are inside a class, why do we need to create a new object of another class inside this one and save the param values there? Couldn't we just put those values as properties of this Pagination class?

    Do we really need getters?
    Do we need this methods here to be called later on:

    PHP Code:
    $animals $dao->listAnimals($pagination->getLimit(), $pagination->getOffset()); 
    Or can we simple do:
    PHP Code:
    $animals $dao->listAnimals($pagination::$limit$pagination::$offset); 
    eliminating the need of this two methods ?


    Thanks a lot for helping me on this,
    Regards,
    Márcio

  23. #23
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by oddz View Post


    $stmt = $this->_dbh->prepare($sql.' LIMIT :limit,ffset');
    $stmt->bindParam(':limit',$limit, PDO::PARAM_INT);
    $stmt->bindParam('ffset',$offset, PDO::PARAM_INT);

    } else if(!is_null($limit)) {

    $stmt = $this->_dbh->prepare($sql.' LIMIT :limit');
    $stmt->bindParam(':limit',$limit, PDO::PARAM_INT);

    } else {

    $stmt = $this->_dbh->prepare($sql);

    }

    }
    [/php]
    Off Topic:


    Just wondering: why you're using prepared statements on a query which will only be executed once. Should be faster (and more concise code) just to use query();

  24. #24
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm newbie, so my knowledge is by any standards confidant. But I was thinking of using prepare statements not because of performance, but to prepare all the statements that may come from the user.
    *Never trust user input* golden rule.

    So, I give myself a simple rule, if there are params to be passed on a SQL Query that come from the user, I want them to be prepared before executed.

    Those are my thoughts...

    Regards,
    Márcio

    ps- my doubts on previous post still applies.

  25. #25
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Quote Originally Posted by oikram
    The use of conditionals on listAnimals methods seems a very nice Idea. I was thinking on creating on method to list with LIMIT, another method to list without LIMIT, another method to list with LIMIT and WHERE, another method to list only with WHERE... I'm newbie. PERIOD.
    So, thanks a lot for that snipped, it helps on solving the issue and more.
    That would be ideal using a DAO. However, integrating it into the same method avoids repetition. Something that I'm sure you have come to know well using a DAO.

    Quote Originally Posted by oikram
    I'm newbie, so my knowledge is by any standards confidant. But I was thinking of using prepare statements not because of performance, but to prepare all the statements that may come from the user.
    *Never trust user input* golden rule.
    The limit and offset are not user controlled. They are each calculated using user controlled variables, but as separate values themselves they are not user controlled. With that said, you could embed them directly, but the micro second advantage if that is hardly worth it in my opinion. Stick to what you know for now. If binding them makes life easier then bind them. Worry about efficiency where it truly matters not by micro managing these little things.

    Quote Originally Posted by oikram
    Do we really need getters?
    Do we need this methods here to be called later on:
    No need to getter and setters because Pagination is not being used as a object and class vars are not needed. Instead your calling a static method that returns a standard object formatted with the appropriate pagination info.

    PHP Code:
    $p Pagination::Pagination(3000,20,1);
    $p->offset
    In the end the listAnimals call would look like:

    PHP Code:
    $dao->listAnimals($p->limit,$p->offset); 


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
  •