How to fix a problem with a nested onetomany relationship not showing in Apigility route - zend-framework

I have a problem using Apigility and Doctrine at their latest versions.
I have 2 entities: Advert and Application on a one to many bidirectional relationship. Many applications can be associated with an Advert and an Advert has many applications.
My tables are created as such (foreign key advert_id on applications table).
I cannot manage to call an advert route with its applications, the applications field is always empty.
After some research ive come to the conclusion that apigility just doesnt know how to render the nested collection. Ive read about hydrators and a package called api-skeletons/zf-doctrine-hydrator that would provide a helper for these doctrine nested relationships, but sadly its not compatible with version 4 of phpro/zf-doctrine-hydration-module and I cannot downgrade because of other packages and dependencies.
<?php
namespace Fil\V1\Entity;
use Doctrine\ORM\Mapping as ORM;
class Applications
{
/**
* #var \Fil\V1\Entity\Adverts
*
* #ORM\ManyToOne(targetEntity="Fil\V1\Entity\Adverts",inversedBy="applications")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=true)
* })
*/
private $advert;
/**
* Set advert.
*
* #param \Fil\V1\Entity\Adverts|null $advert
*
* #return Applications
*/
public function setAdvert(\Fil\V1\Entity\Adverts $advert = null)
{
$this->advert = $advert;
return $this;
}
/**
* Get advert.
*
* #return \Fil\V1\Entity\Adverts|null
*/
public function getAdvert()
{
return $this->advert;
}
}
class Adverts
{
...
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\OneToMany(targetEntity="Fil\V1\Entity\Applications", mappedBy="advert")
*/
private $applications;
/**
* Constructor
*/
public function __construct()
{
$this->applications = new \Doctrine\Common\Collections\ArrayCollection;
}
/**
* Add application.
*
* #param \Fil\V1\Entity\Applications $application
*
* #return Adverts
*/
public function addApplication(\Fil\V1\Entity\Applications $application)
{
$this->applications[] = $application;
return $this;
}
/**
* Remove application.
*
* #param \Fil\V1\Entity\Applications $application
*
* #return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeApplication(\Fil\V1\Entity\Applications $application)
{
return $this->applications->removeElement($application);
}
/**
* Get applications.
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getApplications()
{
return ($this->applications);
}
}
When I call the route for Applications (example: applications/1) it works perfectly, I get my json objet with the Advert relationship correct, with all the information. So the relationship is well conceived.
{
id: 2,
name: "application1",
_embedded:
{
advert:
{
id: 1,
name: "Advert1",
applications: { },
_links:
{
self:
{
href: "http://localhost:8080/adverts/1"
}
}
}
},
_links:
{
self:
{
href: "http://localhost:8080/applications/2"
}
}
}
The problem is when I call and Advert route (example: advert/2). I get the Json with the correct advert information, but the applications field is empty, just {}. There are multiple applications linked to the advert but it just doesnt work.
{
id: 2,
name: "Advert1,
applications: { },
_links:
{
self:
{
href: "http://localhost:8080/adverts/2"
}
}
}
Normally the applications field should be filled with different objets representing each of the applications.
Is there anyway to overcome this issue? thank you!

The ApiSkeletons vendor package has this by design. I'm with you, would prefer it return collections, but the fix is easy: create a new strategy!
Create a strategy class and extend the AllowRemoveByValue strategy of Doctrine.
Overwrite the extract function to return a Collection, either filled or empty
That's it.
Full class:
namespace Application\Strategy;
use DoctrineModule\Stdlib\Hydrator\Strategy\AllowRemoveByValue;
use ZF\Hal\Collection;
class UniDirectionalToManyStrategy extends AllowRemoveByValue
{
public function extract($value)
{
return new Collection($value ?: []);
}
}
Apply this strategy where you need it. E.g. your Advert as many Applications, so the config should be modified like so:
'doctrine-hydrator' => [
'Fil\\V1\\Entity\\ApplicationHydrator' => [
// other config
'strategies' => [
'adverts' => \Application\Strategy\UniDirectionalToManyStrategy::class,
],
],
],
Now collections should be returned.
Quick note: this will only work as a strategy for the *ToMany side.

Related

Dynamic recipient in form

I have a custom extension with list and detail of teachers. On the detail page I include a form with code:
<formvh:render persistenceIdentifier="1:/form_definitions/myform.yaml" />
I need to set the recipient with the teacher's email shown on the page. How can I do?
You can achieve that by writing a custom form finisher.
Add a hidden field to your form which holds the the ID of the teacher
Fetch that id in your form finisher and load the Teacher model by your repository
A (not complete) example of a form finisher, which loads recipient data from a custom model and sends the mail to this specific data:
class EmailToContactPersonFinisher extends EmailFinisher
{
/**
* Executes this finisher
* #see AbstractFinisher::execute()
*
* #throws FinisherException
*/
protected function executeInternal()
{
/** #var FormRuntime $formRuntime */
$formRuntime = $this->finisherContext->getFormRuntime();
if ($formRuntime->getResponse()->getRequest()) {
if ($formRuntime->getResponse()->getRequest()->hasArgument('contactPerson')) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** #var ContactPersonRepository $repository */
$contactPersonRepository = $objectManager->get(ContactPersonRepository::class);
/** #var ContactPerson $contactPerson */
$contactPerson = $contactPersonRepository->findByUid($formRuntime->getResponse()->getRequest()->getArgument('contactPerson'));
}
}
// override contactPerson related options
if ($contactPerson instanceof ContactPerson) {
if ($contactPerson->getEmail()) {
$recipientAddress = $contactPerson->getEmail();
}
}
$this->setOption('recipientAddress', $recipientAddress);
parent::executeInternal();
}
}
You can also have a look at the standard emailFinisher, which gives you quick idea on the architecture.
sysext/form/Classes/Domain/Finishers/EmailFinisher.php

Add own sys_category to record with extbase

I made the table of one of my models categorizable which works fine if I set categories in the Backend. If I try to add categories via a frontend form I always get the error:
Call to a member function attach() on null
and have no clue why that is so. Maybe anyone of you can help.
In controller I try to add as usual, find the category
$videoCat = $this->categoryRepository->findByUid(28);
and add it like that
$this->video->addTxVideoCat($videoCat);
That's where the error occurs.
Find below how I added the category to the model.
ext_tables.sql `tx_video_cat int(11) DEFAULT '0' NOT NULL,`
extended tca in TCA/Overrides/sys_template.php
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
'addvideo',
'tx_addvideo_domain_model_video',
// Do not use the default field name ("categories") for pages, tt_content, sys_file_metadata, which is already used
'tx_video_cat',
array(
// Set a custom label
'label' => 'LLL:EXT:addvideo/Resources/Private/Language/locallang.xlf:video_categories',
// This field should not be an exclude-field
'exclude' => FALSE,
// Override generic configuration, e.g. sort by title rather than by sorting
// string (keyword), see TCA reference for details
'l10n_mode' => 'exclude',
// list of keywords, see TCA reference for details
'l10n_display' => 'hideDiff',
)
);
Created extended category repository
namespace Pixelink\Addvideo\Domain\Repository;
class CategoryRepository extends \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository
Created extended model
namespace Pixelink\Addvideo\Domain\Model;
class Category extends \TYPO3\CMS\Extbase\Domain\Model\Category
{}
Category mapping in typoscript
plugin.tx_addvideo{
persistence {
classes{
Pixelink\Addvideo\Domain\Model\Category {
mapping {
tableName = sys_category
columns {
}
}
}
}
}
}
and in Model\Video.php I added the following
/**
* txVideoCat
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Pixelink\Addvideo\Domain\Model\Category>
*/
protected $txVideoCat = null;
/**
* Get categories
*
* #return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Pixelink\Addvideo\Domain\Model\Category>
*/
public function getTxVideoCat()
{
return $this->txVideoCat;
}
/**
* Set categories
*
* #param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $txVideoCat
*/
public function setTxVideoCat($txVideoCat)
{
$this->txVideoCat = $txVideoCat;
}
/**
* Add category to a post
*
* #param \Pixelink\Addvideo\Domain\Model\Category $txVideoCat
*/
public function addTxVideoCat(\Pixelink\Addvideo\Domain\Model\Category $txVideoCat)
{
$this->txVideoCat->attach($txVideoCat);
}
You should initialize your property in your Video model constructor:
public function __construct()
{
$this->txVideoCat = new ObjectStorage();
}
Your $this->txVideoCat is null. Use initiailizeObject() method to assign it:
public function initializeObject()
{
$this->txVideoCat = new ObjectStorage();
}

Call to undefined method getPosition() - Why and Fix

I made an extbase extension with a class Appointment with a property expertises und another one subExpertises of the same type.
This is how they look like in the Appointment class (subExpertises is the same):
/**
* expertises
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<...\Domain\Model\Expertise>
*/
protected $expertises = NULL;
/**
* Adds an expertise
*
* #param ...\Domain\Model\Expertise $expertise
* #return void
*/
public function addExpertise(...\Domain\Model\Expertise $expertise) {
$this->expertises->attach($expertise);
}
I get an error when executing this code in my controller after editing the appointment in a fluid form:
/**
*
* #param \Domain\Model\Appointment $appointment
* #return void
*/
public function bookAction(\Domain\Model\Appointment $appointment) {
//empty all expertises of appointment - then fill them with the selected from lawyer
$appointment->setExpertises(new \TYPO3\CMS\Extbase\Persistence\ObjectStorage());
$appointment->setSubExpertises(new \TYPO3\CMS\Extbase\Persistence\ObjectStorage());
//add all checked expertises of lawyer to appointment
foreach ($appointment->getLawyer()->getExpertises() as $expertise) {
if ($expertise->getChecked()) {
$appointment->addExpertise($expertise);
}
foreach ($expertise->getSubExpertises() as $subExpertise) {
if ($subExpertise->getChecked()) {
$appointment->addSubExpertise($subExpertise);
}
}
}
$this->appointmentRepository->update($appointment);
}
This is the error:
Fatal error: Call to undefined method \Domain\Model\Expertise::getPosition() in /var/www/typo3_src/typo3_src-6.2.25/typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php on line 453
Now it seems that TYPO3 thinks Expertise is of type ObjectStorage because it tries to call getPosition() but I have no clue why it does that and what I should change in order to successfully save my Appointment object with the new Expertises.
I tried debugging the appointment object, but I couldn't find the problem - it seems okay to me, it just shows that expertises und subExpertises have been modified.
Getter methods in Extbase aren't magic, you have to explicitly define them.
If you're dealing with a n:n-relation, you also need to initialize the Property as ObjectStorage in your model and configure it in the TCA.
/**
* Initialize all ObjectStorage properties.
*
* #return void
*/
protected function initStorageObjects() {
$this->yourProperty = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}

[Symfony][Form] Add validator/constraint to property only if it has changed

I've got the following scenario: I'm validating appointments and there's a custom validator, which tells the user if his choosen date is valid or not. It's not valid, if the date is already blocked by another entity. This works flawlessly on adding new entities.
Now I'd like to trigger the date validation on edit only if the date itself has changed. So just changing the title of the appointment should not validate the date.
My entity class:
use Doctrine\ORM\Mapping as ORM;
use Acme\Bundle\Validator\Constraints as AcmeAssert;
/**
* Appointment
*
* #ORM\Entity
* #AcmeAssert\DateIsValid
*/
class Appointment
{
/**
* #ORM\Column(name="title", type="string", length=255)
*
* #var string
*/
protected $title;
/**
* #ORM\Column(name="date", type="date")
*
* #var \DateTime
*/
protected $date;
}
The validator class (used as a service):
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the date of an appointment.
*/
class DateIsValidValidator extends ConstraintValidator
{
/**
* {#inheritdoc}
*/
public function validate($appointment, Constraint $constraint)
{
if (null === $date = $appointment->getDate()) {
return;
}
/* Do some magic to validate date */
if (!$valid) {
$this->context->addViolationAt('date', $constraint->message);
}
}
}
The corresponding Constraint class is set to target the entity class.
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class DateIsValid extends Constraint
{
public $message = 'The date is not valid!';
/**
* {#inheritdoc}
*/
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
/**
* {#inheritdoc}
*/
public function validatedBy()
{
return 'acme.validator.appointment.date';
}
}
Now I don't find a clean way to depend on a date change. I could simply track the old date in my entity, but that doesn't feel like a proper solution, if I'd like to implement more complex constraints. :[
Cheers
Since symfony 2.3 you can use Form Events to solve this problem. I added the change-check code to my FormType, by storing (and cloning) the original entity at the form creation.
Then added a POST_SUBMIT event listener to check if the fields were changed. The listener can add validation errors to your fields.
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Acme\Bundle\Entity\Appointment;
class AppointmentType extends AbstractType
{
private $originalAppointment;
public function __construct(Appointment $original)
{
// save the original entity
$this->originalAppointment = clone $original;
}
// ...
public function buildForm(FormBuilderInterface $builder, array $options)
{
// define your fields
$builder->addEventListener(FormEvents::POST_SUBMIT, [$this, 'dateCheckListener']);
}
public function dateCheckListener(FormEvent $event)
{
$appointment = $event->getData();
$form = $event->getForm();
// if no appointments exist, we can skip the check
if (empty($appointment) || empty($this->originalAppointment)) {
return;
}
if ($appointment->getDate() !== $this->originalAppointment->getDate()) {
// the dates changed, you can call your validator here
if ('dates are not valid') {
$form->get('date')->addError(new FormError('We have a problem.'));
}
}
}
}
In your controller, you can create this formType with the original appointment:
$appointment = $this->getYourAppointmentSomehow();
$form = $this->createForm(new AppointmentType($appointment), $appointment);
Maybe you will find this article useful, to check which property is changed. Everything is possible in symfony. You might end up writing entity listeners, listener resolvers and so on. Things can get ultra advanced.
http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.html
Pay attention to the setter method:
public function setData($data)
{
if ($data != $this->data) {
$this->_onPropertyChanged('data', $this->data, $data);
$this->data = $data;
}
}
Do you see the trick?:)
I would also use !== operator to also check variable type.
You can also simplify things. You dont need to call _onPropertyChanged, but call the function, which will set a property 'dateChanged' to true. Then use method:
public function getGroupSequence()
{
if($this->dateChanged)
{
return ['date_check'];
}
else
{
return false;
}
}
And also tell your class that it implements GroupSequenceProviderInterface.
You can then use the validation group in your validation.yml for example.
maybe you want to try it with a preUpdate-Listener instead of a custom validation constraint?
Section 10.5.4 in the doctrine documentation gives an example of a validation listener "ValidCreditCardListener".
i know this will not work for automagic form validation, but i think it's the fastest way atm.
edit:
another option could be to use #UniqueEntiy constraint for the date field of your Appointment class. this will not break form validation but will cause an additional database query (as far as i know)

Extbase stores empty values in database

I am trying to create an object, but the values are not stored into the database. This is done on an "index"-action because the plugin is inserted via TypoScript and actually does not create output. So there is no object given when calling the action, that's why I am creating it by myself.
$stat = new Tx_MyExt_Domain_Model_Stat;
$stat->setSubscriberId($_COOKIE['statid']);
$stat->setDomain($_SERVER['HTTP_HOST']);
$stat->setRequestUri($_SERVER['REQUEST_URI']);
$this->statRepository = t3lib_div::makeInstance('Tx_myExt_Domain_Repository_StatRepository');
$this->statRepository->add($stat);
doing a var_dump($stat) gives the following:
object(Tx_MyExt_Domain_Model_Stat)#191 (9) {
["subscriber_id":protected]=>
string(1) "2"
["domain":protected]=>
string(22) "test.localhost.example"
["request_uri":protected]=>
string(26) "/testpage/index.php?id=2"
["uid":protected]=>
NULL
["_localizedUid":protected]=>
NULL
["_languageUid":protected]=>
NULL
["pid":protected]=>
NULL
["_isClone":"Tx_Extbase_DomainObject_AbstractDomainObject":private]=>
bool(false)
["_cleanProperties":"Tx_Extbase_DomainObject_AbstractDomainObject":private]=>
NULL
}
So this looks like the values are assigned properly. But when looking into the database, I get this:
uid pid subscriber_id domain request_uri crdate
13 0 0 NULL NULL 1328176026
Repository:
class Tx_MyExt_Domain_Repository_StatRepository extends Tx_Extbase_Persistence_Repository
{}
Model:
class Tx_MyExt_Domain_Model_Stat extends Tx_Extbase_DomainObject_AbstractEntity
{
/**
* #var int
* #dontvalidate
*/
protected $subscriber_id = 0;
/**
* #var string
* #dontvalidate
*/
protected $domain = '';
/**
* #var string
* #dontvalidate
*/
protected $request_uri = '';
/**
* #param int $susbcriber_id Subscriber id
* #return void
*/
public function setSubscriberId($subscriber_id)
{
$this->subscriber_id = $subscriber_id;
}
/**
* #return int Susbcriber id
*/
public function getSubscriberId()
{
return $this->subscriber_id;
}
/**
* #param string $domain Domain
* #return void
*/
public function setDomain($domain)
{
$this->domain = $domain;
}
/**
* #return string Domain
*/
public function getDomain()
{
return $this->domain;
}
/**
* #param string $request_uri Request URI
* #return void
*/
public function setRequestUri($request_uri)
{
$this->request_uri = $request_uri;
}
/**
* #return string Request URI
*/
public function getRequestUri()
{
return $this->request_uri;
}
}
Can someone give me advise what may be wrong here?
Debugged through the whole extbase process. It seems that in typo3/sysext/extbase/Classes/Persistence/Backend.php, the attributes are skipped on this line:
if (!$dataMap->isPersistableProperty($propertyName) || $this->propertyValueIsLazyLoaded($propertyValue)) continue;
This because $dataMap->isPersistableProperty($propertyName) doesn't return something. Investigating in typo3/sysext/extbase/Classes/Persistence/Mapper, there is:
/**
* Returns TRUE if the property is persistable (configured in $TCA)
*
* #param string $propertyName The property name
* #return boolean TRUE if the property is persistable (configured in $TCA)
*/
public function isPersistableProperty($propertyName) {
return isset($this->columnMaps[$propertyName]);
}
So the solution is quite simple: create a valid TCA. I didn't had one (or a too minimalistic) since the table i am using is not going to be displayed in the backend.
While misconfiguration of TCA might be causing the problem, there might be others. For example, extbase does not like it when you are defining unique keys and fails silently.
Having struggeld with the problems in multiple projects, I am now using the following debugging routine for projects made with the extension builder
Remove your own additions from the table related classes and as well from typoscript. This has to be done for ext_tables.php, ext_tables.sql, all files in Configuration/TCA and Configuration/Typoscript if you have changed their state in Configuration/ExtensionBuilder/settings.yaml to merge or keep.
Check if your application now does save. If not, report a detailed bug report to exentension builder.
Normally your application should save now. Readd recursively the changes you've made until you find the error. Start with ext_tables.sql (don't forget you have to remove and readd the database every time), go on with ext_tables.php, Configuration/TCA/* and end with Configuration/Typoscript (it's my personal experience that these order is the fastest)
Report your stuff to the extbase team and add it to this thread (as it's the first google hit when you experience the error)