Skip to content

Instantly share code, notes, and snippets.

@sanikeev
Created December 27, 2013 05:48
Show Gist options
  • Save sanikeev/8143057 to your computer and use it in GitHub Desktop.
Save sanikeev/8143057 to your computer and use it in GitHub Desktop.
Doctrine 2 + ZF2 example
<?php
// ... some code here
public function editAction() {
$auth = new AuthenticationService();
if (!$auth->hasIdentity()) {
$this->redirect()->toUrl('/login');
}
$request = $this->getRequest();
$storage = $auth->getStorage()->read();
$regions = $this->getEntityManager()->getRepository('Application\Entity\Region')->findAll();
$categories = $this->getEntityManager()->getRepository('Application\Entity\Categories')->findAll();
$users = $this->getEntityManager()->find('Application\Entity\Users', $storage->id);
$category = array();
foreach ($users->category as $value) {
$category[] = $value->id;
}
$regionsArray = array();
foreach ($regions as $value) {
$regionsArray[$value->id] = $value->name;
}
$categoriesArray = array();
foreach ($categories as $value) {
$categoriesArray[$value->id] = $value->name;
}
if(!sizeof($category)){
$category = 1;
}
$form = new ProfileForm('profile', count($category));
$form->get('region')->setValueOptions($regionsArray);
$catCollection = $form->get("categories");
$i = 0;
foreach ($catCollection as $item) {
$item->get('category')->setValueOptions($categoriesArray);
$item->get('category')->setValue($category[$i]);
$i++;
}
$region = '';
foreach ($users->region as $value) {
$region = $value->id;
}
$values = array(
'id' => $users->id,
'login' => $users->login,
'lastname' => $users->lastname,
'name' => $users->name,
'skype' => $users->skype,
'icq' => $users->icq,
'telephone' => $users->telephone,
'region' => $region,
);
if (!$request->isPost()) {
$form->setData($values);
return array('form' => $form);
}
$data = $request->getPost();
$form->setData($data);
if (!$form->isValid()) {
$this->redirect()->toUrl('/profile');
}
$file = $this->params()->fromFiles('file');
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/data/' . $form->get('id')->getValue();
if (!is_dir($uploaddir)) {
mkdir($uploaddir);
}
$filename = $file['name'];
if (empty($file['name'])) {
$filename = $users->avatar;
}
$transfer = new Transfer();
$transfer->setDestination($uploaddir);
$transfer->receive($file['name']);
$date = new \DateTime("now");
$data = array(
'id' => $form->get('id')->getValue(),
'login' => $form->get('login')->getValue(),
'name' => $form->get('name')->getValue(),
'lastname' => $form->get('lastname')->getValue(),
'avatar' => $filename,
'icq' => $form->get('icq')->getValue(),
'skype' => $form->get('skype')->getValue(),
'telephone' => $form->get('telephone')->getValue(),
'profileUpdateDate' => $date,
);
foreach ($form->get('categories') as $item) {
$addCategoryRelation = $this->getEntityManager()->find('Application\Entity\Categories', $item->get('category')->getValue());
if (!$users->category->contains($addCategoryRelation)) {
$users->category->add($addCategoryRelation);
};
}
$addRegionRelation = $this->getEntityManager()->find('Application\Entity\Region', $form->get('region')->getValue());
if (!$users->region->contains($addRegionRelation)) {
$users->region->add($addRegionRelation);
}
$users->populate($data);
$this->getEntityManager()->flush();
return array('form' => $form);
}
public function changepasswordAction() {
$auth = new AuthenticationService();
if (!$auth->hasIdentity()) {
$this->redirect()->toUrl('/login');
}
$storage = $auth->getStorage()->read();
$form = new ChangePassForm();
$form->setAttribute('action', '/profile/changepassword');
$request = $this->getRequest();
if (!$request->isPost()) {
return array('form' => $form);
}
$form->setData($request->getPost());
if (!$form->isValid()) {
return array('form' => $form);
}
$user = $this->getEntityManager()->find('Application\Entity\Users', $storage->id);
$data = $form->getData();
if ($data['newpass'] != $data['confirmpass']) {
$message = 'Пароли не совпадают. Попробуйте еще раз.';
return array(
'form' => $form,
'message' => $message,
);
}
$pass = array('password' => $data['newpass']);
$user->populate($pass);
$this->getEntityManager()->flush();
$message = 'Пароль успешно изменен.';
return array(
'form' => $form,
'message' => $message,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment