示例#1
0
 /**
  *
  * @param \User\Model\User $item
  * @param unknown $options
  */
 public function search($item, $options)
 {
     $select = $this->getDbSql()->select(array('u' => self::TABLE_NAME));
     if ($item->getId()) {
         $select->where(['u.id' => $item->getId()]);
     }
     if ($item->getUsername()) {
         $select->where(['(u.username LIKE ? OR u.fullName LIKE ?)' => ['%' . $item->getUsername() . '%', '%' . $item->getUsername() . '%']]);
     }
     if ($item->getEmail()) {
         $select->where(['(u.email LIKE ?)' => '%' . $item->getEmail() . '%']);
     }
     if ($item->getActive()) {
         if ($item->getActive() > 0) {
             $select->where(['u.active' => 1]);
         } else {
             $select->where(['(u.active IS NULL OR u.active != ?)' => 1]);
         }
     }
     if ($item->getRole()) {
         $select->where(['u.role' => $item->getRole()]);
     }
     $select->order(['u.id' => 'DESC']);
     $paginator = $this->preparePaginator($select, $options, new User());
     $userIds = [];
     $districIds = [];
     $cityIds = [];
     $ids = [];
     foreach ($paginator as $user) {
         /*@var $user \User\Model\User */
         if ($user->getCreatedById()) {
             $userIds[$user->getCreatedById()] = $user->getCreatedById();
         }
         if ($user->getDistrictId()) {
             $districIds[$user->getDistrictId()] = $user->getDistrictId();
         }
         if ($user->getCityId()) {
             $cityIds[$user->getCityId()] = $user->getCityId();
         }
         $ids[] = $user->getId();
     }
     $dbAdapter = $this->getDbAdapter();
     $users = [];
     if (count($userIds)) {
         $select = $this->getDbSql()->select(array('u' => self::TABLE_NAME));
         $select->where(['u.id' => $userIds]);
         $query = $this->getDbSql()->buildSqlString($select);
         $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
         if ($rows->count()) {
             foreach ($rows as $row) {
                 $createdBy = new User();
                 $createdBy->exchangeArray((array) $row);
                 $users[$createdBy->getId()] = $createdBy;
             }
         }
     }
     $cities = [];
     if (count($cityIds)) {
         $select = $this->getDbSql()->select(array('c' => \Address\Model\CityMapper::TABLE_NAME));
         $select->where(['id' => $cityIds]);
         $query = $this->getDbSql()->buildSqlString($select);
         $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
         if ($rows->count()) {
             foreach ($rows as $row) {
                 $city = new \Address\Model\City();
                 $city->exchangeArray((array) $row);
                 $cities[$city->getId()] = $city;
             }
         }
     }
     $districs = [];
     if (count($districIds)) {
         $select = $this->getDbSql()->select(array('c' => \Address\Model\DistrictMapper::TABLE_NAME));
         $select->where(['id' => $districIds]);
         $query = $this->getDbSql()->buildSqlString($select);
         $rows = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
         if ($rows->count()) {
             foreach ($rows as $row) {
                 $distric = new \Address\Model\District();
                 $distric->exchangeArray((array) $row);
                 $districs[$distric->getId()] = $distric;
             }
         }
     }
     if (count($paginator->getCurrentModels())) {
         foreach ($paginator->getCurrentModels() as $user) {
             if ($user->getCreatedById() && isset($users[$user->getCreatedById()])) {
                 $user->addOption('createdBy', $users[$user->getCreatedById()]);
             }
             if ($user->getCityId() && isset($cities[$user->getCityId()])) {
                 $user->setCity($cities[$user->getCityId()]);
             }
             if ($user->getDistrictId() && isset($districs[$user->getDistrictId()])) {
                 $user->setDistrict($districs[$user->getDistrictId()]);
             }
         }
     }
     return $paginator;
 }