feat(db): add entities
This commit is contained in:
32
src/Entity/Database/Comments/Comment.php
Normal file
32
src/Entity/Database/Comments/Comment.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity\Database\Comments;
|
||||
|
||||
use App\Entity\Database\Posts\Post;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\ManyToOne;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
|
||||
#[Entity]
|
||||
#[Table(name: "comments")]
|
||||
class Comment
|
||||
{
|
||||
public function __construct(
|
||||
#[Id,
|
||||
Column]
|
||||
public ?int $id,
|
||||
#[ManyToOne(targetEntity: Post::class, inversedBy: "comments")]
|
||||
public Post $post,
|
||||
#[Column(type: "text")]
|
||||
public string $name,
|
||||
#[Column(length: 320 /* see RFC5321 */)]
|
||||
public string $email,
|
||||
#[Column(type: "text")]
|
||||
public string $body,
|
||||
) {
|
||||
}
|
||||
}
|
||||
39
src/Entity/Database/Posts/Post.php
Normal file
39
src/Entity/Database/Posts/Post.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity\Database\Posts;
|
||||
|
||||
use App\Entity\Database\Comments\Comment;
|
||||
use App\Entity\Database\Users\User;
|
||||
use App\Repository\Post\PostRepository;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\ManyToOne;
|
||||
use Doctrine\ORM\Mapping\OneToMany;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
|
||||
#[Entity(repositoryClass: PostRepository::class)]
|
||||
#[Table(name: "posts")]
|
||||
class Post
|
||||
{
|
||||
public function __construct(
|
||||
#[Id,
|
||||
Column]
|
||||
public ?int $id,
|
||||
#[ManyToOne(targetEntity: User::class)]
|
||||
public User $user,
|
||||
#[Column(type: "text")]
|
||||
public string $title,
|
||||
#[Column(type: "text")]
|
||||
public string $body,
|
||||
/**
|
||||
* @var Collection<int, Comment> $comments
|
||||
*/
|
||||
#[OneToMany(targetEntity: Comment::class, mappedBy: "post")]
|
||||
public Collection $comments,
|
||||
) {
|
||||
}
|
||||
}
|
||||
40
src/Entity/Database/Users/Address.php
Normal file
40
src/Entity/Database/Users/Address.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity\Database\Users;
|
||||
|
||||
use App\Repository\Users\AddressRepository;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\GeneratedValue;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\OneToOne;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
|
||||
#[Entity(repositoryClass: AddressRepository::class)]
|
||||
#[Table(name: "addresses")]
|
||||
class Address
|
||||
{
|
||||
public function __construct(
|
||||
#[Id,
|
||||
Column,
|
||||
GeneratedValue(strategy: "SEQUENCE")]
|
||||
public ?int $id,
|
||||
#[OneToOne(targetEntity: User::class, mappedBy: 'address')]
|
||||
public ?User $user,
|
||||
#[Column(type: "text")]
|
||||
public string $street,
|
||||
#[Column(type: "text")]
|
||||
public string $suite,
|
||||
#[Column(type: "text")]
|
||||
public string $city,
|
||||
#[Column(type: "text")]
|
||||
public string $zipcode,
|
||||
#[Column]
|
||||
public float $lat,
|
||||
#[Column]
|
||||
public float $lng,
|
||||
) {
|
||||
}
|
||||
}
|
||||
39
src/Entity/Database/Users/Company.php
Normal file
39
src/Entity/Database/Users/Company.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity\Database\Users;
|
||||
|
||||
use App\Repository\Users\CompanyRepository;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\GeneratedValue;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\OneToMany;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
|
||||
#[Entity(repositoryClass: CompanyRepository::class)]
|
||||
#[Table(name: "companies")]
|
||||
class Company
|
||||
{
|
||||
public function __construct(
|
||||
#[Id,
|
||||
Column,
|
||||
GeneratedValue(strategy: "SEQUENCE")]
|
||||
public ?int $id,
|
||||
/**
|
||||
* @var Collection<int, User> $users
|
||||
*/
|
||||
#[OneToMany(targetEntity: User::class, mappedBy: 'company')]
|
||||
public Collection $users,
|
||||
// TODO: prepokladam ze tohle muzu povazovat za klic a vsechny dalsi parametry budou stejne pro kazdou company se stejnym jmenem
|
||||
#[Column(type: "text", unique: true)]
|
||||
public string $name,
|
||||
#[Column(type: "text")]
|
||||
public string $catchPhrase,
|
||||
#[Column(type: "text")]
|
||||
public string $bs,
|
||||
) {
|
||||
}
|
||||
}
|
||||
42
src/Entity/Database/Users/User.php
Normal file
42
src/Entity/Database/Users/User.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity\Database\Users;
|
||||
|
||||
use App\Repository\Users\UserRepository;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\ManyToOne;
|
||||
use Doctrine\ORM\Mapping\OneToOne;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
|
||||
#[Entity(repositoryClass: UserRepository::class)]
|
||||
#[Table(name: "users")]
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
#[Id,
|
||||
Column]
|
||||
public ?int $id,
|
||||
#[Column(type: "text")] // TODO: type: text is really non-optimal. But there is no docs for api
|
||||
public string $name,
|
||||
#[Column(type: "text")]
|
||||
public string $username,
|
||||
#[Column(type: "text")]
|
||||
public string $email,
|
||||
#[Column(type: "text")]
|
||||
public string $phone,
|
||||
#[Column(type: "text")]
|
||||
public string $website,
|
||||
#[OneToOne(targetEntity: Address::class, inversedBy: "user")]
|
||||
public Address $address,
|
||||
#[ManyToOne(targetEntity: Company::class, inversedBy: "users")]
|
||||
public Company $company,
|
||||
) {
|
||||
if (empty($this->address->user)) {
|
||||
$this->address->user = $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/Repository/Comments/CommentsRepository.php
Normal file
20
src/Repository/Comments/CommentsRepository.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository\Comments;
|
||||
|
||||
use App\Entity\Database\Comments\Comment;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Comment>
|
||||
*/
|
||||
class CommentsRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Comment::class);
|
||||
}
|
||||
}
|
||||
20
src/Repository/Post/PostRepository.php
Normal file
20
src/Repository/Post/PostRepository.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository\Post;
|
||||
|
||||
use App\Entity\Database\Posts\Post;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Post>
|
||||
*/
|
||||
class PostRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Post::class);
|
||||
}
|
||||
}
|
||||
20
src/Repository/Users/AddressRepository.php
Normal file
20
src/Repository/Users/AddressRepository.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository\Users;
|
||||
|
||||
use App\Entity\Database\Users\Address;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Address>
|
||||
*/
|
||||
class AddressRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Address::class);
|
||||
}
|
||||
}
|
||||
40
src/Repository/Users/CompanyRepository.php
Normal file
40
src/Repository/Users/CompanyRepository.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository\Users;
|
||||
|
||||
use App\Entity\Database\Users\Company;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Company>
|
||||
*/
|
||||
class CompanyRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Company::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find company by name
|
||||
* @param string $name
|
||||
* @return Company|null
|
||||
*/
|
||||
public function findByName(string $name): ?Company
|
||||
{
|
||||
return $this->findOneBy(['name' => $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete companies with no associated users
|
||||
* @return void
|
||||
*/
|
||||
public function deleteOrphanRecords(): void
|
||||
{
|
||||
$this->getEntityManager()->createQuery('DELETE FROM App\Entity\Database\Users\Company c WHERE NOT EXISTS (SELECT 1 FROM App\Entity\Database\Users\User u WHERE u.company = c.id)')
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
41
src/Repository/Users/UserRepository.php
Normal file
41
src/Repository/Users/UserRepository.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository\Users;
|
||||
|
||||
use App\Entity\Database\Users\User;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<User>
|
||||
*/
|
||||
class UserRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find users by IDs. If user not exists, will not be set in the result array.
|
||||
* @param array<int> $ids
|
||||
* @return array<User>
|
||||
*/
|
||||
public function findByIds(array $ids): array
|
||||
{
|
||||
$result = [];
|
||||
$resultQ = $this->createQueryBuilder('u')
|
||||
->where('u.id IN (:ids)')
|
||||
->setParameter('ids', $ids)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
foreach ($resultQ as $user) {
|
||||
$result[$user->id] = $user;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user