在FOSUser寄存器窗体中添加具有OneToOne关系的字段

时间:2013-04-29 11:27:20

标签: symfony doctrine fosuserbundle

我需要你的帮助才能在FOSUSerBundle的寄存器表单中添加一些字段。 我想本地化用户并以寄存器形式保存他的纬度/经度。 我在我的用户实体和我的本地化实体之间创建了一个oneToOne关系,但我没有看到我可以在代码寄存阶段设置代码来设置用户的纬度和经度。

我是否必须覆盖寄存器控制器或某些事件?我只是看不到放我代码的地方......

首先,此处的文档未针对新签名方法进行更新:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md

本教程没问题,但我没看到如何将它与oneToOne字段一起使用......: http://www.idci-consulting.fr/fosuserbundle-comment-gerer-les-utilisateurs-avec-symfony2/

事实上,我在用于FOSU​​serBundle的用户实体中添加了我的关系:

<?php

namespace rs\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use FOS\MessageBundle\Model\ParticipantInterface;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User extends BaseUser implements ParticipantInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="rs\UserBundle\Entity\Localisation", cascade={"persist"})
     */
    protected $localisation;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set localisation
     *
     * @param \rs\UserBundle\Entity\Localisation $localisation
     * @return User
     */
    public function setLocalisation(\rs\UserBundle\Entity\Localisation $localisation = null)
    {
        $this->localisation = $localisation;

        return $this;
    }

    /**
     * Get localisation
     *
     * @return \rs\UserBundle\Entity\Localisation 
     */
    public function getLocalisation()
    {
        return $this->localisation;
    }
}

这是我的本地化实体:

<?php

namespace rs\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Localisation
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Localisation
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @var float
     *
     * @ORM\Column(name="latitude", type="decimal")
     */
    private $latitude;

    /**
     * @var float
     *
     * @ORM\Column(name="longitude", type="decimal")
     */
    private $longitude;

    /**
     * @var integer
     *
     * @ORM\OneToOne(targetEntity="rs\UserBundle\Entity\Clan", cascade={"persist"})
     */
    private $clan;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set latitude
     *
     * @param float $latitude
     * @return Localisation
     */
    public function setLatitude($latitude)
    {
        $this->latitude = $latitude;

        return $this;
    }

    /**
     * Get latitude
     *
     * @return float 
     */
    public function getLatitude()
    {
        return $this->latitude;
    }

    /**
     * Set longitude
     *
     * @param float $longitude
     * @return Localisation
     */
    public function setLongitude($longitude)
    {
        $this->longitude = $longitude;

        return $this;
    }

    /**
     * Get longitude
     *
     * @return float 
     */
    public function getLongitude()
    {
        return $this->longitude;
    }



    /**
     * Set clan
     *
     * @param \rs\UserBundle\Entity\Clan $clan
     * @return Localisation
     */
    public function setClan(\robStorm\UserBundle\Entity\Clan $clan = null)
    {
        $this->clan = $clan;

        return $this;
    }

    /**
     * Get clan
     *
     * @return \rs\UserBundle\Entity\Clan 
     */
    public function getClan()
    {
        return $this->clan;
    }
}

谢谢;)

1 个答案:

答案 0 :(得分:0)

您必须覆盖/扩展现有的RegistrationFormType。请参阅文档中的overriding forms。在您的类型中,您将位置添加为子表单。所以你也必须创建一个LocationFormType。

如果您需要处理某些logig,则必须hook into the controller with events。不幸的是,事件仅在dev-master中可用,而不是在稳定版本中。如果您被迫使用稳定版本,则必须override the controller并复制旧版本中的所有逻辑,然后添加逻辑。