Created
March 28, 2021 11:47
-
-
Save BlackScorp/c7b7a5b8b982d3961fafb704c9018e28 to your computer and use it in GitHub Desktop.
Doctrine ORM Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\Configuration; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Doctrine\ORM\Tools\Setup; | |
require_once __DIR__.'/vendor/autoload.php'; | |
$container= []; | |
$container['db'] = [ | |
'driver' => 'pdo_mysql', | |
'user' => 'root', | |
'password' => '', | |
'dbname' => 'shop', | |
'charset' => 'utf8' | |
]; | |
$container['env']= 'dev'; | |
$container[Configuration::class] = function () use($container){ | |
$isDevelopment = $container['env'] === 'dev'; | |
return Setup::createAnnotationMetadataConfiguration( | |
[ | |
__DIR__.'/src/Entity' | |
],$isDevelopment,null,null,false | |
); | |
}; | |
$container[EntityManagerInterface::class] = function() use($container){ | |
$connection = $container['db']; | |
$annotationConfiguration = $container[Configuration::class](); | |
return EntityManager::create($connection,$annotationConfiguration); | |
}; | |
return $container; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use BlackScorp\ORM\Entity\User; | |
$container = require_once __DIR__.'/depdency.php'; | |
/** @var \Doctrine\ORM\EntityManager $entityManager */ | |
$entityManager = $container[\Doctrine\ORM\EntityManagerInterface::class](); | |
/** @var \BlackScorp\ORM\Entity\User $user */ | |
$user = $entityManager->find(User::class,26); | |
$user->setPassword(password_hash('testpassword',PASSWORD_DEFAULT)); | |
$entityManager->remove($user); | |
try{ | |
$entityManager->flush(); | |
}catch (Exception $e){ | |
echo $e->getMessage(); | |
die(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace BlackScorp\ORM\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Class User | |
* @package BlackScorp\ORM\Entity | |
* @ORM\Entity | |
*/ | |
class User | |
{ | |
/** | |
* @var int | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue | |
*/ | |
protected int $id; | |
/** | |
* @var string | |
* @ORM\Column(type="string",nullable=false) | |
*/ | |
protected string $username; | |
/** | |
* @var string | |
* @ORM\Column(type="string",nullable=false) | |
*/ | |
protected string $password; | |
/** | |
* @var string | |
* @ORM\Column(type="string", nullable=false,unique=true) | |
*/ | |
protected string $email; | |
/** | |
* @var string|null | |
* @ORM\Column(type="string", nullable=true) | |
*/ | |
protected ?string $activationKey = null; | |
/** | |
* @var string | |
* @ORM\Column(type="string",columnDefinition="ENUM('USER','ADMIN')", options={"default":"USER"}) | |
*/ | |
protected string $userRights; | |
/** | |
* @return int | |
*/ | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
/** | |
* @param int $id | |
*/ | |
public function setId(int $id): void | |
{ | |
$this->id = $id; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUsername(): string | |
{ | |
return $this->username; | |
} | |
/** | |
* @param string $username | |
*/ | |
public function setUsername(string $username): void | |
{ | |
$this->username = $username; | |
} | |
/** | |
* @return string | |
*/ | |
public function getEmail(): string | |
{ | |
return $this->email; | |
} | |
/** | |
* @param string $email | |
*/ | |
public function setEmail(string $email): void | |
{ | |
$this->email = $email; | |
} | |
/** | |
* @return string|null | |
*/ | |
public function getActivationKey(): ?string | |
{ | |
return $this->activationKey; | |
} | |
/** | |
* @param string|null $activationKey | |
*/ | |
public function setActivationKey(?string $activationKey): void | |
{ | |
$this->activationKey = $activationKey; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUserRights(): string | |
{ | |
return $this->userRights; | |
} | |
/** | |
* @param string $userRights | |
*/ | |
public function setUserRights(string $userRights): void | |
{ | |
$this->userRights = $userRights; | |
} | |
/** | |
* @return string | |
*/ | |
public function getPassword(): string | |
{ | |
return $this->password; | |
} | |
/** | |
* @param string $password | |
*/ | |
public function setPassword(string $password): void | |
{ | |
$this->password = $password; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment