duan62819774 2015-05-18 22:31
浏览 51
已采纳

ManyToOne与zjcuser doctrine实体的关联与bjyauthorize

I'm using the zfcuser doctrine module for my zf2 project with bjyauthorize, which is working quite fine. Now I'd like to get the connected Country entity out of my User entity

in 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;

in 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;

A simple testAction in one of my Controllers fails:

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

which results in:

[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

I'm wondering why it's not possible to get the Country entity from the User entity. With other entities in this same project this is working fine.

Can somebody tell me what needs to be done to be able to get the Country entity out of this zfcuser-bjyauthorize-doctrine User entity?

For more code info, this whole project is available at https://github.com/inbaz/ers in the develop branch.

EDIT:

With users who have no country set there needs to be an error. It's right that there is the need to check if a country exists. But this user has a country set. I checked that via phpmyadmin. It's not possible to get this country entity via the getCountry() method.

Maybe this is cause the deserializing and serializing of the doctrine entity into the session. I checked the doctrine documentation on how to save entities into the session. But I'd like to keep all subentities in the session, so in my case I have a order entity in the session which holds multiple package entities. Each package entity has one user and multiple item entities. When getting the order entity back from the session I'd like to be able to access all these elements.

I even tried to do a merge on each user in the session like:

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

but that doesn't change anything. Even a merge on the whole order was not successful.

Do you have an idea on how to get the doctrine entities back from the session with the full doctrine features?

  • 写回答

2条回答 默认 最新

  • douzhao5656 2015-05-19 07:33
    关注

    It looks all fine to me. At first I thought your getCountry method was missing from your entity definition but I see it is there.

    My guess is that user with id 1 does not have a Country set. Go to your table (phpmyadmin or whatever) and check the value of the Country_id column. If you want each user to have a country you can insure your data integrity by adding nullable=false to your join column definition according to the Doctrine2 @JoinColumn specs.

    Otherwise (if $country is allowed to be null) you should maybe first check if country is an object before pulling 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
    

    One more thing, I would strongly suggest to use only lowercase strings for table and column names. Using uppercase could get you into trouble.

    EDIT

    I took a closer look and see now that your mappings are wrong. Did you use doctrine to validate your schema? There is a @ORM\JoinColumn on the inverse side of the relationship (inside Country.php). The inverse side does not need this @ORM\JoinColumn declariation. You should remove this line. The owning side is User so the join column definition should be only declared there.

    It should be like this:

    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条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了