developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Ticket #1419: jDbPDOResultSet.fetchAll.patch

File jDbPDOResultSet.fetchAll.patch, 2.9 KB (added by Vincentv, 10 years ago)
  • jelix/db/jDbPDOResultSet.class.php

     
    1818 */
    1919class jDbPDOResultSet extends PDOStatement {
    2020
    21     const FETCH_CLASS = 8;
    22 
    2321    protected $_fetchMode = 0;
    2422
    25     public function fetch ($fetch_style = PDO::FETCH_BOTH, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) {
    26         $rec = parent::fetch();
     23    public function fetch ($fetch_style = null, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) {
     24        // we take a shortcut: unused parameters are ignored by parent::fetch
     25        // let the parent::setFetchMode override as needed, and PHP use its default
     26        if ($fetch_style) {
     27            $rec = parent::fetch($fetch_style, $cursor_orientation, $cursor_offset);
     28        }
     29        else {
     30            $rec = parent::fetch();
     31        }
     32
    2733        if ($rec && count($this->modifier)) {
    2834            foreach($this->modifier as $m)
    2935                call_user_func_array($m, array($rec, $this));
     
    3238    }
    3339
    3440    /**
    35      * return all results from the statement.
    36      * Arguments are ignored. JDb don't care about it (fetch always as classes or objects)
    37      * But there are here because of the compatibility of internal methods of PDOStatement
     41     * return all results from the statement
    3842     * @param integer $fetch_style ignored
    3943     * @param integer $column_index
    4044     * @param array $ctor_arg  (ignored)
    4145     * @return array list of object which contain all rows
    4246     */
    43     public function fetchAll ($fetch_style = PDO::FETCH_OBJ, $column_index=0, $ctor_arg=null) {
    44         if ($this->_fetchMode) {
    45             if ($this->_fetchMode != PDO::FETCH_COLUMN)
    46                 return parent::fetchAll($this->_fetchMode);
    47             else
    48                 return parent::fetchAll($this->_fetchMode, $column_index);
     47    public function fetchAll ($fetch_style = null, $fetch_argument=null, $ctor_arg=null) {
     48        // if the user requested to override the style set with setFetchMode, use it
     49        $final_style = ($fetch_style ? $fetch_style : $this->_fetchMode);
     50
     51        // Check how many arguments, if available should be given
     52        if (!$final_style) {
     53            $records = parent::fetchAll();
    4954        }
     55        else if ($ctor_arg) {
     56            $records = parent::fetchAll($final_style, $fetch_argument, $ctor_arg);
     57        }
     58        else if ($fetch_argument) {
     59            $records = parent::fetchAll($final_style, $fetch_argument);
     60        }
    5061        else {
    51             return parent::fetchAll(PDO::FETCH_OBJ);
     62            $records = parent::fetchAll($final_style);
    5263        }
     64
     65        foreach ($records as $key => $record)
     66            foreach ($this->modifier as $modif)
     67                call_user_func_array($m, array($rec, $this));
     68        return $records;
    5369    }