【问题标题】:zf2 doctrine 2 - Output OnetoOne Unidirectionalzf2 学说 2 - 输出 OnetoOne 单向
【发布时间】: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,但不提供名称:

&lt;?php echo $this-&gt;escapeHtml($pea-&gt;ic_hq_country);?&gt;

我实际上期望这样的事情是可能的,输出国家名称而不是 id:

&lt;?php echo $this-&gt;escapeHtml($pea-&gt;country);?&gt;

感谢您的阅读和任何帮助,这可以引导我走向正确的方向!

【问题讨论】:

  • 你看我的回答了吗?
  • 对不起!很忙,我认为我从 stackoverflow 收到一条带有注释的消息,有人回答了。非常感谢您的回答,它确实帮助了我!

标签: doctrine-orm zend-framework2 mapping associations entitymanager


【解决方案1】:

您不应在Peopleentity 的$ic_hq_country 字段中使用@Column anotation

/**
* @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());?>

【讨论】:

  • 抱歉没认出来!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2014-06-02
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多