Exemple #1
0
 /**
  * Paginate and return result
  *
  * @param Table $table
  * @return array
  */
 public function paginate(Table $table)
 {
     $ands = [];
     foreach ($this->sqlAnds as $filter => $ors) {
         $ands[] = '(' . join(') OR (', $ors) . ')';
     }
     $where = '';
     if (strlen($this->initialWhere) > 0) {
         $where = ' WHERE (' . $this->initialWhere . ')';
         if (count($ands)) {
             $where .= ' AND (' . join(') AND (', $ands) . ')';
         }
     } else {
         if (count($ands)) {
             $where = ' WHERE (' + join(') AND (', $ands) + ')';
         }
     }
     $preparedParams = [];
     foreach ($this->sqlParams as $name => $value) {
         if ($value instanceof \DateTime) {
             $preparedParams[$name] = $value->format('Y-m-d H:i:s');
         } else {
             $preparedParams[$name] = $value;
         }
     }
     $db = $this->getPdo();
     $mapper = $table->getMapper();
     if (!$mapper) {
         throw new \Exception("Data 'mapper' is required when using PDOAdapter");
     }
     $sql = "SELECT COUNT(*) AS count" . "  FROM " . $this->initialFrom . " " . $where . " ";
     $sth = $db->prepare($sql);
     $sth->execute($preparedParams);
     $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
     $count = $result[0]['count'];
     $table->calculatePageParams($count);
     if ($this->sqlOrderBy) {
         $where .= ' ORDER BY ' . $this->sqlOrderBy . ' ';
     }
     if ($table->getPageSize() > 0) {
         $where .= ' LIMIT ' . $table->getPageSize() . ' ';
         $where .= ' OFFSET ' . $table->getPageSize() * ($table->getPageNumber() - 1) . ' ';
     }
     $sql = "SELECT " . $this->initialSelect . " " . "  FROM " . $this->initialFrom . " " . $where . " ";
     $sth = $db->prepare($sql);
     $sth->execute($preparedParams);
     $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
     $data = [];
     for ($i = 0; $i < count($result); $i++) {
         $data[] = $mapper($result[$i]);
     }
     return $data;
 }
 /**
  * Paginate and return result
  *
  * @param Table $table
  * @return array
  */
 public function paginate(Table $table)
 {
     $ands = [];
     foreach ($this->sqlAnds as $filter => $ors) {
         $ands[] = '(' . join(') OR (', $ors) . ')';
     }
     $where = '';
     if (strlen($this->initialWhere) > 0) {
         $where = ' WHERE (' . $this->initialWhere . ')';
         if (count($ands)) {
             $where .= ' AND (' . join(') AND (', $ands) . ')';
         }
     } else {
         if (count($ands)) {
             $where = ' WHERE (' . join(') AND (', $ands) . ')';
         }
     }
     $preparedParams = [];
     foreach ($this->sqlParams as $name => $value) {
         if ($value instanceof \DateTime) {
             $preparedParams[$name] = $value->format('Y-m-d H:i:s');
         } else {
             $preparedParams[$name] = $value;
         }
     }
     $db = $this->getPdo();
     $mapper = $table->getMapper();
     if (!$mapper) {
         throw new \Exception("Data 'mapper' is required when using PDOAdapter");
     }
     $sql = "SELECT COUNT(*) AS count" . "  FROM " . $this->initialFrom . " " . $where . " ";
     $sth = $db->prepare($sql);
     $sth->execute($preparedParams);
     $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
     $count = $result[0]['count'];
     $table->calculatePageParams($count);
     if ($this->sqlOrderBy) {
         $where .= ' ORDER BY ' . $this->sqlOrderBy . ' ';
     }
     if ($table->getPageSize() > 0) {
         $where .= ' LIMIT ' . $table->getPageSize() . ' ';
         $where .= ' OFFSET ' . $table->getPageSize() * ($table->getPageNumber() - 1) . ' ';
     }
     $sql = "SELECT " . $this->initialSelect . " " . "  FROM " . $this->initialFrom . " " . $where . " ";
     $sth = $db->prepare($sql);
     $sth->execute($preparedParams);
     $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
     $data = [];
     foreach ($result as $row) {
         foreach ($table->getColumns() as $columnId => $columnParams) {
             $value = $row[$columnId];
             if ($value === null) {
                 continue;
             }
             if ($columnParams['type'] == Table::TYPE_DATETIME) {
                 if (is_string($value)) {
                     if ($this->getDbTimezone()) {
                         $dt = new \DateTime($value, new \DateTimeZone($this->getDbTimezone()));
                     } else {
                         $dt = new \DateTime($value);
                     }
                 } else {
                     if (is_int($value)) {
                         $dt = new \DateTime('@' . $value);
                     } else {
                         $dt = $value;
                     }
                 }
                 if (date_default_timezone_get()) {
                     $dt->setTimezone(new \DateTimeZone(date_default_timezone_get()));
                 }
                 $row[$columnId] = $dt;
             }
         }
         $data[] = $mapper ? $mapper($row) : $row;
     }
     return $data;
 }
 /**
  * Paginate and return result
  *
  * @param Table $table
  * @return array
  */
 public function paginate(Table $table)
 {
     $table->calculatePageParams(count($this->data));
     if ($table->getPageSize() > 0) {
         $offset = $table->getPageSize() * ($table->getPageNumber() - 1);
         $length = $table->getPageSize();
         $data = array_slice($this->data, $offset, $length);
     } else {
         $data = $this->data;
     }
     $mapper = $table->getMapper();
     $result = [];
     foreach ($data as $row) {
         foreach ($table->getColumns() as $columnId => $columnParams) {
             $value = $row[$columnId];
             if ($value === null) {
                 continue;
             }
             if ($columnParams['type'] == Table::TYPE_DATETIME) {
                 if (is_string($value)) {
                     if ($this->getDbTimezone()) {
                         $dt = new \DateTime($value, new \DateTimeZone($this->getDbTimezone()));
                     } else {
                         $dt = new \DateTime($value);
                     }
                 } else {
                     if (is_int($value)) {
                         $dt = new \DateTime('@' . $value);
                     } else {
                         $dt = $value;
                     }
                 }
                 if (date_default_timezone_get()) {
                     $dt->setTimezone(new \DateTimeZone(date_default_timezone_get()));
                 }
                 $row[$columnId] = $dt;
             }
         }
         $result[] = $mapper ? $mapper($row) : $row;
     }
     return $result;
 }
 /**
  * Paginate and return result
  *
  * @param Table $table
  * @return array
  */
 public function paginate(Table $table)
 {
     $query = $this->getQueryBuilder()->getQuery();
     $paginator = new Paginator($query);
     $table->calculatePageParams(count($paginator));
     if ($table->getPageSize() > 0) {
         $paginator->getQuery()->setFirstResult($table->getPageSize() * ($table->getPageNumber() - 1))->setMaxResults($table->getPageSize());
     }
     $mapper = $table->getMapper();
     if (!$mapper) {
         throw new \Exception("Data 'mapper' is required when using DoctrineAdapter");
     }
     $result = [];
     foreach ($paginator as $row) {
         $result[] = $mapper($row);
     }
     return $result;
 }
 /**
  * Sort data
  *
  * @param Table $table
  * @return GenericDBAdapter
  */
 public function sort(Table $table)
 {
     $column = $table->getSortColumn();
     $dir = $table->getSortDir();
     if (!$column) {
         return;
     }
     $sqlId = null;
     foreach ($table->getColumns() as $id => $params) {
         if ($id == $column) {
             $sqlId = $params['sql_id'];
             break;
         }
     }
     if (!$sqlId) {
         throw new \Exception("No 'sql_id' for column: {$column}");
     }
     $this->sqlOrderBy = $sqlId . " " . $dir;
     return $this;
 }
 /**
  * Paginate and return result
  *
  * @param Table $table
  * @return array
  */
 public function paginate(Table $table)
 {
     $table->calculatePageParams(count($this->data));
     if ($table->getPageSize() > 0) {
         $offset = $table->getPageSize() * ($table->getPageNumber() - 1);
         $length = $table->getPageSize();
         $data = array_slice($this->data, $offset, $length);
     } else {
         $data = $this->data;
     }
     $mapper = $table->getMapper();
     if (!$mapper) {
         return $data;
     }
     $result = [];
     foreach ($data as $row) {
         $result[] = $mapper($row);
     }
     return $result;
 }
 /**
  * Paginate and return result
  *
  * @param Table $table
  * @return array
  */
 public function paginate(Table $table)
 {
     $query = $this->getQueryBuilder()->getQuery();
     $cursor = $query->execute();
     $table->calculatePageParams($cursor->count());
     if ($table->getPageSize() > 0) {
         $cursor->skip($table->getPageSize() * ($table->getPageNumber() - 1));
         $cursor->limit($table->getPageSize());
     }
     $mapper = $table->getMapper();
     if (!$mapper) {
         throw new \Exception("Data 'mapper' is required when using DoctrineAdapter");
     }
     $result = [];
     foreach ($cursor as $row) {
         $result[] = $mapper($row);
     }
     return $result;
 }