【发布时间】:2014-01-31 17:29:31
【问题描述】:
注释 OneToOne 单向后,我想输出连接的列,而不使用表单。
一个人实体有一个列来存储国家实体的 id。
我能做什么:我可以使用带有下拉选择的表单将国家/地区的 id 存储到人员实体中,该表单绑定到国家实体。
问题:我无法输入希望正确的连接表的值国家/地区。
人物实体:
<?php
namespace People\Entity;
use Doctrine\ORM\Mapping as ORM;
// ...
/**
* A people entity.
*
* @ORM\Entity
* @ORM\Table(name="icd_people")
* @property int $id
// ...
* @property string $ic_hq_country
*/
class People implements InputFilterAwareInterface
{
protected $inputFilter;
/**
* @ORM\Id
* @ORM\Column(type="integer");
*/
protected $id;
/**
* @ORM\Column(type="integer")
* @ORM\OneToOne(targetEntity="Country")
* @ORM\JoinColumn(name="ic_hq_country", referencedColumnName="id")
*/
protected $ic_hq_country;
// getter and setter
}
国家实体:
<?php
namespace People\Entity;
use Doctrine\ORM\Mapping as ORM;
//...
/**
* A Country entity.
*
* @ORM\Entity
* @ORM\Table(name="pre_country")
* @property int $id
* @property string $country
*/
class Country implements InputFilterAwareInterface
{
protected $inputFilter;
/**
* @ORM\Id
* @ORM\Column(type="integer");
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $country;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set country
*
* @param string $country
* @return Country
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Convert the object to an array.
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
throw new \Exception("Not used");
}
}
控制器动作:
public function indexAction()
{
$userid = $this->zfcUserAuthentication()->getIdentity()->getId();
return new ViewModel(array(
'pea' => $this->getEntityManager()->find('People\Entity\People', $userid),
));
}
View 提供国家的 id,但不提供名称:
<?php echo $this->escapeHtml($pea->ic_hq_country);?>
我实际上期望这样的事情是可能的,输出国家名称而不是 id:
<?php echo $this->escapeHtml($pea->country);?>
感谢您的阅读和任何帮助,这可以引导我走向正确的方向!
【问题讨论】:
-
你看我的回答了吗?
-
对不起!很忙,我认为我从 stackoverflow 收到一条带有注释的消息,有人回答了。非常感谢您的回答,它确实帮助了我!
标签: doctrine-orm zend-framework2 mapping associations entitymanager