-2

I have problem with update entity with relations (one to many, many to one). I trying to add some new element to ArrayCollection when update, but nothing to do. Here is my code of create and add relation:

$auctionPhoto = new AuctionPhoto();
$auctionPhoto->setAuction($auction);
$auctionPhoto->setPath($path);
$auction->getPhotos()->add($auctionPhoto);

All is running by doctrine entity listener (preUptade). The same code is do when I create entity (prePersist), but then is works fine.

I debug this and before persist I have in Auction object right relations, but nothing save to database.

Jacek
  • 33
  • 2
  • 8

3 Answers3

2

Why do you do $auction->getPhotos()->add($auctionPhoto) ?

You should have a method addPhoto() or addAuctionPhoto() in your Auction entity, and use it like this:

$auction->addPhoto($auctionPhoto) or $auction->addAuctionPhoto($auctionPhoto)

EDIT:

Maybe your entity Auction is not the owner of the relation between the two entities, then you need to add $auctionPhoto->setAuction($auction), or if it's ManyToMany relation, then add $auctionPhoto->addAuction($auction)

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
progg
  • 294
  • 1
  • 4
  • I don't have any. How should looks like this method? – Jacek Nov 22 '16 at 08:39
  • inside the method, you should have `$this->photos->add($auctionPhoto)`. The difference with the two method is that `$auction->getPhotos()` return an array with the same values as photos, not the reference of you photos attribut – progg Nov 22 '16 at 08:43
  • Next time, generate your entities with `php bin/console doctrine:generate:entities AppBundle/Entity/YourEntity` Doc: http://symfony.com/doc/current/doctrine.html#generating-getters-and-setters – progg Nov 22 '16 at 08:44
  • I'm not sure. Before I tried to create new ArrayCollection, add photos to this and after that use setPhotos on Auction - and doesn't work. – Jacek Nov 22 '16 at 10:52
  • You can run `php bin/console doctrine:generate:entities AppBundle/Entity/YourEntity` and see what methods Symfony will create for you. You will have your current entity named `~YourEntity` and it will generate you a new entity file. Or maybe your entity `Auction` is not the owner of the relation between the two entities, they you need to add `$auctionPhoto->setAuction($auction)`, or if it's ManyToMany, do `$auctionPhoto->addAuction($auction)` – progg Nov 23 '16 at 15:42
1

Replace $auction->getPhotos()->add($auctionPhoto); with $auction->addPhoto($auctionPhoto);.

In your Auction entity, define the new method

// Auction.php
public function addPhoto(AuctionPhoto $thePhoto)
{
    $this->photos[] = $thePhoto; // Add the photo to the object
    $thePhoto->setAuction($this); // AuctionPhoto entity need to know about the reference

    return $this; // Just for method chaining
}

(I assume $photos is your ArrayCollection which contains auction's photos)

Basically what you missed is to give a reference back to your entity: $thePhoto->setAuction($this);

lolmx
  • 521
  • 6
  • 18
0

Are you saying nothing is in the database before you run this:

$em->persist($auction);
$em->flush();

If so, that is correct functionality. You need to persist then flush, then data is stored.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • No, this is not this. Other field of auction like title saved. And as I say, this is the same code as in create action but create works. – Jacek Nov 22 '16 at 08:23