I use the last version of EasyAdmin and my add and remove functions are ignored when I submit the form:
Ambiance entity:
/**
* #ORM\OneToMany(targetEntity="Vehicule", mappedBy="ambiance")
*/
protected Collection $vehicules;
public function __construct()
{
$this->vehicules = new ArrayCollection();
}
public function addVehicule(Vehicule $vehicule): self
{
if (!$this->vehicules->contains($vehicule)) {
$this->vehicules[] = $vehicule;
$vehicule->setAmbiance($this);
}
return $this;
}
public function removeVehicule(Vehicule $vehicule): void
{
if (!$this->vehicules->contains($vehicule)) {
return;
}
$this->vehicules->removeElement($vehicule);
}
public function getVehicules()
{
return $this->vehicules;
}
public function setVehicules($vehicules): void
{
$this->vehicules = $vehicules;
}
Yet my Doctrine mapping is valid..
My EasyAdmin form in AmbianceCrudController.php:
'vehicules' => AssociationField::new('vehicules', 'VĂ©hicules'),
It generates a multiple select2 but when I add vehicles and submit my form, no data is inserted.
Replace
AssociationField::new('vehicules')
by
AssociationField::new('vehicules', 'VĂ©hicules')->setFormTypeOption('by_reference', false)
I'm strugling with the same problem, but I found already bits of the puzzle that could be helpful:
in my experience, it seems that the owning side of the association (the one that has the foreign key in the database) results in an edit form that persists the change.
the other side ignores the change completely
the setters and getters, and addABC and removeABC methods seem indeed to be skipped for reasons that are unclear to me as well.
I hope you can find a solution with the hint that the crudcontroller other side in the relationship, I think Vehicules in your case.
Related
This is a question about the event system in Doctrine (within a Symfony project).
I have two classes, User and Visit, that are associated via a many-to-many relationship. I.e. a user can have many visits and a visit can have many users (that attend the visit).
class Visit
{
#[ORM\Column]
protected string $Date;
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: "Visits")]
#[ORM\JoinTable(name: "users_visits")]
protected Collection $Users;
public function __construct()
{
$this->Users = new ArrayCollection();
}
//... other properties and methods omitted
}
class User
{
#[ORM\Column]
protected string $Name;
#[ORM\ManyToMany(targetEntity: Visit::class, inversedBy: "Users")]
#[ORM\JoinTable(name: "users_visits")]
protected Collection $Visits;
public function __construct()
{
$this->Visits = new ArrayCollection();
}
//... other properties and methods omitted
}
I also have an UpdateSubscriber that is supposed to record certain inserts, updates or removals, in a separate sql-table to create an overview over all relevant changes later on.
class UpdateSubscriber implements EventSubscriberInterface
{
public function __construct(private LoggerInterface $logger)
{
}
public function getSubscribedEvents(): array
{
return [
Events::preUpdate,
Events::postPersist,
Events::postRemove,
Events::postFlush
];
}
public function preUpdate(PreUpdateEventArgs $args): void
{
$this->logger->debug('Something has been updated');
if($args->hasChangedField('Date')){
$this->logger->debug('The date has been changed.');
}
if($args->hasChangedField('Users')){
$this->logger->debug('It was the Users field');
}
}
// ... other methods emitted
I have gotten this system to work, but when I run this test code
$visitRepo = $this->om->getRepository(Visit::class);
$userRepo = $this->om->getRepository(User::class);
// you can assume that visit 7 and user 8 already exist in the database
$v = $visitRepo->find(7);
$u = $userRepo->find(8);
$v->setDate('2022-01-05');
$this->om->flush();
$v->addUser($u);
$this->om->flush();
The test code works without errors and I can see a new row in the sql-table "users_visits". I can also see the date for visit 7 has been changed to 2022-01-05.
BUT: Checking the log I can only find
Something has been updated.
The date has been changed.
Something has been updated.
There is no "It was the Users field". Using my debugging tools I can see that the EntityChangeSet is empty during preUpdate() during the $v->addUser($u), which is weird and unexpected.
I have extensively been reading the docs for the event PreUpdate but there is no mentioning of why changes to associated collections are not shown in the EntityChangeSet or how I could track those changes in an EventSubscriber.
Do I have to go via the rather cumbersome UnitOfWork during the onFlushEvent? Or this there something I have been missing?
In case I myself or somebody else reads this, my pretty dirty solution was to collect all currently updated collections and then check if the property ("Users" in this case) matched on of these.
$updatedCollectionNames = array_map(
function (PersistentCollection $update) {
return $update->getMapping()['fieldName'];
},
$args->getEntityManager()->getUnitOfWork()->getScheduledCollectionUpdates()
);
// and later
if (in_array('Users', $updatedCollectionNames, true)){
$this->logger->debug('It was the Users field');
}
This seems dirty and contrived, but it will do for now until somebody has a better proposal.
I have a many-to-many relationship between users (the owning side) and user groups, and am having issues using doctrine module's hydrator to create a new user group.
When I create a new user group and hydrate, persist, and flush it, the records change in the database, but the entity variable itself representing the user group doesn't end up with any users in it post-hydration.
Context: We have a REST controller route that we use to create a new user group via POST. It accepts parameters to initialize it with some users via hydration. This operation successfully updates the database, but its response is incorrect. It is supposed to extract the data from the now-persistent entity and echo it back to the client. However, it fails to extract any users, so the response incorrectly returns as an empty group. Not using the hydrator's extract method and instead using more basic doctrine commands fails too--it seems like the entity variable itself is just not kept up to date after being persisted.
So my question really is: why is the hydrator not extracting users? If we've messed up the owner/inverse assocation, why is it working at all (i.e. persisting the users to the database but not to the entity).
Here is the relevant code, probably only the first two blocks are needed.
public function create($data) {
...
$hydrator = $this->getHydrator();
$em = $this->getEntityManager();
$entity = $this->getEntity();
$entity = $hydrator->hydrate($data, $entity);
// Persist the newly created entity
$em->persist($entity);
// Flush the changes to the database
$em->flush();
return $this->createResponse(
201,
true,
'Created item',
$this->getHydrator()->extract($entity)
);
Here is are the setters and getters the hydrator is using:
... more fields...
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="groups")
*/
protected $users;
...
/**
* Getter for users
*
* #return mixed
*/
public function getUsers() {
return $this->users;
}
public function addUsers(Collection $users) {
foreach ($users as $user) {
$user->addGroups(new ArrayCollection(array($this)));
}
}
I think the above is the only relevant code, but I'll include some more in case I'm wrong. Here is the getHydrator method:
public function getHydrator() {
if(null === $this->hydrator) {
$hydrator = new DoctrineObject($this->getEntityManager(), $this->getEntityName());
// Use Entity metadata to add extraction stategies for associated fields
$metadata = $this->em->getClassMetadata($this->getEntityName());
foreach ($metadata->associationMappings as $field => $mapping) {
// Use our custom extraction strategies for single and collection valued associations
if($metadata->isSingleValuedAssociation($field)) {
$hydrator->addStrategy($field, new RestfulExtractionStrategy());
}
else if($metadata->isCollectionValuedAssociation($field)) {
$hydrator->addStrategy($field, new RestfulExtractionCollectionStrategy());
}
}
$this->hydrator = $hydrator;
}
return $this->hydrator;
}
Here is the RestfulExtractionCollectionStrategy (the other strategy isn't being used here, I have verified this).
namespace Puma\Controller;
use DoctrineModule\Stdlib\Hydrator\Strategy\AllowRemoveByValue;
use Doctrine\Common\Collections\Collection;
/**
* You can use this strategy with fields that are collections,
* e.g. one to many. You need to use the RestfulExtractionStrategy
* if you want to get extract entities from a singleton field, e.g. manyToOne.
**/
class RestfulExtractionCollectionStrategy extends AllowRemoveByValue
{
public function extract($value)
{
if ($value instanceof Collection) {
$return = array();
foreach ($value as $entity) {
if(method_exists($entity, 'getId')){
$return[] = $entity->getId();
}
else {
$return[] = $entity;
}
}
return $return;
}
return $value;
}
}
I am not quite familiar with hydration, etc., so your code looks kind of strange to me and I cannot guarantee this will work, but have you tried to refresh the entity after flushing (i.e. $em->refresh($entity)) and maybe return the entity instead of $this->getHydrator()->extract($entity)?
I think I've finally solved it--I added a line to the "setter" method, addUsers which manually updates the users property of the group after updating the related users. I would be a bit surprised if this was best practice, though. I had thought that updating the owning side (the users) would automatically update the inverse side (the user group). Perhaps I was wrong. If anyone else has a better idea I'll gladly give the answer credit to them.
public function addUsers(Collection $users) {
foreach ($users as $user) {
$user->addGroups(new ArrayCollection(array($this)));
// This is the new line (updating the usergroup property manually)
$this->users->add($user);
}
}
We have two models
class Base extends AbstractModel
{
public $id;
public $name;
public function tableName()
{
return 'table_base';
}
}
class ExtendBase extends AbstractModel
{
public $id;
public $baseID;
public function tableName()
{
return 'table_base_extend';
}
}
I won't describe all, will describe only main things.
So, now I want create form that will combine this forms. And want in action do something like this
public function actionSome ()
{
$oBase = new Base();
$aExtendBase = array(); // important this is array
for ($i = 1; $i <= 3; $i++) {
$aExtendBase[] = new Extend(); // fill in array, pre-create for form
}
if ($this->isPost()) { // protected custom method
if (isset($_POST['Base'], $_POST['ExtendBase'])) // here should be specific checker because we have several ExtendBase instances in form
{
// save Base model
// save each ExtendBase model
}
}
$this->render('formView', array('oBase' => $oBase, 'aExtendBase' => $aExtendBaes))
}
So question is
How to create such 'combined' form in view;
How to get all forms data (I mean each) after POST action;
See the solution on the Yii WIKI
http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/
You can create a model (CFormModel) which does not represent an ActiveRecord just for a single form.
I think it would be the best way for you to proceed (assuming I've understood your problem correctly).
If I were you, I would create a Model to represent the rules for this form only (like I said, an instance of CFormModel, not ActiveRecord) and then, in your controller, take the elements that got submitted and manually create and/or update objects for both classes.
I use the following code for my many-to-many relation in symfony2 (doctrine)
Entity:
/**
* #ORM\ManyToMany(targetEntity="BizTV\ContainerManagementBundle\Entity\Container", inversedBy="videosToSync")
* #ORM\JoinTable(name="syncSchema")
*/
private $syncSchema;
public function __construct()
{
$this->syncSchema = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addSyncSchema(\BizTV\ContainerManagementBundle\Entity\Container $syncSchema)
{
$this->syncSchema[] = $syncSchema;
}
Controller:
$entity->addSyncSchema($container);
$em->flush();
Now, how can I use this to remove a relation? Do I need to add a method to my entity like removeSyncSchema()? What would that look like?
You're looking for the ArrayCollection::removeElement method here.
public function removeSchema(SchemaInterface $schema)
{
$this->schemata->removeElement($schema)
return $this;
}
tip:
You can use ArrayCollection::add to add elements to an existing collection. OOP.
In some cases you may also want to check wether already contains the element before adding it.
public function addSchema(SchemaInterface $schema)
{
if (!$this->schemata->contains($schema)) {
$this->schemata->add($schema);
}
return $this;
}
I have an entity that needs to return an instance of another entity - the stored record or a new one if one has not been stored.
<?php
/**
* #Entity
*/
class User {
/**
* #OneToOne(targetEntity="Basket")
* #JoinColumn(nullable=true)
*/
protected $activeBasket;
public function getActiveBasket() {
if (!isset($this->activeBasket)) {
$this->activeBasket = new Basket($this);
// $em->persist($this->activeBasket);
// $em->flush();
}
return $this->activeBasket;
}
}
My problem is that I don't have an EntityManager to use to persist this new Basket (and obviously don't want one in the Model). I'm not sure as to the best way to do this. I do want to be able to call $user->getActiveBasket() and retrieve a basket, whether it's previously created or a new one.
It feels like this should be a common problem, or that there's a better way to structure this (I'm hoping there's a way to hook one in in an EntityRepository or something). How can I do this?
I wouldn't return a new Basket() when it is null or not set. The model (entity) should only serve for setting and getting its properties.
The logic when $user->getActiveBasket() returned null should be elsewhere - in controller or in entity's reporitory object...
So I would move public function getActiveBasket() { ... } into just public function getActiveBasket() { return $this->activeBasket; } and somewhere in the controller I would do:
$if(!$user->getActiveBasket()) {
$user->setActiveBasket(new Basket($user));
$this->_em->persist($user);
$this->_em->flush();
}
// now do something with that activeBasket...