I have an ExtBase extension with a Model with a 1:n relation in it. E.G. so:
class School extends AbstractEntity {
/**
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Pupil>
* #copy ignore
*/
protected $pupils = NULL;
}
If I clone a school in the Typo3 backend, all related pupils are also cloned. How can I prevent the model from cloning the related objects (it should leave empty the pupils object)? The #copy ignore annotations doesn't help.
Related
I have an events extension (for TYPO3 9 LTS and 10 LTS), say MyVendor\MyEvents and a Locations extension, say MyVendor\MyLocations.
The Model MyVendor\MyEvents\Domain\Model\Events has a property eventLocation which is defined to be an object of MyVendor\MyLocations\Domain\Model\Locations.
Now I want to make the relation to MyVendor\MyLocations\Domain\Model\Locations optional. I have found a way for the TCA to show a different form field in the backend depending on the MyLocations extension being installed. But I have no idea how to make all the type definitions in the Events model conditional. They are crucial for the extension to work:
namespace MyVendor\MyEvents\Domain\Model
class Events extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* #var \MyVendor\MyLocations\Domain\Model\Locations
*/
protected $eventLocation = NULL;
/**
* #return \MyVendor\MyLocations\Domain\Model\Locations $eventLocation
*/
public function getEventLocation()
{
return $this->eventLocation;
}
/**
* #param \MyVendor\MyLocations\Domain\Model\Locations $eventLocation
* #return void
*/
public function setEventLocation(\MyVendor\MyLocations\Domain\Model\Locations $eventLocation)
{
$this->eventLocation = $eventLocation;
}
}
In case MyVendor\MyLocations is loaded it needs to be defined as above, in case it isn’t loaded it should be just an integer.
In the TCA I am using if (TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('my_locations')) for showing a different field in the backend form for an event.
The Locations Model is in a separate extension because I am using it in a third extension as well.
In your events extension you could setup a repository for locations. Then you can map this repository to your location extensions model via TypoScript.
I'm trying to use a virtual domain model property in TYPO3 9.5.x that doesn't have a database field representation but I can't get it to work.
My model looks like this
class Project extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
/**
* participants
*
* #var string
*/
protected $participants;
...
/**
* Returns the participants
*
* #return string $participants
*/
public function getParticipants()
{
$this->participants = "foo";
return $this->participants;
}
}
I do see the property when I debug the model but it's always null as if it doesn't even recognise the getter method getParticipants().
Any idea what I might be doing wrong?
Already added a database field to ext_tables.sql and the TCA, but it didn't seem to make a difference.
The property is null because that's the state when the Extbase debugger inspects it. Notice that the Extbase debugger knows nothing about getters and also does not call them.
So if you want to initialize your property you must do this at the declaration time:
protected $participants = 'foo';
You can debug this property by simpy accessing it.
In Fluid, if you use <f:debug>{myModel}</f:debug>, you will see NULL for your property.
But if you directly use <f:debug>{myModel.participants}</f:debug>, you will see 'foo'.
I am trying to create a user management module. I would like to get all FE users.
This is my Controller:
/**
* #var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
* #inject
*/
protected $feUserRepository;
then I use:
$users = $this->feUserRepository->findAll();
$this->view->assign('users', $users);
but all I get is an empty object.
EDIT:
for some reason
$this->feUserRepository->findByUId(1);
does work but findAll() not...
This is because extbase will silently disable the respectStoragePage setting on the querySettings for a findByUid($uid) call.
So, you have two options:
Provide the correct storage pid in the TypoScript configuration of your plugin (plugin.tx_myextension.persistence.storagePid). This way, you will find every frontenduser that is stored on the given page.
You could implement your own FrontendUserRepository that extends the repository from extbase but disables the respectStoragePage for all calls (this way you'll get every frontendUser regardless of the page the record is stored on). Here is how you do it:
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository as ExtbaseFrontendUserRepository;
class FrontendUserRepository extends ExtbaseFrontendUserRepository
{
/**
* Disable respecting of a storage pid within queries globally.
*/
public function initializeObject()
{
$defaultQuerySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings:class);
$defaultQuerySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($defaultQuerySettings);
}
}
In your Controller you then inject your FrontendUserRepository. Then you should do the same for the FrontendUser Model and tell extbase afterwards that you are using the fe_users table for your Model:
config.tx_extbase {
persistence {
classes {
Vendor\MyExtension\Domain\Model\FrontendUser {
mapping {
tableName = fe_users
}
}
}
}
}
I have a Mongo database to which I am connected through the following document classes in Symfony, using Doctrine ODM (I am only showing the relevant properties).
/**
* #MongoDB\Document
* #MongoDB\InheritanceType("COLLECTION_PER_CLASS")
*/
class Feature {
/** #MongoDB\EmbedOne(targetDocument="FeatureProperties") */
protected $properties;
}
/** #MongoDB\Document */
class City extends Feature {}
/** #MongoDB\EmbeddedDocument */
class FeatureProperties {
/** #MongoDB\String */
protected $name;
}
I am trying to clone a City in my controller, including its properties, essentially its properties.name.
To do this, I get the city using QueryBuilder and call it $city_origin.
Then, here's how I clone it:
$new_city = clone $city_origin;
$city_name = $city_origin->getProperties()->getName();
// $city_name .= ' ';
$documentManager->detach($city_origin);
$new_city->setId('_' . $city_origin->getId());
$new_city->getProperties()->setName($city_name);
$documentManager->persist($new_city);
$documentManager->flush();
The new document is created, but properties and properties.name do not exist.
Now the strange thing is, if I uncomment the line where I modify the $city_name, then the properties.name is created with the modified value.
If instead of fetching the $city_name from $city_origin I input its value manually (as a string), the attribute is not created either. If I give the name of another City in my collection, there is no issue.
Ideally, I would like to clone the entire document with its embedded documents: how can I do that?
When you clone a object, you get a copy of this object whit the same reference of the embedded object.
When you do :
$documentManager->detach($city_origin);
Doctrine will detach all the object related to $city_origin because of your entity definition :
cascade=DETACH or cascade=ALL
http://docs.doctrine-project.org/en/2.0.x/reference/working-with-objects.html#detaching-entities
You need to define the clone function in the city class and clone all the object you need.
http://www.php.net/manual/en/language.oop5.cloning.php
I used Symfony2 and Doctrine MongoDBBundle and I have simple Single Collection Inheritance classes. How can I know what type of document it is in a twig template? For example the base class is Entity and extended by User and Organization, in listing those in a twig template I'd like to know what type of entity it is (i.e. whether it's a User or an Organization). I wonder if it's possible to get the value of the DiscriminatorField of the document.
/**
* #MongoDB\Document(collection="entity")
* #MongoDB\InheritanceType("SINGLE_COLLECTION")
* #MongoDB\DiscriminatorField(fieldName="type")
* #MongoDB\DiscriminatorMap({"user"="User", "shop"="Shop"})
*/
class Entity
{
/**
* #MongoDB\Id
*/
protected $id;
protected $entityType;
public function getEntityType()
{
return $this->entityType;
}
}