duanke3985 2012-12-12 14:32
浏览 137
已采纳

Doctrine ORM关联的默认值

I have the entity (such as below). I want to set some default values while creating. As you can see in __construct, it is easy to set the $name (string), but how can I set the $group? (for example I know that there is a group in database with id=122)

/**
 * @ORM\Entity
 */
class Person {
    private $id;

    /** @ORM\Column(type="string") */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="persons")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    private $group;

    public function setGroup(Group $group)
    {
        $this->group = $group;
        $group->addPerson($this);
    }

    // ... setters/getters

    //construct default Person
    public function __construct()
    {
        $this->setName("Mike");
        $this->setGroup($EXISTING_GROUP_FROM_MY_DB); // <<--------------
    }
}
  • 写回答

2条回答 默认 最新

  • duanli6618 2012-12-12 16:23
    关注

    I agree with moonwave99 that this is poor design. Here you are trying to access the database (through the Doctrine service) from a place that is not container-aware (i.e. it does not, and should not, know about Doctrine).

    I had a similar issue recently... pretty much the same exact issue, actually. But I didn't want this logic to be inside the controller. So I wrote a service to take care of the User creation. And I gave that service access to the only other service it needed: Doctrine.

    Here's an example, where a User is created with all available Roles:

    namespace MyBundle\Entity;
    
    class UserFactory
    {
        private $doctrine;
    
        public function __construct($doctrine)
        {
            $this->doctrine = $doctrine;
        }
    
        public function generateNewUser($email, $password)
        {
            $user = new User();
    
            // Since you have access to the Doctrine service, you can use $this->doctrine
            // to do anything you would normally do in your controller with $this->getDoctrine()
            $roles = $this->doctrine->getEntityManager()->getRepository("MyBundle:Role")->findAll();
    
            foreach ($roles as $role)
            {
                $user->addRole($role);
            }
    
            return $user;
        }
    }
    

    Now register that service in config.yml or services.yml, remembering to pass the Doctrine service to it:

    services:
        mybundle.factory.user:
            class: MyBundle\Entity\UserFactory
            arguments: ['@doctrine']
    

    And that's it... Now, in your controller, you can create a new User by doing:

    public function MyController()
    {
        $user = $this->get("mybundle.factory.user")->generateNewUser("someone@email.com", "password123");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入