dpp89959 2014-08-27 01:49
浏览 33
已采纳

注册后从用户对象访问相关的实体属性

I have a main User entity that holds common fields but also has some extensions like for example Company and Natural both extends from User. Basically (I remove the rest of info since it's not relevant here) this is how my entities looks:

\UserBundle\Entity\User.php:

class User extends BaseUser
{

    /**
     * @var \Natural
     *
     * @ORM\OneToMany(targetEntity="\UserBundle\Entity\Natural", mappedBy="user")
     */
    protected $natural;

    /**
     * @var \Company
     *
     * @ORM\OneToMany(targetEntity="\UserBundle\Entity\Company", mappedBy="user")
     */
    protected $company;

    public function __construct()
    {
        parent::__construct();
        $this->natural = new ArrayCollection();
        $this->company = new ArrayCollection();
    }

    /**
     * Get natural
     *
     * @return \UserBundle\Entity\Natural
     */
    public function getNatural()
    {
        return $this->natural;
    }
}

\UserBundle\Entity\Company.php:

class Company
{

    /**
     * @ORM\Column(name="social_reason", type="string", length=200, nullable=false)
     */
    protected $social_reason;

    /**
     * @var \User
     *
     * @ORM\ManyToOne(targetEntity="\UsuarioBundle\Entity\User", inversedBy="company")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * Get razon social
     *
     * @return decimal
     */
    public function getSocialReason()
    {
        return $this->social_reason;
    }

    /**
     * Set User
     *
     * @param \UserBundle\Entity\User $user
     * @return Company
     */
    public function setUser(\UserBundle\Entity\User $user = NULL)
    {
        $this->user = $user;
        return $this;
    }

    /**
     * Get User
     *
     * @return \UserBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }
}

\UserBundle\Entity\Natural.php:

class Natural
{

    /**
     * @ORM\Column(name="name", type="string", length=150)
     */
    protected $name;

    /**
     * @ORM\Column(name="lastname", type="string", length=200)
     */
    protected $lastname;

    /**
     * @var \User
     *
     * @ORM\ManyToOne(targetEntity="\UserBundle\Entity\User", inversedBy="company")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * Set User
     *
     * @param \UserBundle\Entity\User $user
     * @return Empresa
     */
    public function setUser(\UserBundle\Entity\User $user = NULL)
    {
        $this->user = $user;
        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Get lastname
     *
     * @return string
     */
    public function Lastname()
    {
        return $this->lastname;
    }

    /**
     * Get User
     *
     * @return \UserBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }
}

Having this relationship between entities, how in FOSUserBundle:Registration:email.txt.twig (which is override by me at \app) I can access methods from Natural entity or Company entity? Let's say I need from Natural the name getName and lastname getLastName? Why? Because I need to send extra info on the email sended to Users and that info is stored on that tables when Users got registered, any help?

Clarifying some things

  • User: is a system User from FOSUserBundle
  • Company and Natural: both are extensions of User the difference is which kind of information stores each one, for example a Company may have Social Reason, RIF (some kind of ID number), How many people works on that company, if it's a Regional Company or a main Company and Contact Person (this is not a system user is a normal entity linked just to Company) and a Natural may have a CI (some kind of ID number like DNI on European countries), Name, Lastname, Birthday, Sex and so on. Both Natural and Company shares some properties as for example username, email (canonicalEmail), address, Phone Number and some others, for that reason Company and Natural are linked to User and they can not exists if a User does not exists before

So the process here is:

  • Create the user and persist it using UserManager
  • Create a Natural/Company entity and set their properties from data in $request and yes this has to be done by hand for that you'll see setUser() (which set the current user created before as the FK for Natural/Company) and some others
  • Handle the relationship in a N:M entity UserHasService()
  • 写回答

1条回答 默认 最新

  • douju4278 2014-08-27 13:02
    关注

    I'm assuming in the Company and Natural entities you do actually have a Name field, also I'm assuming you have getters and setters in all of your entities, including getCompany/getNatural inside your user. Then do the following.

    If you set a toString in both Natural and Company like so:

    public function __toString()
    {
        return $this->getName();
    }
    

    That way in your template all you have to call is:

    {{ (user.company) }}
    

    "Doug I tried from the template itself as user.natural.getName but I got an empty string with this error Impossible to access an attribute ("getName") on a NULL variable ("")"

    I think if your calling .getName you might find just removing 'get' could fix it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路