Forms 将父实体Id传递到子窗体
你好, 我将实体ScheduledCommand放入到与ScheduledCommand的关系中Forms 将父实体Id传递到子窗体,forms,symfony,doctrine,Forms,Symfony,Doctrine,你好, 我将实体ScheduledCommand放入到与ScheduledCommand的关系中 /** * @ORM\MappedSuperclass (repositoryClass="Synolia\SyliusSchedulerCommandPlugin\Repository\ScheduledCommandRepository") * @ORM\Table("scheduled_command") */ class ScheduledCom
/**
* @ORM\MappedSuperclass (repositoryClass="Synolia\SyliusSchedulerCommandPlugin\Repository\ScheduledCommandRepository")
* @ORM\Table("scheduled_command")
*/
class ScheduledCommand implements ScheduledCommandInterface
{
/**
* @var int|null
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
* @ORM\Column(type="string")
*/
private $name = '';
/**
* @var string
* @ORM\Column(type="string")
*/
private $command = '';
/**
* @var int
* @ORM\Column(type="integer")
*/
private $priority = 0;
/**
* @ORM\OneToMany(targetEntity="ScheduledCommandLimitRange", mappedBy="scheduledCommand", orphanRemoval=true, cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $commandLimitRanges;
public function getId(): ?int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): ScheduledCommandInterface
{
$this->name = $name;
return $this;
}
public function getCommand(): string
{
return $this->command;
}
public function setCommand(string $command): ScheduledCommandInterface
{
$this->command = $command;
return $this;
}
public function getPriority(): int
{
return $this->priority;
}
public function setPriority(int $priority): ScheduledCommandInterface
{
$this->priority = $priority;
return $this;
}
public function getCommandLimitRanges(): ?Collection
{
return $this->commandLimitRanges;
}
public function setCommandLimitRanges(Collection $commandLimitRange): ?Collection
{
$this->commandLimitRanges = $commandLimitRange;
return $this->commandLimitRanges;
}
}
这是我的多通课程:
/**
* @ORM\Entity(repositoryClass="Synolia\SyliusSchedulerCommandPlugin\Repository\ScheduledCommandLimitRangeRepository")
* @ORM\Table(name="scheduled_command_limit_range")
*/
class ScheduledCommandLimitRange implements ScheduledCommandLimitRangeInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var \DateTime|null
* @ORM\Column(type="datetime", nullable=true)
*/
private $timeFrom;
/**
* @var \DateTime|null
* @ORM\Column(type="datetime", nullable=true)
*/
private $timeTo;
/**
* @ORM\ManyToOne(targetEntity="ScheduledCommand", inversedBy="commandLimitRanges")
*/
private $scheduledCommand;
public function getId(): ?int
{
return $this->id;
}
public function getTimeFrom(): ?\DateTime
{
return $this->timeFrom;
}
public function setTimeFrom(?\DateTime $timeFrom): ScheduledCommandLimitRangeInterface
{
$this->timeFrom = $timeFrom;
return $this;
}
public function getTimeTo(): ?\DateTime
{
return $this->timeTo;
}
public function setTimeTo(?\DateTime $timeTo): ScheduledCommandLimitRangeInterface
{
$this->timeTo = $timeTo;
return $this;
}
public function getScheduledCommand(): ?ScheduledCommand
{
return $this->scheduledCommand;
}
public function setScheduledCommand(ScheduledCommand $scheduledCommand): ?ScheduledCommand
{
$this->scheduledCommand = $scheduledCommand;
return $this;
}
我得到了ScheduledCommandType,它在一个字段中具有CollectionType,其条目类型为CommandLimitType
final class ScheduledCommandType extends AbstractType
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
->add('command', CommandChoiceType::class)
->add('arguments')
->add('cronExpression')
->add('commandLimitRanges', CollectionType::class, [
'label' => 'sylius.ui.scheduled_command_limit',
'entry_type' => CommandLimitType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'inherit_data' => true,
'entry_options' => array(
'scheduledCommand' => 'id'),
])
->add('logFile')
->add('priority')
->add('executeImmediately')
->add('enabled')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => ScheduledCommand::class,
]);
}
}
这是我的输入类型:
class CommandLimitType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('timeTo', DateTimeType::class, [
'widget' => 'single_text',
])
->add('timeFrom', DateTimeType::class, [
'widget' => 'single_text'
])
->add('scheduledCommand', HiddenType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(['scheduledCommand']);
$resolver->setDefaults([
'data_class' => ScheduledCommandLimitRange::class,
]);
}
}
我有一个问题,当我想要提交这个表单值时,CommandLimitRange parentId为null,所以我尝试将父Id作为隐藏字段传递到子表单中。但在提交后仍然得到,仍然存在错误:
Cannot read index "0" from object of type "Synolia\SyliusSchedulerCommandPlugin\Entity\ScheduledCommand" because it doesn't implement \ArrayAccess.
将父实体Id传递到条目中的正确方法是什么,以便在提交后将正确的父实体Id链接到数据库中的行?
非常感谢我找到了解决办法。好的,这里有几个问题,我以前没有弄清楚。 首先也是最重要的——我只有基本的setter,而不是add/remove方法
public function addCommandLimitRange(ScheduledCommandLimitRange $commandLimitRange): self
{
if (!$this->commandLimitRanges->contains($commandLimitRange)) {
$this->commandLimitRanges[] = $commandLimitRange;
$commandLimitRange->setScheduledCommand($this);
}
return $this;
}
public function removeCommandLimitRange(ScheduledCommandLimitRange $commandLimitRange): self
{
if ($this->commandLimitRanges->removeElement($commandLimitRange)) {
// set the owning side to null (unless already changed)
if ($commandLimitRange->getScheduledCommand() === $this) {
$commandLimitRange->setScheduledCommand(null);
}
}
return $this;
}
接下来,我需要将我的属性设置为构造函数中的ArrayCollection对象:
public function __construct()
{
$this->commandLimitRanges = new ArrayCollection();
}
最后,为了得到正确的delete,而不是在parentId上运行带有null的update,我需要在注释中添加孤儿删除选项
/**
* @ORM\OneToMany(targetEntity="ScheduledCommandLimitRange", mappedBy="scheduledCommand", cascade={"persist"}, orphanRemoval=true)
*/
private $commandLimitRanges;