在注释OneToOne Unidirectional后,我想输出已连接的列,而不使用表单。
One People Entity有一列用于存储国家/地区实体的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),
));
}
视图提供国家/地区的ID,但不是名称:
<?php echo $this->escapeHtml($pea->ic_hq_country);?>
我实际上期望这样的事情是可能的,输出国家名称而不是id:
<?php echo $this->escapeHtml($pea->country);?>
感谢您阅读,以及任何帮助,这可能会让我走向正确的方向!
答案 0 :(得分:0)
您不应在@Column anotation
实体的$ic_hq_country
字段中使用People
。
/**
* @ORM\OneToOne(targetEntity="Country")
* @ORM\JoinColumn(name="ic_hq_country", referencedColumnName="id")
*/
protected $ic_hq_country;
像这样,希望ic_hq_country
将成为实体的代理而不是id。
所以在你看来你可以使用:
<?php echo $pea->ic_hq_country->getId();?>
以及
<?php echo $this->escapeHtml($pea->ic_hq_country->getCountry());?>