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
你想要什么?选择元素与国家?一个活动的所有相关国家的名单?此外,'__toString'显然应该返回一个**字符串**! –
一个活动的所有相关国家的名单将是我想要的..谢谢你的回应! –