Bildiğimiz gibi @ORM\GeneratedValue(strategy="AUTO") stratejisi, $id alanını otomatik olarak doldurur. Eğer ID bilgilerini Doctrine data fixtures içindeyken kendiniz belirlemek isterseniz, aşağıdaki örneği takip edebilirsiniz.


Entity


use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @ORM\Entity
* @ORM\Table(name="user")
* @UniqueEntity(fields="username", message="Username is already in use.")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(name="username", type="string", length=20, unique=true)
*/
protected $username;

/**
* @ORM\Column(name="password", type="string", length=40)
*/
protected $password;
}

Fixtures


use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Application\BackendBundle\Entity\User;

class UserFixtures extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
foreach ($this->getData() as $data) {
$user = $this->getUser($data);

$manager->persist($user);

// Enforce specified record ID
$metadata = $manager->getClassMetaData(get_class($user));
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
}

$manager->flush();
}

private function getUser(array $data)
{
return
(new User())
->setId($data['id'])
->setUsername($data['username'])
->setPassword($data['password']);
}

private function getData()
{
return
[
[
'id' => '11',
'username' => 'inanzzz',
'password' => '09cd68a2a77b22a312dded612dd0d9988685189f',
],
[
'id' => '22',
'username' => 'robertdeniro',
'password' => '09cd68a2a77b22a312dded612dd0d9988685189f',
],
[
'id' => '33',
'username' => 'alpacino',
'password' => '09cd68a2a77b22a312dded612dd0d9988685189f',
],
];
}

public function getOrder()
{
return 1;
}
}