0
votes

I've already asked the question within that post: Symfony dependent dropdown with 3 entities but so far nobody could help me with my problem so I will try to ask my question differently.

I'm trying to design a creation form for documents which will be assigned to channels and agencies. Agencies are dependent on channel3s and channel3s are dependent on channel1s, so when selecting these I want the dropdown contents to be limited to the according data. Channel1 --> channel3 --> Agency

Channel1 and 3 have a ManyToOne Relationship to Agencies and Channel3s have a OneToMany Relationship to Channel1s. So in my entity classes it looks like that: Agency Entity

  /**
 *
 * @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;
}

Channel1

 /**
    * @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;
}

Channel3

 /**
 * @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;
}

I've tried any way of ajax functions I could think of. I've tried to use EventSubscribers or just EventListeners but not even adjusting two entities is working.

I often get exceptions like "Attempted to call undefined method of of class "Doctrine\Common\Collections\ArrayCollection". for my methods getChannel3s() but I don't see why they are undefined. I would be super happy about anyone who might be able to help me and give me a little guide through this since I'm on that task for several days now and really have to get that work done!

1

1 Answers

0
votes

To start remove @ORM\Column(type="string", length=2) and @ORM\Column(type="string", length=3) from the annotations since the relations are not strings but Entity objects.

Also change

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

to

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

because with the original method you add an array to an array instead of overwriting the array.

You have a method getChannel3 in your Channel1 entity while the attribute does not exist in the entity.

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