dongrong7883 2014-01-02 23:04
浏览 41
已采纳

如何将region_id从当前登录的用户分配到实体表 - Symfony2

I have a users table with a foreign key: person_id. The persons table has a foreign key address_id. The addresses table has a foreign key region_id. Now I want to make an entity. But the entities table has also a few foreign keys: user_id, region_id, category_id.

I assign the user_id in the entities table with this:

$entity = new Entities();
$entity->setUser($this->get('security.context')->getToken()->getUser());

Now I want to know how you sign the region_id to the entities table? That region_id is already assigned in the addresses table.

Create action of EntityController

public function createAction(Request $request)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $entity = new Entities();

        $entity->setUser($this->get('security.context')->getToken()->getUser());

        $form = $this->createForm(new EntityType(), $entity);

        if ($request->getMethod() == 'POST')
        {
            $form->bind($request);

            if($form->isValid())
            {
                $date = new \DateTime('NOW');
                $entity->setentityCreated($date);

                $entityManager = $this->getDoctrine()->getManager();

                $entityManager->persist($entity);

                $entityManager->flush();

                return $this->redirect($this->generateUrl('reuzze_reuzze_homepage'));
            }
        }

        return $this->render('ReuzzeReuzzeBundle:Entity:create.html.twig', array(
            'form' => $form->createView(),
        ));
    }

Users Entity

class Users implements UserInterface
{
    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $user_id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=45, nullable=false)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=60, nullable=false)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=30, nullable=false)
     */
    private $salt;

    /**
     * @var string
     *
     * @ORM\Column(name="user_email", type="string", length=255, nullable=false)
     */
    private $userEmail;

    /**
     * @var integer
     *
     * @ORM\Column(name="user_rating", type="integer", nullable=false)
     */
    private $userRating;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_created", type="datetime", nullable=false)
     */
    private $userCreated;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_modified", type="datetime", nullable=true)
     */
    private $userModified;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_deleted", type="datetime", nullable=true)
     */
    private $userDeleted;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_lastlogin", type="datetime", nullable=true)
     */
    private $userLastlogin;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_locked", type="datetime", nullable=true)
     */
    private $userLocked;

    /**
     * @var \Reuzze\ReuzzeBundle\Entity\Persons
     *
     * @ORM\OneToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Persons")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="person_id", referencedColumnName="person_id", unique=true)
     * })
     */
    protected $person;

    /**
     * @var \Reuzze\ReuzzeBundle\Entity\Roles
     *
     * @ORM\ManyToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Roles")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
     * })
     */
    protected $role;

    private $plainPassword;

    public function __construct(){
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }

    /**
     * Set user_id
     *
     * @param integer $user_id
     * @return Users
     */
    public function setUserId($user_id)
    {
        $this->user_id = $user_id;

        return $this;
    }

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

    /**
     * Set username
     *
     * @param string $username
     * @return Users
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

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

    /**
     * Set password
     *
     * @param string $password
     * @return Users
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

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

    /**
     * Set salt
     *
     * @param string $salt
     * @return Users
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

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

    /**
     * Set userEmail
     *
     * @param string $userEmail
     * @return Users
     */
    public function setUserEmail($userEmail)
    {
        $this->userEmail = $userEmail;

        return $this;
    }

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

    /**
     * Set userRating
     *
     * @param integer $userRating
     * @return Users
     */
    public function setUserRating($userRating)
    {
        $this->userRating = $userRating;

        return $this;
    }

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

    /**
     * Set userCreated
     *
     * @param \DateTime $userCreated
     * @return Users
     */
    public function setUserCreated($userCreated)
    {
        $this->userCreated = $userCreated;

        return $this;
    }

    /**
     * Get userCreated
     *
     * @return \DateTime
     */
    public function getUserCreated()
    {
        return $this->userCreated;
    }

    /**
     * Set userModified
     *
     * @param \DateTime $userModified
     * @return Users
     */
    public function setUserModified($userModified)
    {
        $this->userModified = $userModified;

        return $this;
    }

    /**
     * Get userModified
     *
     * @return \DateTime
     */
    public function getUserModified()
    {
        return $this->userModified;
    }

    /**
     * Set userDeleted
     *
     * @param \DateTime $userDeleted
     * @return Users
     */
    public function setUserDeleted($userDeleted)
    {
        $this->userDeleted = $userDeleted;

        return $this;
    }

    /**
     * Get userDeleted
     *
     * @return \DateTime
     */
    public function getUserDeleted()
    {
        return $this->userDeleted;
    }

    /**
     * Set userLastlogin
     *
     * @param \DateTime $userLastlogin
     * @return Users
     */
    public function setUserLastlogin($userLastlogin)
    {
        $this->userLastlogin = $userLastlogin;

        return $this;
    }

    /**
     * Get userLastlogin
     *
     * @return \DateTime
     */
    public function getUserLastlogin()
    {
        return $this->userLastlogin;
    }

    /**
     * Set userLocked
     *
     * @param \DateTime $userLocked
     * @return Users
     */
    public function setUserLocked($userLocked)
    {
        $this->userLocked = $userLocked;

        return $this;
    }

    /**
     * Get userLocked
     *
     * @return \DateTime
     */
    public function getUserLocked()
    {
        return $this->userLocked;
    }

    /**
     * Set person
     *
     * @param \Reuzze\ReuzzeBundle\Entity\Persons $person
     * @return Users
     */
    public function setPerson(\Reuzze\ReuzzeBundle\Entity\Persons $person = null)
    {
        $this->person = $person;

        return $this;
    }

    /**
     * Get person
     *
     * @return \Reuzze\ReuzzeBundle\Entity\Persons
     */
    public function getPerson()
    {
        return $this->person;
    }

    /**
     * Set role
     *
     * @param \Reuzze\ReuzzeBundle\Entity\Roles $role
     * @return Users
     */
    public function setRoles(\Reuzze\ReuzzeBundle\Entity\Roles $role = null)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get role
     *
     * @return \Reuzze\ReuzzeBundle\Entity\Roles
     */
    public function getRoles()
    {
        return ['ROLE_USER'];
    }

    public function eraseCredentials(){
        $this->setPlainPassword(null);
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($plainPassword)
    {
        $this->plainPassword = $plainPassword;
    }
}

Entities Entity

class Entities
{
    /**
     * @var integer
     *
     * @ORM\Column(name="entity_id", type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $entityId;

    /**
     * @var string
     *
     * @ORM\Column(name="entity_title", type="string", length=255, nullable=false)
     */
    private $entityTitle;

    /**
     * @var string
     *
     * @ORM\Column(name="entity_description", type="text", nullable=false)
     */
    private $entityDescription;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="entity_starttime", type="datetime", nullable=false)
     */
    private $entityStarttime;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="entity_endtime", type="datetime", nullable=false)
     */
    private $entityEndtime;

    /**
     * @var string
     *
     * @ORM\Column(name="entity_instantsellingprice", type="decimal", precision=12, scale=2, nullable=false)
     */
    private $entityInstantsellingprice;

    /**
     * @var string
     *
     * @ORM\Column(name="entity_shippingprice", type="decimal", precision=6, scale=2, nullable=true)
     */
    private $entityShippingprice;

    /**
     * @var string
     *
     * @ORM\Column(name="entity_condition", type="string", nullable=false)
     */
    private $entityCondition;

    /**
     * @var integer
     *
     * @ORM\Column(name="entity_views", type="bigint", nullable=true)
     */
    private $entityViews;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="entity_created", type="datetime", nullable=false)
     */
    private $entityCreated;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="entity_modified", type="datetime", nullable=true)
     */
    private $entityModified;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="entity_deleted", type="datetime", nullable=true)
     */
    private $entityDeleted;

    /**
     * @var \Reuzze\ReuzzeBundle\Entity\Users
     *
     * @ORM\ManyToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Users")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     * })
     */
    private $user;

    /**
     * @var \Reuzze\ReuzzeBundle\Entity\Regions
     *
     * @ORM\ManyToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Regions")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="region_id", referencedColumnName="region_id")
     * })
     */
    private $region;

    /**
     * @var \Reuzze\ReuzzeBundle\Entity\Categories
     *
     * @ORM\ManyToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Categories")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="category_id", referencedColumnName="category_id")
     * })
     */
    private $category;



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

    /**
     * Set entityTitle
     *
     * @param string $entityTitle
     * @return Entities
     */
    public function setEntityTitle($entityTitle)
    {
        $this->entityTitle = $entityTitle;

        return $this;
    }

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

    /**
     * Set entityDescription
     *
     * @param string $entityDescription
     * @return Entities
     */
    public function setEntityDescription($entityDescription)
    {
        $this->entityDescription = $entityDescription;

        return $this;
    }

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

    /**
     * Set entityStarttime
     *
     * @param \DateTime $entityStarttime
     * @return Entities
     */
    public function setEntityStarttime($entityStarttime)
    {
        $this->entityStarttime = $entityStarttime;

        return $this;
    }

    /**
     * Get entityStarttime
     *
     * @return \DateTime 
     */
    public function getEntityStarttime()
    {
        return $this->entityStarttime;
    }

    /**
     * Set entityEndtime
     *
     * @param \DateTime $entityEndtime
     * @return Entities
     */
    public function setEntityEndtime($entityEndtime)
    {
        $this->entityEndtime = $entityEndtime;

        return $this;
    }

    /**
     * Get entityEndtime
     *
     * @return \DateTime 
     */
    public function getEntityEndtime()
    {
        return $this->entityEndtime;
    }

    /**
     * Set entityInstantsellingprice
     *
     * @param string $entityInstantsellingprice
     * @return Entities
     */
    public function setEntityInstantsellingprice($entityInstantsellingprice)
    {
        $this->entityInstantsellingprice = $entityInstantsellingprice;

        return $this;
    }

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

    /**
     * Set entityShippingprice
     *
     * @param string $entityShippingprice
     * @return Entities
     */
    public function setEntityShippingprice($entityShippingprice)
    {
        $this->entityShippingprice = $entityShippingprice;

        return $this;
    }

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

    /**
     * Set entityCondition
     *
     * @param string $entityCondition
     * @return Entities
     */
    public function setEntityCondition($entityCondition)
    {
        $this->entityCondition = $entityCondition;

        return $this;
    }

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

    /**
     * Set entityViews
     *
     * @param integer $entityViews
     * @return Entities
     */
    public function setEntityViews($entityViews)
    {
        $this->entityViews = $entityViews;

        return $this;
    }

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

    /**
     * Set entityCreated
     *
     * @param \DateTime $entityCreated
     * @return Entities
     */
    public function setEntityCreated($entityCreated)
    {
        $this->entityCreated = $entityCreated;

        return $this;
    }

    /**
     * Get entityCreated
     *
     * @return \DateTime 
     */
    public function getEntityCreated()
    {
        return $this->entityCreated;
    }

    /**
     * Set entityModified
     *
     * @param \DateTime $entityModified
     * @return Entities
     */
    public function setEntityModified($entityModified)
    {
        $this->entityModified = $entityModified;

        return $this;
    }

    /**
     * Get entityModified
     *
     * @return \DateTime 
     */
    public function getEntityModified()
    {
        return $this->entityModified;
    }

    /**
     * Set entityDeleted
     *
     * @param \DateTime $entityDeleted
     * @return Entities
     */
    public function setEntityDeleted($entityDeleted)
    {
        $this->entityDeleted = $entityDeleted;

        return $this;
    }

    /**
     * Get entityDeleted
     *
     * @return \DateTime 
     */
    public function getEntityDeleted()
    {
        return $this->entityDeleted;
    }

    /**
     * Set user
     *
     * @param \Reuzze\ReuzzeBundle\Entity\Users $user
     * @return Entities
     */
    public function setUser(\Reuzze\ReuzzeBundle\Entity\Users $user = null)
    {
        $this->user = $user;

        return $this;
    }

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

    /**
     * Set region
     *
     * @param \Reuzze\ReuzzeBundle\Entity\Regions $region
     * @return Entities
     */
    public function setRegion(\Reuzze\ReuzzeBundle\Entity\Regions $region = null)
    {
        $this->region = $region;

        return $this;
    }

    /**
     * Get region
     *
     * @return \Reuzze\ReuzzeBundle\Entity\Regions 
     */
    public function getRegion()
    {
        return $this->region;
    }

    /**
     * Set category
     *
     * @param \Reuzze\ReuzzeBundle\Entity\Categories $category
     * @return Entities
     */
    public function setCategory(\Reuzze\ReuzzeBundle\Entity\Categories $category = null)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \Reuzze\ReuzzeBundle\Entity\Categories 
     */
    public function getCategory()
    {
        return $this->category;
    }
}
  • 写回答

1条回答 默认 最新

  • duan5991518 2014-01-02 23:18
    关注

    Fields mapping was the problem. It should look like this:

    /**
    * @var \Reuzze\ReuzzeBundle\Entity\Persons
    *
    * @ORM\OneToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Persons", fetch="EAGER") * @ORM\JoinColumns({
    *     @ORM\JoinColumn(name="person_id", referencedColumnName="person_id", unique=true)
    * })
    */
    protected $person;

    fetch="EAGER" param is the solution. Same goes for entities person and address. Then you can assign region to entity:

    $entity->setRegion($this->getUser()->getPerson()->getAddress()->getRegion());

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

报告相同问题?

悬赏问题

  • ¥15 dev运行环境,c语言,报错是为什么?怎么改
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能