-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
example of implementation of select from database with delegators #400
base: 4.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Frontend\Fixtures; | ||
|
||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
use Doctrine\Persistence\ObjectManager; | ||
use Frontend\Contact\Entity\Department; | ||
|
||
class DepartmentLoader implements FixtureInterface | ||
{ | ||
public function load(ObjectManager $manager): void | ||
{ | ||
$support = new Department(); | ||
$support->setName('Support'); | ||
|
||
$financial = new Department(); | ||
$financial->setName('Financial'); | ||
|
||
$hr = new Department(); | ||
$hr->setName('Human Resource'); | ||
|
||
$manager->persist($support); | ||
$manager->persist($financial); | ||
$manager->persist($hr); | ||
|
||
$manager->flush(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,3 @@ public function load(ObjectManager $manager): void | |
$manager->flush(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; | ||||
use Dot\AnnotatedServices\Factory\AnnotatedServiceFactory; | ||||
use Frontend\Contact\Controller\ContactController; | ||||
use Frontend\Contact\Delegator\ContactFormDelegator; | ||||
use Frontend\Contact\Form\ContactForm; | ||||
use Frontend\Contact\Service\MessageService; | ||||
use Frontend\Contact\Service\MessageServiceInterface; | ||||
|
@@ -42,10 +43,14 @@ public function getDependencies(): array | |||
Application::class => [ | ||||
RoutesDelegator::class, | ||||
], | ||||
ContactForm::class => [ | ||||
ContactFormDelegator::class | ||||
] | ||||
], | ||||
'factories' => [ | ||||
ContactController::class => AnnotatedServiceFactory::class, | ||||
MessageService::class => AnnotatedServiceFactory::class, | ||||
ContactForm::class => ElementFactory::class | ||||
], | ||||
'aliases' => [ | ||||
MessageServiceInterface::class => MessageService::class, | ||||
|
@@ -73,10 +78,13 @@ public function getForms(): array | |||
return [ | ||||
'form_manager' => [ | ||||
'factories' => [ | ||||
ContactForm::class => ElementFactory::class, | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
], | ||||
'aliases' => [ | ||||
], | ||||
'delegators' => [ | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
], | ||||
], | ||||
]; | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -34,6 +34,7 @@ class ContactController extends AbstractActionController | |||
protected AuthenticationServiceInterface $authenticationService; | ||||
protected FlashMessenger $messenger; | ||||
protected FormsPlugin $forms; | ||||
protected ContactForm $contactForm; | ||||
protected DebugBar $debugBar; | ||||
protected array $config; | ||||
|
||||
|
@@ -46,6 +47,7 @@ class ContactController extends AbstractActionController | |||
* @param AuthenticationService $authenticationService | ||||
* @param FlashMessenger $messenger | ||||
* @param FormsPlugin $forms | ||||
* @param ContactForm $contactForm | ||||
* @param DebugBar $debugBar | ||||
* @param array $config | ||||
* @Inject({ | ||||
|
@@ -57,6 +59,7 @@ class ContactController extends AbstractActionController | |||
* FlashMessenger::class, | ||||
* FormsPlugin::class, | ||||
* DebugBar::class, | ||||
* ContactForm::class, | ||||
* "config" | ||||
* }) | ||||
*/ | ||||
|
@@ -69,6 +72,7 @@ public function __construct( | |||
FlashMessenger $messenger, | ||||
FormsPlugin $forms, | ||||
DebugBar $debugBar, | ||||
ContactForm $contactForm, | ||||
array $config = [] | ||||
) { | ||||
$this->messageService = $messageService; | ||||
|
@@ -78,16 +82,19 @@ public function __construct( | |||
$this->authenticationService = $authenticationService; | ||||
$this->messenger = $messenger; | ||||
$this->forms = $forms; | ||||
$this->contactForm = $contactForm; | ||||
$this->debugBar = $debugBar; | ||||
$this->config = $config; | ||||
|
||||
} | ||||
|
||||
/** | ||||
* @return ResponseInterface | ||||
*/ | ||||
public function formAction(): ResponseInterface | ||||
{ | ||||
$form = new ContactForm(); | ||||
// $form = new ContactForm(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
// $form = $this->forms(ContactForm::class); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
$request = $this->getRequest(); | ||||
|
||||
if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { | ||||
|
@@ -105,9 +112,9 @@ public function formAction(): ResponseInterface | |||
} | ||||
|
||||
$data['subject'] = $data['subject'] ?: $this->config['application']['name'] . ' Contact'; | ||||
$form->setData($data); | ||||
if ($form->isValid()) { | ||||
$dataForm = $form->getData(); | ||||
$this->contactForm->setData($data); | ||||
if ($this->contactForm->isValid()) { | ||||
$dataForm = $this->contactForm->getData(); | ||||
$result = $this->messageService->processMessage($dataForm); | ||||
|
||||
if ($result) { | ||||
|
@@ -118,13 +125,13 @@ public function formAction(): ResponseInterface | |||
return new RedirectResponse($request->getUri(), 303); | ||||
} | ||||
} else { | ||||
$this->messenger->addError($this->forms->getMessages($form)); | ||||
$this->messenger->addError($this->forms->getMessages($this->contactForm)); | ||||
return new RedirectResponse($request->getUri(), 303); | ||||
} | ||||
} | ||||
|
||||
return new HtmlResponse($this->template->render('contact::contact-form', [ | ||||
'form' => $form, | ||||
'form' => $this->contactForm, | ||||
'recaptchaSiteKey' => $this->config['recaptcha']['siteKey'] | ||||
])); | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||
<?php | ||||||
|
||||||
namespace Frontend\Contact\Delegator; | ||||||
|
||||||
use Doctrine\ORM\EntityManager; | ||||||
use Doctrine\ORM\Exception\NotSupported; | ||||||
use Frontend\Contact\Entity\Department; | ||||||
use Frontend\Contact\Form\ContactForm; | ||||||
use Frontend\User\Entity\UserRole; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
use Laminas\ServiceManager\Factory\DelegatorFactoryInterface; | ||||||
use Psr\Container\ContainerExceptionInterface; | ||||||
use Psr\Container\ContainerInterface; | ||||||
use Psr\Container\NotFoundExceptionInterface; | ||||||
|
||||||
class ContactFormDelegator implements DelegatorFactoryInterface | ||||||
{ | ||||||
/** | ||||||
* @throws ContainerExceptionInterface | ||||||
* @throws NotFoundExceptionInterface | ||||||
* @throws NotSupported | ||||||
*/ | ||||||
public function __invoke( | ||||||
ContainerInterface $container, | ||||||
$name, | ||||||
callable $callback, | ||||||
?array $options = null | ||||||
): ContactForm { | ||||||
|
||||||
/** @var ContactForm $contactForm */ | ||||||
$contactForm = $callback(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No need to use the callback here, |
||||||
|
||||||
/** @var EntityManager $entityManager */ | ||||||
$entityManager = $container->get(EntityManager::class); | ||||||
|
||||||
$departments = $entityManager->getRepository(Department::class) | ||||||
->findAll(); | ||||||
|
||||||
$options = []; | ||||||
|
||||||
/** @var Department $department */ | ||||||
foreach ($departments as $department){ | ||||||
$data = [ | ||||||
'value' => $department->getUuid()->toString(), | ||||||
'label' => $department->getName(), | ||||||
'selected' => $department->getName() === 'Support' // here you can add your option which one to be default selected | ||||||
]; | ||||||
|
||||||
$options[] = $data; | ||||||
} | ||||||
|
||||||
$contactForm->setDepartment($options); | ||||||
|
||||||
return $contactForm; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||
<?php | ||||
|
||||
namespace Frontend\Contact\Entity; | ||||
|
||||
use Frontend\App\Common\AbstractEntity; | ||||
use Doctrine\ORM\Mapping as ORM; | ||||
|
||||
|
||||
/** | ||||
* @ORM\Entity(repositoryClass="Frontend\Contact\Repository\DepartmentRepository") | ||||
* @ORM\Table(name="department") | ||||
* @ORM\HasLifecycleCallbacks | ||||
*/ | ||||
class Department extends AbstractEntity | ||||
{ | ||||
/** | ||||
* @ORM\Column(name="name", type="string", length=191) | ||||
*/ | ||||
protected string $name = ''; | ||||
|
||||
public function getName(): string | ||||
{ | ||||
return $this->name; | ||||
} | ||||
|
||||
public function setName(string $name): void | ||||
{ | ||||
$this->name = $name; | ||||
} | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Frontend\Contact\Repository; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
|
||
class DepartmentRepository extends EntityRepository | ||
{ | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TEXT (or even a VARCHAR) should suffice - let's revert this change.