Reputation: 967
I have got two (2) classes:
<?php
class Person extends BaseDto
{
/**
* @var array|PostalAddress
*/
protected $postalAddresses = array();
/**
* @param array|PostalAddress $postalAddresses
*/
public function setPostalAddresses($postalAddresses)
{
$this->postalAddresses = $postalAddresses;
}
/**
* @return array|PostalAddress[]
*/
public function getPostalAddresses()
{
return $this->postalAddresses;
}
}
<?php
class PostalAddress
{
/**
* @var string $privatePersonFirstName
*/
protected $privatePersonFirstName;
/**
* @var string $privatePersonName
*/
protected $privatePersonName;
/**
* @return string
*/
public function getPrivatePersonFirstName()
{
return $this->privatePersonFirstName;
}
/**
* @param string $privatePersonFirstName
*/
public function setPrivatePersonFirstName($privatePersonFirstName)
{
$this->privatePersonFirstName = $privatePersonFirstName;
}
/**
* @return string
*/
public function getPrivatePersonName()
{
return $this->privatePersonName;
}
/**
* @param string $privatePersonName
*/
public function setPrivatePersonName($privatePersonName)
{
$this->privatePersonName = $privatePersonName;
}
}
In the controller PostalAddressConroller
I have got an action which creates the form to edit a single address.
I would like to make some fields editable only if certain conditions are met. Example: The organization fields on the address are only editable in case the person is of type private person and the address is of type employer.
To implement such a condition check, I would like to create a method on the PostalAddress
model. But for this, It would require to have a reference back to the parent object inside the controller.
I would like to avoid to put all the logic inside the templates to keep the templates clean and easy to understand.
Is there support on extbase level for such back references?
In case I have to implement such a back reference myself: How do I prevent circular references in general (for example on object serialization)?
Upvotes: 0
Views: 1018
Reputation: 6379
I would handle the problem differently. This is no controller job imho. This is definetly a template/view job. I'd use if conditions in the template to show the correct layout (field editable or not). Afterwards you have to make sure that nobody can just make the fields editable via developer tools for example.
That would be achievable by adding conditions in the backend logic, for example:
if($model->isAllowedProperty) { AddFieldToResultArrOrSimilar() }
Upvotes: 1
Reputation: 1054
As I understand the MVC pattern, the model should only carry data, no logic and no dependencies of any kind. So to solve you problem I would use two different model classes, based on the same table, carying only those properites and validation metadata which applies to that particular model.
Imagine those two models below:
namespace Acme\MyPlugin\Domain\Model;
class PostalAddressPrivate
{
/**
* @var string $privatePersonFirstName
*/
protected $privatePersonFirstName;
/**
* @var string $privatePersonName
*/
protected $privatePersonName;
[...]
}
namespace Acme\MyPlugin\Domain\Model;
class PostalAddressCommercial
{
/**
* @var string $privatePersonFirstName
*/
protected $companyName;
[...]
}
Now you must tell the persistence layer, that those models goes to the same table. You do this in typoscript setup for that plugin.
plugin.tx_myplugin {
persistence {
classes {
Acme\MyPlugin\Domain\Model\PostalAddressPrivate {
mapping {
tableName = tx_myplugin_domain_model_postal_address
}
}
Acme\MyPlugin\Domain\Model\PostalAddressCommercial {
mapping {
tableName = tx_myplugin_domain_model_postal_address
}
}
}
}
Now you can transfer the logic into the controller and decide there which model to use. You can extend this simple case using a common Interface or abstract class, etc.
This "choose the right model" logic in the controller may be a little bit tricky at times. In general you will need to place some code dealing with the extbase "property mapper" inside the appropriate "initializeXxxAction" method. At the begining I was inspired by this article in german (for an older version extbase!): https://jweiland.net/typo3/codebeispiele/extension-programmierung/extbase-dynamische-validierung.html Hope google translate will give you some hints to solve upcomming problems.
On top you can assist the server based validation and processing by some frontend work. E.g. JavaScript tricks to enable or disable certain formular fields depending on the private/commercial status chosen. You can as well tune the fluid templates to render/not render certain parts depending of the model variant used in controller.
Upvotes: 0