/** * @param \Application\Model\User $user */ public function __construct(\Application\Model\User $user) { parent::__construct($user->getId()); if ($user->isAdministrator()) { $this->setName('admin'); } else { $this->setName('member'); } $this->user = $user; }
public function save(User $user) { $data = array('username' => $user->getUsername(), 'password' => $user->getPassword(), 'name' => $user->getName(), 'valid' => $user->getValid(), 'role' => $user->getRole()); $id = (int) $user->getId(); if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->get($id)) { $this->tableGateway->update($data, array('id' => $id)); } else { throw new \Exception('User não existe'); } } }
/** * Merge second user into first one and returns it * @param User $userByEmail user to be kept * @param User $userByIdentity user to be deleted * @param Identity $identity identity to be moved from the deleted user to kept user * @return User */ private function mergeUser(User $userByEmail, User $userByIdentity, Identity $identity) { // Gather all references to the user that we are going to delete $rsm = new \Doctrine\ORM\Query\ResultSetMapping(); $rsm->addScalarResult('TABLE_NAME', 'TABLE_NAME'); $rsm->addScalarResult('COLUMN_NAME', 'COLUMN_NAME'); $qb = $this->getEntityManager()->createNativeQuery("\n SELECT TABLE_NAME, COLUMN_NAME\n FROM `information_schema`.`KEY_COLUMN_USAGE`\n WHERE\n REFERENCED_TABLE_SCHEMA = :database\n AND REFERENCED_TABLE_NAME = 'user'\n AND REFERENCED_COLUMN_NAME = 'id'", $rsm); $database = $this->getEntityManager()->getConnection()->getDatabase(); $qb->setParameters(['database' => $database]); $records = $qb->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); // Update all references from the old user to the user we're going to keep foreach ($records as $r) { $table = $r['TABLE_NAME']; $field = $r['COLUMN_NAME']; $query = "UPDATE `{$table}` SET `{$field}` = :newUser WHERE `{$field}` = :oldUser"; $this->getEntityManager()->getConnection()->executeUpdate($query, ['newUser' => $userByEmail->getId(), 'oldUser' => $userByIdentity->getId()]); } // This is not strictly necessary since the DB just has been update a // few lines before, but just to keep the model in memory up to date we do it "again" $identity->setUser($userByEmail); // Delete the duplicated user $this->getEntityManager()->remove($userByIdentity); return $userByEmail; }
public function addUser(User $user) { $data = array('id' => $user->getId(), 'username' => $user->getUsername(), 'password' => $user->getPassword(), 'role_id' => $user->getRoleId(), 'create_date' => date("Y-m-d H:i:s")); $sql = new Sql($this->dbAdapter); $insert = $sql->insert('users'); $insert->values($data); $selectString = $sql->getSqlStringForSqlObject($insert); $ret; try { $ret = $this->dbAdapter->query($selectString, Adapter::QUERY_MODE_EXECUTE); } catch (\Exception $e) { $ret = NULL; } return $ret; }