【问题标题】:Symfony dependent forms with 3 entities in dropdowns下拉列表中有 3 个实体的 Symfony 相关表单
【发布时间】:2017-08-22 12:54:19
【问题描述】:

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

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

Channel1 和 3 与 Agencies 具有多对一关系,而 Channel3 与 Channel1 具有 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;
}

第三频道

 /**
 * @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,但甚至没有调整两个实体。

对于我的方法 getChannel3s(),我经常遇到诸如“尝试调用类“Doctrine\Common\Collections\ArrayCollection”的未定义方法之类的异常,但我不明白为什么它们是未定义的。 如果有人能够帮助我并给我一些指导,我会非常高兴,因为我已经做了好几天了,真的必须完成这项工作!

【问题讨论】:

    标签: symfony dropdown


    【解决方案1】:

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

    也要改

    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.
     }
    

    【讨论】:

    • 谢谢!我仍然收到错误“尝试调用类“Doctrine\Common\Collections\ArrayCollection”的名为“getChannel3s”的未定义方法。”,但是......你知道为什么吗?
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2018-04-28
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多