Skip to content

Instantly share code, notes, and snippets.

@hpatoio
Created March 4, 2013 09:48
Show Gist options
  • Select an option

  • Save hpatoio/5081140 to your computer and use it in GitHub Desktop.

Select an option

Save hpatoio/5081140 to your computer and use it in GitHub Desktop.
Right way to add a Doctrine2 relation
<?php
namespace Acme\DemoBundle\DataFixtures\ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Acme\DemoBundle\Entity\Team;
use Acme\DemoBundle\Entity\Player;
class LoadDemoDataOI implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$players_source = array(
array("name" => "Giuseppe Giannini"),
array("name" => "Marco Van Basten"),
array("name" => "George Best")
);
$_team = new Team();
$_team->setName("Dream team B");
$manager->persist($_team);
foreach ($players_source as $player_data) {
$_player = new Player();
$_player->setName($player_data['name']);
$_player->setPlayfor($_team); # This is a change on the owning side in our relation
$manager->persist($_player);
}
$manager->flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment