public function editAction() { $accountMapper = $this->getServiceLocator()->get('Accounting\\Model\\AccountMapper'); $account = new Account(); $account->setId($this->params()->fromQuery('id')); if (!$account->getId() || !$accountMapper->get($account)) { return $this->page404(); } $form = new \Accounting\Form\Account($this->getServiceLocator()); $form->remove('afterSubmit'); $form->populateValues($account->toFormValues()); if ($this->getRequest()->isPost()) { $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { $account->exchangeArray($form->getData()); $accountMapper->save($account); return $this->redirect()->toUrl('/accounting/account/index?id=' . $account->getId()); } } $this->getViewModel()->setVariable('form', $form); return $this->getViewModel(); }
/** * * @author Hungpx * @param \Accounting\Model\Account $item */ public function fetchAll($item) { $select = $this->getDbSql()->select(array('c' => self::TABLE_NAME)); if ($item->getId()) { $select->where(['c.id' => $item->getId()]); } elseif ($item->getOption('ids')) { $select->where(['c.id' => $item->getOption('ids')]); } if ($item->getCompanyId()) { $select->where(['c.companyId' => $item->getCompanyId()]); } if ($item->getOption('companyId')) { $select->where(['c.companyId' => $item->getOption('companyId')]); } if ($item->getName()) { $select->where(['c.name LIKE ?' => '%' . $item->getName() . '%']); } if ($item->getOption('sort')) { $select->order([implode(' ', $item->getOption('sort'))]); } else { $select->order(['c.id' => 'DESC']); } $query = $this->getDbSql()->buildSqlString($select); $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE); $paginator = []; $userIds = []; $companyIds = []; $accountIds = []; if ($results->count()) { foreach ($results as $row) { $row = (array) $row; $account = new Account(); $account->exchangeArray($row); $paginator[] = $account; if ($account->getCreatedById()) { $userIds[$account->getCreatedById()] = $account->getCreatedById(); } if ($account->getCompanyId()) { $companyIds[$account->getCompanyId()] = $account->getCompanyId(); } $accountIds[$account->getId()] = $account->getId(); } } if ($item->getOption('fetchOnlyIds')) { return $accountIds; } $userNames = []; $companyNames = []; // get User's name if (count($userIds) && $item->getOption('loadUser')) { $select = $this->getDbSql()->select(['c' => \User\Model\UserMapper::TABLE_NAME], ['id', 'fullName']); $select->where(['c.id' => $userIds]); $query = $this->getDbSql()->buildSqlString($select); $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE); unset($select); if ($results) { $resultArr = $results->toArray(); foreach ($resultArr as $result) { $userNames[$result['id']] = $result['fullName']; } } } // Get Company if (count($companyIds) && $item->getOption('loadCompany')) { $select = $this->getDbSql()->select(['cp' => \Company\Model\CompanyMapper::TABLE_NAME], ['id', 'name']); $select->where(['cp.id' => $companyIds]); $query = $this->getDbSql()->buildSqlString($select); $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE); unset($select); if ($results) { $resultArr = $results->toArray(); foreach ($resultArr as $result) { $companyNames[$result['id']] = $result['name']; } } } foreach ($paginator as $account) { $userId = $account->getCreatedById(); $companyId = $account->getCompanyId(); $itemId = $account->getId(); $account->addOption('userName', isset($userNames[$userId]) ? $userNames[$userId] : null); $account->addOption('companyName', isset($companyNames[$companyId]) ? $companyNames[$companyId] : null); } return $paginator; }
/** * * @param \Accounting\Model\Account $item */ public function isExisted($item) { if (!$item->getCode() || !$item->getCompanyId()) { return null; } $select = $this->getDbSql()->select(self::TABLE_NAME); $select->where(['code' => $item->getCode(), 'companyId' => $item->getCompanyId()]); if ($item->getId()) { $select->where(['id != ?' => $item->getId()]); } if ($item->getParentId()) { $select->where(['parentId' => $item->getParentId()]); } else { $select->where(['parentId IS NULL']); } $select->limit(1); $query = $this->getDbSql()->buildSqlString($select); $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE); if ($results->count()) { $item->exchangeArray((array) $results->current()); return true; } return false; }