Skip to content
Snippets Groups Projects
Commit bb2d8810 authored by Moreau Elise's avatar Moreau Elise
Browse files

update db with join tables (group, messages, users)

parent 160f7560
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ use App\Application\Actions\Action;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManager;
use Slim\Views\Twig;
use Slim\Flash\Messages;
abstract class GroupAction extends Action
{
......@@ -19,12 +20,12 @@ abstract class GroupAction extends Action
* @param LoggerInterface $logger
* @param GroupRepository $groupRepository
*/
public function __construct(LoggerInterface $logger, EntityManager $em, Twig $twig)
public function __construct(LoggerInterface $logger, EntityManager $em, Twig $twig, Messages $flash)
{
parent::__construct($logger);
$this->groupRepository = $em->getRepository('App\Domain\Group\Group');
$this->em = $em;
$this->twig = $twig;
$this->flash = $flash;
}
}
......@@ -46,7 +46,7 @@ class CreateUserAction extends UserAction
$date = new DateTime('now');
$user = new User(null, $username, $firstname, $lastname, $mail, 0, null, $password, $date);
$user = new User(null, $username, $firstname, $lastname, $mail, $password, $date);
$this->em->persist($user);
$this->em->flush();
......
......@@ -26,7 +26,6 @@ abstract class UserAction extends Action
$this->userRepository = $em->getRepository('App\Domain\User\User');
$this->em = $em;
$this->flash = $flash;
$this->twig = $twig;
}
......
......@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\Domain\Group;
use App\Domain\User\User;
use JsonSerializable;
use Doctrine\ORM\Mapping as ORM;
......@@ -23,9 +24,14 @@ class Group implements JsonSerializable
/**
* @var string
* @ORM\Column(type="string")
* @ORM\ManyToMany(targetEntity="App\Domain\User\User")
* @ORM\OrderBy({"username" = "ASC"})
* @ORM\JoinTable(name="group_users",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
private $ids_users;
private $users;
/**
* @var string
......@@ -44,14 +50,13 @@ class Group implements JsonSerializable
* @param int|null $id
* @param string $name
* @param string $description
* @param string $ids_users
*/
public function __construct(?int $id, string $name, string $description, string $ids_users)
public function __construct(?int $id, string $name, string $description)
{
$this->id = $id;
$this->name = strtolower($name);
$this->description = ucfirst($description);
$this->ids_users = $ids_users;
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
......@@ -90,4 +95,8 @@ class Group implements JsonSerializable
'ids_users' => $this->ids_users
];
}
public function addUser(User $user){
$this->users[] = $user;
}
}
......@@ -6,6 +6,7 @@ namespace App\Domain\Message;
use JsonSerializable;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
......@@ -24,13 +25,13 @@ class Message implements JsonSerializable
/**
* @var int
* @ORM\Column(type="integer")
* @ORM\OneToMany(targetEntity="App\Domain\User\User", mappedBy="username")
*/
private $id_receiver;
/**
* @var int
* @ORM\Column(type="integer")
* @ORM\OneToMany(targetEntity="App\Domain\User\User", mappedBy="username")
*/
private $id_transmitter;
......@@ -59,8 +60,8 @@ class Message implements JsonSerializable
$date_format = 'd/m/Y';
$this->id = $id;
$this->id_receiver = $id_receiver;
$this->id_transmitter = $id_transmitter;
$this->id_receiver = new ArrayCollection();
$this->id_transmitter = new ArrayCollection();
$this->date = DateTime::createFromFormat($date_format, $date);
$this->body = $body;
}
......
......@@ -46,11 +46,15 @@ class User implements JsonSerializable
*/
private $mail;
/**
* @var int
* @ORM\Column(type="integer")
/**
* @ORM\ManyToMany(targetEntity="App\Domain\Group\Group")
* @ORM\OrderBy({"name" = "ASC"})
* @ORM\JoinTable(name="users_groups",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
private $group_id;
private $groups;
/**
* @var bool
......@@ -70,14 +74,19 @@ class User implements JsonSerializable
*/
private $joinDate;
/**
* @var string
* @ORM\ManyToMany(targetEntity="App\Domain\Message\Message")
* @ORM\OrderBy({"username" = "ASC"})
*/
private $messages;
/**
* @param int|null $id
* @param string $username
* @param string $firstName
* @param string $lastName
* @param string $mail
* @param int $group_id
* @param bool $contaminated
* @param string $password
* @param date $joinDate
*/
......@@ -88,8 +97,6 @@ class User implements JsonSerializable
string $firstName,
string $lastName,
string $mail,
?int $group_id,
?bool $contaminated,
string $password,
DateTime $joinDate
) {
......@@ -99,9 +106,10 @@ class User implements JsonSerializable
$this->lastName = ucfirst($lastName);
$this->password = $password;
$this->mail = strtolower($mail);
$this->group_id = $group_id;
$this->contaminated = (bool)$contaminated;
$this->contaminated = false;
$this->joinDate = $joinDate;
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
......@@ -156,9 +164,9 @@ class User implements JsonSerializable
/**
* @return string
*/
public function getGroupid(): ?int
public function getGroups(): ?int
{
return $this->group_id;
return $this->groups;
}
/**
......@@ -241,7 +249,7 @@ class User implements JsonSerializable
'lastName' => $this->lastName,
'password' => $this->password,
'mail' => $this->mail,
'group_id' => $this->group_id,
'groups' => $this->groups,
'contaminated' => (bool)$this->contaminated,
'join_date' => $this->getJoinDateString()
];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment