Donatas Veikutis
Donatas Veikutis

Reputation: 1004

symfony2 doctrine how to update OneToMany entities after persist?

I get thees two entities. This is first which have OneToMany releation:

class MarketMain
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToMany(targetEntity="\Acme\CMSBundle\Entity\MarketLanguage", mappedBy="marketMain", indexBy="langId", cascade="all", orphanRemoval=true, fetch="EXTRA_LAZY")
     */
    private $marketLanguage;
}

This is second which have reverst ManyToOne releation:

class MarketLanguage
{
    /**
     * @var integer
     *
     * @ORM\Column(name="market_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $marketId = 0;

    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(name="lang_id", type="integer", nullable=false)
     */
    private $langId = 1;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=200, nullable=true)
     */
    private $name;

    /**
     * @var \Acme\CMSBundle\Entity\MarketMain
     *
     * @ORM\ManyToOne(targetEntity="\Acme\CMSBundle\Entity\MarketMain", inversedBy="marketLanguage")
     * @ORM\JoinColumn(name="market_id", referencedColumnName="id")
     */
    private $marketMain;
}

And I want to save entity like this:

$em = $this->getDoctrine()->getManager();

$marketMain = new MarketMain();
$marketLanguage = new ArrayCollection();
$marketLanguage->set(1,new MarketLanguage());
$marketLanguage->set(2,new MarketLanguage());
$marketLanguage->set(3,new MarketLanguage());
$marketMain->setMarketLanguage($marketLanguage);

foreach ($marketMain->getMarketLanguage() as $market_language)
{
$market_language->setMarketMain($marketMain);
}
$em->persist($marketMain);
$em->flush();

foreach ($marketMain->getMarketLanguage() as $market_language)
{
   $market_language->setName("Default name for MarketMain entity id:".$marketMain->getId());
}
$em->flush();

But after second flush my MarketLanguage entities do not updates, in doctrine query log I see that MarketLanguage entities do not have relation after persist with MarketMain. And please do not say that I have to set MarketLanguage names before persist because I have to add flushed ID to that name.

This is query (from doctrine logs) that executes update in second flush:

"START TRANSACTION" [] []
UPDATE market_language SET name = ? WHERE market_id = ? AND lang_id = ? ["Default name for MarketMain entity 15",0,"1"] []
"COMMIT" [] []

Upvotes: 1

Views: 1905

Answers (2)

qtuan
qtuan

Reputation: 687

Look like the problem is due the MarketLanguage ID is fixed, because of:

private $marketId = 0;

Please try again without = 0

EDIT: Since you use "NONE" ID generation strategy, you must manually assign ID for your MarketLanguage entity. See doc

Upvotes: 0

szapio
szapio

Reputation: 1008

$marketMain = new MarketMain(); You are creating new instance so

$marketMain->getMarketLanguage() 

will return empty array, maybe you should query from database all MarketLanguages and then assign them to MarketMain ?

$langs = $em->getRepositiory('Bundle:MarketLanguage')->findAll()
foreach ($langs as $market_language)
  {
     $market_language->setMarketMain($marketMain);
  }
$em->persist($marketMain);
$em->flush();

EDIT:

So try this:

$ml1 = new MarketLanguage();
$ml2 = new MarketLanguage();
$ml3 = new MarketLanguage();
$marketLanguage->set(1,$ml1);
$marketLanguage->set(2,$ml2);
$marketLanguage->set(3,$ml3);
$marketMain->setMarketLanguage($marketLanguage);

foreach ($marketMain->getMarketLanguage() as $market_language)
{
$market_language->setMarketMain($marketMain);
}
$em->persist($marketMain);

$em->persist($ml1);
$em->persist($ml2);
$em->persist($ml3);
$em->flush();

Upvotes: 0

Related Questions