Question

I have a problem with a zend2 form. I made an entity which gets some data from the database and joins some tables...

here is the entity:

class Campaigns
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     *
     * @ORM\Column(name="campaign_name", type="string")
     *
     */
    protected $campaigns;

    /**
     * @var mixed
     *
     * @ORM\ManyToMany(targetEntity="Application\Entity\Countries", cascade={"persist"}, orphanRemoval=false)
     * @ORM\JoinTable(name="campaigns_countries",
     *      joinColumns={@ORM\JoinColumn(name="campaign_id", referencedColumnName="id", onDelete="CASCADE")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id", onDelete="CASCADE")}
     *      )
     */
    protected $countries;

Below this code are the getters and setters, a construct function, an add and an remove function.

Here they are:

public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function getCampaigns()
    {
        return $this->campaigns;
    }

    public function setCampaigns($campaigns)
    {
        $this->campaigns = $campaigns;
        return $this;
    }



    public function  addCampaigns($campaigns = null)
    {
        foreach ($campaigns as $c) {
            if (!$this->campaigns->contains($c)) {
                $this->campaigns->add($c);
            }
        }
    }

    public function removeCampaigns($campaigns)
    {
        foreach ($campaigns as $c) {
            if ($this->campaigns->contains($c)) {
                $this->campaigns->removeElement($c);
            }
        }
    }
    public function getCountries()
    {
        return $this->countries;
    }

    public function setCountries($countries)
    {
        $this->countries = $countries;
        return $this;
    }

    public function  addCountries($countries = null)
    {
        foreach ($countries as $c) {
            if (!$this->countries->contains($c)) {
                $this->countries->add($c);
            }
        }
    }

    public function removeCountries($countries)
    {
        foreach ($countries as $c) {
            if ($this->countries->contains($c)) {
                $this->countries->removeElement($c);
            }
        }
    } //construct for countries
    public function __construct()
    {
        $this->setCountries(new ArrayCollection());
    }

My problem is with the protected $countries. If i add in the form the property value, it gives me the "countries" property not found in entity.

If I do not add it, and instead use __toString() function, it gives me an error saying that it could not convert countries to string...in the __toString() function I added the following code:

public function __toString()
{

return $this->countries;

}

Thank you for all your help!

AE

Was it helpful?

Solution

You say you want a string containing all related countries. The following code demonstrates how you could achieve this:

$campaignCountryNames = array();

$campaignCountries = $campaign->getCountries(); 

foreach ($campaignCountries as $country) {
    // I assume your Country entity has a name property
    $campaignCountryNames[] = $country->getName();
}

echo implode(', ', $campaignCountryNames);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top