Sulu\Bundle\ContactBundle\Entity\Country::setId PHP Method

setId() public method

To force id = 1 in load fixtures.
public setId ( integer $id )
$id integer
    public function setId($id)
    {
        $this->id = $id;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     // force id = 1
     $metadata = $manager->getClassMetaData(get_class(new Country()));
     $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
     $i = 1;
     $file = dirname(__FILE__) . '/../countries.xml';
     $doc = new \DOMDocument();
     $doc->load($file);
     $xpath = new \DOMXpath($doc);
     $elements = $xpath->query('/Countries/Country');
     if (!is_null($elements)) {
         /** @var $element DOMNode */
         foreach ($elements as $element) {
             $country = new Country();
             $country->setId($i);
             $children = $element->childNodes;
             /** @var $child DOMNode */
             foreach ($children as $child) {
                 if (isset($child->nodeName)) {
                     if ($child->nodeName == 'Name') {
                         $country->setName($child->nodeValue);
                     }
                     if ($child->nodeName == 'Code') {
                         $country->setCode($child->nodeValue);
                     }
                 }
             }
             $manager->persist($country);
             ++$i;
         }
     }
     $manager->flush();
 }