Created
March 4, 2013 09:48
-
-
Save hpatoio/5081140 to your computer and use it in GitHub Desktop.
Right way to add a Doctrine2 relation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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