ManyToOne与zjcuser doctrine实体的关联与bjyauthorize

时间:2015-05-18 22:31:41

标签: php doctrine-orm zend-framework2 zfcuser bjyauthorize

我使用zfcuser doctrine模块为我的zf2项目使用bjyauthorize,它工作得很好。现在我想从我的用户实体中获取连接的Country实体

在User.php中:

/**
 * An example of how to implement a role aware user entity.
 *
 * @ORM\Entity
 * @ORM\Table(name="users", indexes={
 *      @ORM\Index(name="fk_User_Country1_idx", columns={"Country_id"}), 
 * }, uniqueConstraints={@ORM\UniqueConstraint(name="email_UNIQUE", columns={"email"})})
 * @ORM\HasLifecycleCallbacks()
 */
class User implements UserInterface, ProviderInterface
{

...

/**
 * @var ersEntity\Country
 * @ORM\ManyToOne(targetEntity="Country", inversedBy="users")
 * @ORM\JoinColumn(name="Country_id", referencedColumnName="id")
 */
protected $country;
在Country.php中

/**
 * Entity\Country
 *
 * @ORM\Entity()
 * @ORM\Table(name="Country")
 * @ORM\HasLifecycleCallbacks()
 */
class Country implements InputFilterAwareInterface
{

...

/**
 * @ORM\OneToMany(targetEntity="User", mappedBy="country")
 * @ORM\JoinColumn(name="id", referencedColumnName="Country_id")
 */
protected $users;

我的一个控制器中的一个简单的testAction失败:

$user = $em->getRepository("ersEntity\Entity\User")
    ->findOneBy(array('id' => 1));
error_log($user->getFirstname().' '.$user->getSurname());
error_log('country: '.$user->getCountry()->getName());

导致:

[Tue May 19 00:02:44 2015] [error] [client 185.17.207.16] Andi N.
[Tue May 19 00:02:44 2015] [error] [client 185.17.207.16] PHP Fatal error:  Call to a member function getName() on a non-object in /home/ers/www/ers/module/Admin/src/Admin/Controller/TestController.php on line 172

我想知道为什么不能从User实体获取Country实体。与同一项目中的其他实体一起工作正常。

有人可以告诉我,为了能够从这个zfcuser-bjyauthorize-doctrine用户实体中获取Country实体需要做些什么?

有关更多代码信息,整个项目可在开发分支的https://github.com/inbaz/ers处获得。

修改

对于没有设置国家/地区的用户,需要出错。没关系,需要检查一个国家是否存在。但是这个用户有一个国家/地区。我通过phpmyadmin检查了一下。通过getCountry()方法无法获得此国家/地区实体。

这可能导致将学说实体反序列化并序列化到会话中。我检查了doctrine documentation如何将实体保存到会话中。但是我想在会话中保留所有子实体,所以在我的情况下,我在会话中有一个包含多个包实体的订单实体。每个包实体都有一个用户和多个项实体。从会话中获取订单实体后,我希望能够访问所有这些元素。

我甚至尝试在会话中对每个用户进行合并,例如:

foreach($participants as $participant) {
    $participant = $em->merge($participant);
}

但这并没有改变任何事情。即使合并整个订单也没有成功。

您是否了解如何使用完整的学说功能从理论中恢复学说实体?

2 个答案:

答案 0 :(得分:0)

对我来说看起来很好。起初我认为您的实体定义中缺少getCountry方法,但我看到it is there

我的猜测是id为1的用户没有设置国家/地区。转到您的表格(phpmyadmin或其他)并检查Country_id列的值。 如果您希望每个用户都拥有一个国家/地区,则可以根据the Doctrine2 @JoinColumn specs nullable=false 添加到您的加入列定义中,以确保您的数据完整性。

否则(如果允许$countrynull),您应首先检查国家/地区是否为对象,然后再提取getName()

$countryName = null;
$country = $user->getCountry();
if(is_object($country)){
    $countryName = $country->getName();
}
/** @var null|string $countryName */
$countryName  //... use your country name which is null or string

还有一件事,我强烈建议只为名称使用小写字符串。使用大写可能会让你陷入困境。

修改

我仔细看了一下,现在看到你的映射是错误的。你 use doctrine to validate your schema 了吗?关系的反面有一个@ORM\JoinColumn(在Country.php中)。反面不需要这个@ORM\JoinColumn声明。你应该删除这一行。拥有方是User,因此连接列定义应仅在那里声明。

应该是这样的:

<强> Country.php:

/**
 * @var Collection
 * @ORM\OneToMany(targetEntity="User", mappedBy="country")
 */
protected $users;

<强> user.php的:

/**
 * @var Country
 * @ORM\ManyToOne(targetEntity="Country", inversedBy="users")
 * @ORM\JoinColumn(name="Country_id", referencedColumnName="id")
 */
protected $country;

答案 1 :(得分:0)

Doctrine 2: Can entities be saved into sessions?

这是我的问题的答案。

  

如果在序列化时未加载延迟加载的实体,则在反序列化后将无法加载。因此,在序列化之前,您必须确保实体已完全加载。

如果无法创建新的学说实体并获得反序列化实体的数组副本并将此数据填充到新实体中,那么仍然存在问题。

相关问题