Symfony依赖形式,下拉列表中有3个实体

时间:2017-08-22 12:54:20

标签: symfony dropdown

我已在该帖子中提出问题:Symfony dependent dropdown with 3 entities但到目前为止,没有人可以帮我解决问题所以我会尝试以不同的方式提出问题。

我正在尝试为将分配给渠道和代理商的文档设计创建表单。代理商依赖于channel3s,channel3s依赖于channel1s,因此在选择这些时,我希望下拉内容仅限于相应的数据。 频道1 - > channel3 - >机构

Channel1和3与代理商之间存在ManyToOne关系,而Channel3与Channel1s之间存在OneToMany关系。 所以在我的实体类中看起来像这样: 代理商实体

  /**
 *
 * @var string @ORM\Column(type="string", length=2)
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Channel1", inversedBy="agency")
 * @ORM\JoinColumn(name="channel", referencedColumnName="id", nullable=true)
 */
protected $channel;

/**
 *
 * @var string @ORM\Column(type="string", length=255, name="channel_name")
 */
protected $channelName;

/**
 * @var string @ORM\Column(type="string", length=3)
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Channel3", inversedBy="agency")
 * @ORM\JoinColumn(name="channel3", referencedColumnName="id", nullable=true)
 */
protected $channel3;

/**
 *
 * @var string @ORM\Column(type="string", length=255, name="channel_3_name")
 */
protected $channel3Name;


  /**
 * Set channel
 *
 * @param string $channel
 *
 * @return Agency
 */
public function setChannel($channel)
{
    $this->channel = $channel;

    return $this;
}

/**
 * Get channel
 *
 * @return string
 */
public function getChannel()
{
    return $this->channel;
}

/**
 * Set channelName
 *
 * @param string $channelName
 *
 * @return Agency
 */
public function setChannelName($channelName)
{
    $this->channelName = $channelName;

    return $this;
}

/**
 * Get channelName
 *
 * @return string
 */
public function getChannelName()
{
    return $this->channelName;
}
/**
 * Set channel3
 *
 * @param string $channel3
 *
 * @return Agency
 */
public function setChannel3($channel3)
{
    $this->channel3 = $channel3;

    return $this;
}

/**
 * Get channel3
 *
 * @return string
 */
public function getChannel3()
{
    return $this->channel3;
}

/**
 * Set channel3Name
 *
 * @param string $channel3Name
 *
 * @return Agency
 */
public function setChannel3Name($channel3Name)
{
    $this->channel3Name = $channel3Name;

    return $this;
}

/**
 * Get channel3Name
 *
 * @return string
 */
public function getChannel3Name()
{
    return $this->channel3Name;
}

通道1

 /**
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Channel3", mappedBy="channel1")
 **/
protected $channel3s;

  /**
 * Constructor
 */
public function __construct()
{
    $this->channel3s = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add channel3
 *
 * @param \AppBundle\Entity\Channel3 $channel3
 *
 * @return Channel1
 */
public function addChannel3(\AppBundle\Entity\Channel3 $channel3)
{
    $this->channel3s[] = $channel3;

    return $this;
}

/**
 * Remove channel3
 *
 * @param \AppBundle\Entity\Channel3 $channel3
 */
public function removeChannel3(\AppBundle\Entity\Channel3 $channel3)
{
    $this->channel3s->removeElement($channel3);
}

/**
 * Get channel3s
 *
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getChannel3s()
{
    return $this->channel3s;
}
    /**
     * Get channel3
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChannel3()
    {
            return $this->channel3;
    }

/**
 * Set the value of Channel
 * @param mixed $channel3s
 * @return self
 */
public function setChannel3s($channel3s) {
    $this->channel3s[] = $channel3s;
    return $this;
}

通道3

 /**
 * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\Channel1", inversedBy="channel3s")
 * @ORM\JoinColumn(name="channel1", referencedColumnName="id", nullable=false)
 * @var \AppBundle\Entity\Channel1
 **/
private $channel1;

  /**
 * Set channel1
 *
 * @param \AppBundle\Entity\Channel1 $channel1
 *
 * @return Channel3
 */
public function setChannel1(\AppBundle\Entity\Channel1 $channel1)
{
    $this->channel1 = $channel1;

    return $this;
}

/**
 * Get channel1
 *
 * @return \AppBundle\Entity\Channel1
 */
public function getChannel1()
{
    return $this->channel1;
}

我已经尝试过任何我能想到的ajax功能。我试过使用EventSubscribers或只是EventListeners但是甚至没有调整两个实体。

我经常遇到类似&#34的异常;试图调用类的未定义方法" Doctrine \ Common \ Collections \ ArrayCollection"。对于我的方法getChannel3s(),但我不明白为什么它们是未定义的。 对于能够帮助我并给我一点指导的人,我会非常高兴,因为我现在已经完成了这项任务已经好几天了,真的必须完成这项工作!

1 个答案:

答案 0 :(得分:0)

开始从注释中删除@ORM\Column(type="string", length=2)@ORM\Column(type="string", length=3),因为关系不是字符串而是Entity对象。

同时更改

public function setChannel3s($channel3s) {
    $this->channel3s[] = $channel3s;
    return $this;
}

public function setChannel3s($channel3s) {
    $this->channel3s = $channel3s;
    return $this;
}

因为使用原始方法,您可以将数组添加到数组中,而不是覆盖数组。

您的Channel1实体中有一个getChannel3方法,而该实体中不存在该属性。

 public function getChannel3()
 {
     return $this->channel3; // channel3 variable does not exist in class.
 }