Add createdBy on subject

This commit is contained in:
myrmidex 2026-06-02 22:42:51 +00:00
parent 63638c2236
commit f93a2e5e19
5 changed files with 115 additions and 1 deletions

View file

@ -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 Version20260602224032 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 subject ADD created_by_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE subject ADD CONSTRAINT FK_FBCE3E7AB03A8386 FOREIGN KEY (created_by_id) REFERENCES "user" (id) NOT DEFERRABLE');
$this->addSql('CREATE INDEX IDX_FBCE3E7AB03A8386 ON subject (created_by_id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE subject DROP CONSTRAINT FK_FBCE3E7AB03A8386');
$this->addSql('DROP INDEX IDX_FBCE3E7AB03A8386');
$this->addSql('ALTER TABLE subject DROP created_by_id');
}
}

View file

@ -32,6 +32,7 @@ class SubjectController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$subject->setCreatedBy($this->getUser());
$em->persist($subject);
$em->flush();

View file

@ -20,6 +20,10 @@ class Subject
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
private ?User $createdBy = null;
public function getId(): ?int
{
return $this->id;
@ -36,4 +40,16 @@ class Subject
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): static
{
$this->createdBy = $createdBy;
return $this;
}
}

View file

@ -0,0 +1,55 @@
<?php
namespace App\Factory;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityRepository;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;
/**
* @extends PersistentObjectFactory<User>
*/
final class UserFactory extends PersistentObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct() {}
#[\Override]
public static function class(): string
{
return User::class;
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
#[\Override]
protected function defaults(): array|callable
{
return [
'email' => self::faker()->text(180),
'password' => self::faker()->text(),
'roles' => [],
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
#[\Override]
protected function initialize(): static
{
return $this
// ->afterInstantiate(function(User $user): void {})
;
}
}

View file

@ -3,6 +3,7 @@
namespace App\Tests\Integration\Subject;
use App\Entity\Restaurant;
use App\Factory\UserFactory;
use App\Repository\SubjectRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -11,8 +12,12 @@ class AddSubjectTest extends WebTestCase
public function testAddingNewSubject(): void
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$user = UserFactory::createOne();
$client->loginUser($user);
$client->request('GET', '/');
$client->submitForm('form_save', ['form[name]' => 'Riviera']);
$this->assertResponseRedirects();
@ -25,6 +30,8 @@ class AddSubjectTest extends WebTestCase
$this->assertNotNull($subject);
$this->assertInstanceOf(Restaurant::class, $subject);
$this->assertNotNull($subject->getCreatedBy());
$this->assertSame($user->getId(), $subject->getCreatedBy()->getId());
$this->assertSelectorTextContains('ul li', 'Riviera');
}