Estou me baseando nesse tutorial, mas tenho as classes Country e State. Estou tentando importar os dados usando o comando:
php console doctrine:fixtures:load
quando executo ele funciona para o CountryLoader, carrega os dados e armazena no banco de dados, mas dá falha ao tentar carregar StateLoader.
A mensagem que ele exibe é:
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Undefined index: country
Como fazer para funcionar o StateLoader?
CountryLoader.php
namespace Test\Bundle\TestBundle\DataFixtures\ORM;
use Hautelook\AliceBundle\Alice\DataFixtureLoader;
/**
* Description of CountryLoader
*
*/
class CountryLoader extends DataFixtureLoader {
protected function getFixtures() {
return array(
__DIR__ . '/country.yml',
);
}
}
StateLoader.php
namespace Test\Bundle\TestBundle\DataFixtures\ORM;
use Hautelook\AliceBundle\Alice\DataFixtureLoader;
/**
* Description of StateLoader
*
*/
class StateLoader extends DataFixtureLoader {
protected function getFixtures() {
return array(
__DIR__ . '/country.yml',
__DIR__ . '/state.yml',
);
}
}
Country.php
/**
* Description of Country
* @ORM\Entity
* @ORM\Table(name="country")
*/
class Country {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\Column(type="string", length=64, unique=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=8, unique=true)
*/
private $iso3;
/**
* @ORM\Column(type="string", length=8, unique=true)
*/
private $numcode;
/**
* @ORM\Column(type="string", length=8, unique=true)
*/
private $acronym;
/**
* @ORM\OneToMany(targetEntity="City", cascade={"all"}, mappedBy="country")
* @var ArrayCollection
*/
private $states;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getIso3() {
return $this->iso3;
}
public function getNumcode() {
return $this->numcode;
}
public function getAcronym() {
return $this->acronym;
}
public function getStates() {
return $this->states;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function setIso3($iso3) {
$this->iso3 = $iso3;
return $this;
}
public function setNumcode($numcode) {
$this->numcode = $numcode;
return $this;
}
public function setAcronym($acronym) {
$this->acronym = $acronym;
return $this;
}
public function setStates(ArrayCollection $states) {
$this->states = $states;
return $this;
}
}
State.php
/**
* Description of State
* @ORM\Entity
* @ORM\Table(name="state")
*/
class State {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\Column(type="string", length=64, unique=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=8, unique=true)
*/
private $acronym;
/**
* @ORM\OneToMany(targetEntity="City", cascade={"all"}, mappedBy="state")
* @var ArrayCollection
*/
private $cities;
/**
* @ORM\ManyToOne(targetEntity="Country", inversedBy="states", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="country_acronym", referencedColumnName="acronym", onDelete="CASCADE")
* @var Country
*/
private $country;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getAcronym() {
return $this->acronym;
}
public function getCities() {
return $this->cities;
}
public function getCountry() {
return $this->country;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function setAcronym($acronym) {
$this->acronym = $acronym;
return $this;
}
public function setCities(ArrayCollection $cities) {
$this->cities = $cities;
return $this;
}
public function setCountry(Country $country) {
$this->country = $country;
return $this;
}
}
country.yml
Test\Bundle\TestBundle\Entity\Country:
BR:
iso3: BRA
acronym: BR
numcode: 76
name: Brasil
state.yml
Test\Bundle\TestBundle\Entity\State:
AC:
acronym: AC
name: Acre
country: @BR
doctrine:schema:validate
) e, além disso, retirei ocountry.yml
do carregamento dos estados (senão carregaria os dados em duplicata).