2013-11-20 70 views
1

我有一個zend2形式的問題。我做了從數據庫中獲取一些數據,並加入一些表實體...必須在zend2中返回一個字符串值。怎麼樣?

這裏是實體:

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; 

下面這段代碼是getter和setter,一個構造函數,附加和一個刪除功能。

在這裏,他們是:

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()); 
    } 

我的問題是與protected $countries。如果我在表單中添加屬性值,它會給我在實體中找不到的「國家/地區」屬性。

如果我不加它,而是使用__toString()功能,它給了我一個錯誤說,它不能轉換的國家串......在__toString()函數添加以下代碼:

public function __toString() 
{ 

return $this->countries; 

} 

謝謝你的幫助!

AE

+1

你想要什麼?選擇元素與國家?一個活動的所有相關國家的名單?此外,'__toString'顯然應該返回一個**字符串**! –

+1

一個活動的所有相關國家的名單將是我想要的..謝謝你的迴應! –

回答

1

你說你想要一個包含所有相關國家的字符串。以下代碼演示如何實現此目的:

$campaignCountryNames = array(); 

$campaignCountries = $campaign->getCountries(); 

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

echo implode(', ', $campaignCountryNames); 
+1

謝謝!這很好用! –

相關問題