Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ APP_SECRET=b2b0f5c49ef6e56406e93246d9250c00
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_URL="mysql://symfony:symfony@localhost:3306/symfony?serverVersion=10.3.34-MariaDB&charset=utf8mb4"
DATABASE_URL="mysql://symfony:symfony@127.0.0.1:3306/symfony?serverVersion=10.3.34-MariaDB&charset=utf8mb4"
# DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?ser/verVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###
2 changes: 1 addition & 1 deletion config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ security:
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/task/*, roles: ROLE_USER }

- { path: ^/project/*, roles: ROLE_USER}
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
6 changes: 3 additions & 3 deletions config/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#index:
# path: /
# controller: App\Controller\DefaultController::index
index:
path: /
controller: App\Controller\ProjectController::list
34 changes: 34 additions & 0 deletions migrations/Version20220602121009.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220602121009 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE project (id INT AUTO_INCREMENT NOT NULL, author_id INT NOT NULL, key_project VARCHAR(5) NOT NULL, name VARCHAR(255) NOT NULL, INDEX IDX_2FB3D0EEF675F31B (author_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE project ADD CONSTRAINT FK_2FB3D0EEF675F31B FOREIGN KEY (author_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE task CHANGE is_completed is_completed TINYINT(1) DEFAULT 0 NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE project');
$this->addSql('ALTER TABLE task CHANGE is_completed is_completed TINYINT(1) DEFAULT NULL');
}
}
35 changes: 35 additions & 0 deletions migrations/Version20220602123943.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220602123943 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE task ADD project_id INT NOT NULL');
$this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB25166D1F9C FOREIGN KEY (project_id) REFERENCES project (id)');
$this->addSql('CREATE INDEX IDX_527EDB25166D1F9C ON task (project_id)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE task DROP FOREIGN KEY FK_527EDB25166D1F9C');
$this->addSql('DROP INDEX IDX_527EDB25166D1F9C ON task');
$this->addSql('ALTER TABLE task DROP project_id');
}
}
80 changes: 80 additions & 0 deletions src/Controller/ProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Controller;

use App\Entity\Project;
use App\Entity\Task;
use App\Form\ProjectType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Type\TaskFilterType;
use App\Type\TaskType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Routing\Annotation\Route;


class ProjectController extends AbstractController
{
/**
* @Route("/projects", name="project_list")
*/
public function list(): Response
{
$user = $this->getUser();
$projects = $this->getDoctrine()->getManager()
->getRepository(Project::class)
->getAvailableProjects($user->getId(), $this->isGranted('ROLE_ADMIN'));
return $this->render('project/list.html.twig', [
'projects' => $projects,
]);
}


/**
* @Route("/projects/create", name="project_create")
*/
public function create(Request $request): Response
{
$project = new Project();
$form = $this->createForm(ProjectType::class, $project);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

$project->setAuthor($this->getUser());

$this->getDoctrine()->getManager()->persist($project);
$this->getDoctrine()->getManager()->flush();

return $this->redirectToRoute('project_list');
}

return $this->render("project/create.html.twig", [
'form' => $form->createView()
]);
}

/**
* @Route("project/show={id}", name="show_porject")
* @param $id
* @return void
*/
public function show($id): Response

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно добавить проверку что пользователю доступен данные проект

{
$project = $this->getDoctrine()->getManager()->find(Project::class, $id);

if ($project === null) {
throw $this->createNotFoundException(sprintf("Project with id %s not found", $id));
}

$tasks = $this->getDoctrine()->getRepository(Task::class)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно использовать $project->getTasks();
Для этого надо https://symfony.com/doc/4.4/doctrine/associations.html#joining-related-records

->findBy(['project' => $id], []);

return $this->render('project/show.html.twig',[
'id' => $id,
'tasks' => $tasks,
]);
}
}
76 changes: 76 additions & 0 deletions src/Entity/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Entity;

use App\Repository\ProjectRepository;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass=ProjectRepository::class)
*/
class Project
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=5)
*/
private $keyProject;

/**
* @ORM\Column(type="string", length=255)
*/
private $name;

/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="projects")
* @ORM\JoinColumn(nullable=false)
*/
private $author;

public function getId(): ?int
{
return $this->id;
}

public function getKeyProject(): ?string
{
return $this->keyProject;
}

public function setKeyProject(string $keyProject): self
{
$this->keyProject = $keyProject;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

public function getAuthor(): ?User
{
return $this->author;
}

public function setAuthor(?User $author): self
{
$this->author = $author;

return $this;
}
}
15 changes: 14 additions & 1 deletion src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Task
*/
protected $author;

/**
* @ORM\ManyToOne(targetEntity=Project::class, inversedBy="project")
*/
private $project;

/**
* Create empty task
*/
Expand Down Expand Up @@ -161,7 +166,15 @@ public function setIsCompleted(bool $isCompleted = false)
$this->isCompleted = $isCompleted;
}

public function getProject(): ?Project
{
return $this->project;
}

public function setProject(?Project $project): self
{
$this->project = $project;


return $this;
}
}
12 changes: 12 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

Expand Down Expand Up @@ -34,6 +36,16 @@ class User implements UserInterface
*/
private $password;

/**
* @ORM\OneToMany(targetEntity=Project::class, mappedBy="author")
*/
private $projects;

public function __construct()
{
$this->projects = new ArrayCollection();
}

public function __toString()
{
return $this->getUsername();
Expand Down
22 changes: 22 additions & 0 deletions src/Form/ProjectType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Form;

use App\Entity\Project;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProjectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('keyProject', TextType::class)
->add('name', TextType::class)
->add('save', SubmitType::class)
;
}
}
Loading