示例#1
0
 /**
  * Execute query and fetch models from database
  *
  * @return $this
  * @throws Exception
  */
 public function fetch()
 {
     $modelName = $this->_className;
     // execute fetch query
     $query = $this->_dataSource->prepare($this->_queryHelper->buildQuery());
     $query->execute($this->_queryHelper->getWhereParamsValues());
     // check for mysql error
     $errorcode = $query->errorInfo();
     if ($errorcode[0] != "00000") {
         throw new Exception($errorcode[2]);
     }
     // fetch query and hydrate models
     $fetch = $query->fetchAll(\PDO::FETCH_ASSOC);
     foreach ($fetch as &$unRes) {
         /** @var $object \PicORM\Model */
         $object = new $modelName();
         $object->hydrate($unRes, false);
         $unRes = $object;
     }
     // configure collection after fetch
     $this->isFetched = true;
     $this->models = $fetch;
     // if pagination used grab the total found model
     if ($this->_usePagination) {
         $this->_paginationFoundModels = $this->queryFoundModels();
     }
     return $this;
 }