Меня озадачила проблема с Doctrine \ ORM \ Mapping. У меня есть две сущности, и они связаны с однонаправленным взаимодействием «многие-к-одному». Я хочу работать с Chipinfo (добавлять / обновлять / удалять) без влияния на производителя. Только сохранить Chipinfo, пока нет производителя.
class Chipinfo implements
{
/**
* @var integer
*
* @ORM\Column(name="ChipID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $chipid;
/**
* @var \Shop\Entity\Producer
*
* @ORM\ManyToOne(targetEntity="Shop\Entity\Producer")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="pId", referencedColumnName="producerid")
* })
*/
protected $pid;
}
class Producer{
/**
* @ORM\Id
* @ORM\Column(name="producerid", type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $producerId;
/**
* @ORM\Column(name="producername", type="string")
*/
protected $producerName;
}
ChipInfo и Producer являются однонаправленными отношениями «многие-к-одному»: chipinfo может быть создан только одним производителем, в то время как один производитель может создавать несколько chipinfo. Я хочу, чтобы: добавление / обновление / удаление элементов в Chipinfo не повлияло на производителя.
$chip = new Chipinfo();
$formData = $this->initFormData($form->getData());
$chip->populate($formData);
$this->getEntityManager()->persist($chip);
$this->getEntityManager()->flush();
private function initFormData(&$raw){
$raw['eid'] = new Encapuser($this->findEntity("Shop\Entity\Encapuser", $raw['eid']));
$this->log($raw->eid);
$raw['vid'] = new Vendors($this->findEntity("Shop\Entity\Vendors", $raw['vid']));
$raw['pid'] = new Producer($this->findEntity("Shop\Entity\Producer", $raw['pid']));
$this->log($raw);
return $raw;
}
would throws errors:
A new entity was found through the relationship 'Shop\Entity\Chipinfo#pid' that was not configured to cascade persist operations for entity: Shop\Entity\Producer@00000000349002ee00000000c955fd11. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Shop\Entity\Producer#__toString()' to get a clue.
Then I configured the pid as:
@ORM\ManyToOne(targetEntity="Shop\Entity\Producer", cascade={"persist"})
Хоть ошибка пропадает, но это не то, что я хочу. Потому что, когда я вызываю flush () для chipinfo с существующим производителем A, вставляется новый A, который дублируется.
Поэтому у меня следующие вопросы: 1) Как мне настроить поле @manyToone, я не получил подсказки от http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working-with-associations.html#transitive-persistence-cascade-operations
2) Следует ли добавить: @ORM \ OneToMany, targetEntity = "Shop \ Entity \ Producer" private @chip;
в продюсере? Если да, потребуется ли для операций (добавление / удаление / обновление) производителя @chip?