我与父母和孩子有一个自我引用实体。奇怪的是,当我将父元素(doctrinemodule objectselect(添加到for for for for表单时,当我坚持该实体时,父母实体的其他一些字段不会更新。儿童实体更新。
在父母的更新查询中,不是我要更新的这些字段。就像学说不再识别父母(拥有方(的更改。在持久之前,我获得了正确的实体,具有从表单中的实际更改,但除了学说没有更新查询中的更改字段。
当我以表单中的父和子女的形式元素删除表单元素时,一切正常,父母实体更新/坚持所有字段。
/**
* @var string
*
* @GedmoTranslatable
* @ORMColumn(type="text")
*/
private $teaser;
/**
* @var string
*
* @GedmoTranslatable
* @ORMColumn(type="text")
*/
private $description;
/**
* One Category has Many Categories.
* @var Collection
* @ORMOneToMany(targetEntity="RentalEntityRental", mappedBy="parent")
*/
private $children;
/**
* Many Categories have One Category.
* @ORMManyToOne(targetEntity="RentalEntityRental", inversedBy="children")
* @ORMJoinColumn(name="parent_id", referencedColumnName="houses_id", nullable=true, onDelete="SET NULL")
*/
private $parent;
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* Get teaser
*
* @return string
*/
public function getTeaser()
{
return $this->teaser;
}
/**
* Set teaser
*
* @param string $teaser
*/
public function setTeaser($teaser)
{
$this->teaser = $teaser;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set description
*
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Add $child
* @param Collection $children
*/
public function addChildren(Collection $children)
{
foreach ($children as $child) {
$this->addChild($child);
}
}
/**
* @param Rental $child
* @return void
*/
public function addChild(Rental $child)
{
if ($this->children->contains($child)) {
return;
}
$child->setParent($this);
$this->children[] = $child;
}
/**
* Remove children
* @param Rental $children
*/
public function removeChildren(Collection $children)
{
foreach ($children as $child) {
$this->removeChild($child);
}
}
/**
* Remove child.
*
* @param RentalEntityRental $child
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeChild(Rental $child)
{
return $this->children->removeElement($child);
}
/**
* Get children.
*
* @return DoctrineCommonCollectionsCollection
*/
public function getChildren()
{
return $this->children;
}
/**
* Set parent.
*
* @param RentalEntityRental|null $parent
*/
public function setParent(Rental $parent = null)
{
$this->parent = $parent;
}
/**
* Get parent.
*
* @return RentalEntityRental|null
*/
public function getParent()
{
return $this->parent;
}
基于答案(可能仍会更改(根据与OP的评论中的讨论。
简单的用例:自我引用学说实体无法正确更新与储蓄行动的父/儿童相关实体对象属性。
OP提供的实体。
我假设您为该实体设置了表单。我还假设它是正确的设置,因为您在被询问后没有提供(因为正确的是,这意味着很多代码(。但是,假设有代码的东西会造成很多错误,这就是为什么我在这里提到它的原因。
因此,我假设在控制器中处理您的表格可能是错误的。要检查,请使用以下简化的addAction
功能并给它拍摄(下面的出厂代码(。
/**
* @var RentalForm
*/
protected $form;
/**
* @var ObjectManager
*/
protected $objectManager;
public function __construct(ObjectManager $objectManager, RentalForm $form)
{
$this->form = $form;
$this->objectManager = $objectManager;
}
public function addAction()
{
/** @var RentalForm $form */
$form = $this->getForm();
/** @var Request $request */
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$entity = $form->getObject();
$this->getObjectManager()->persist($entity);
try {
$this->getObjectManager()->flush();
} catch (Exception $e) {
$message = sprintf(
'Was unable to save the data. Saving threw an error. <br />Code: %s. <br />Message: %s',
$e->getCode(),
$e->getMessage()
);
$this->flashMessenger()->addErrorMessage($message);
return [
'form' => $form,
'validationMessages' => $form->getMessages() ?: '',
];
}
$this->flashMessenger()->addSuccessMessage(
$this->getTranslator()->translate('Successfully created object.')
);
// TODO replace vars with your own: return $this->redirect()->route($route, $routeParams);
}
$this->flashMessenger()->addWarningMessage(
'Your form contains errors. Please correct them and try again.'
);
}
return [
'form' => $form,
'validationMessages' => $form->getMessages() ?: '',
];
}
上课的工厂,根据需要修改情况
class RentalControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/** @var ObjectManager $objectManager */
$objectManager = $container->get(EntityManager::class);
/** @var FormElementManagerV3Polyfill $formElementManager */
$formElementManager = $container->get('FormElementManager');
/** @var RentalForm $form */
$form = $formElementManager->get(RentalForm::class);
return new RentalController($objectManager, $form);
}
}