教义ORM关联的默认值 [英] Default value of Doctrine ORM association

查看:117
本文介绍了教义ORM关联的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体(如下)。我想在创建时设置一些默认值。
正如你在 __构造中可以看到的,很容易设置 $ name (string),但是如何设置 $ group ? (例如,我知道在数据库中有一个 id = 122 的组)

 $ b * / 
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

//构造默认Person
public function __construct()
{
$ this-> setName(Mike);
$ this-> setGroup($ EXISTING_GROUP_FROM_MY_DB); //< --------------
}
}


解决方案

我同意moonwave99这是糟糕的设计。在这里,您尝试从不是容器感知的地方访问数据库(通过Doctrine服务)(即不知道Doctrine不应该知道)。



实际上,我最近有一个类似的问题...几乎是相同的确切问题。但我不希望这个逻辑在控制器内。所以我写了一个服务来照顾用户创建。而且我把这个服务访问了唯一需要的其他服务:Doctrine。



这里有一个例子,用户创建了所有可用的角色:

 命名空间MyBundle\Entity; 

class UserFactory
{
private $ doctrine;

公共函数__construct($ doctrine)
{
$ this-> doctrine = $ doctrine;
}

public function generateNewUser($ email,$ password)
{
$ user = new User();

//由于您可以访问Doctrine服务,您可以使用$ this-> doctrine
//来执行您通常使用$ this-> getDoctrine()
$ roles = $ this-> doctrine-> getEntityManager() - > getRepository(MyBundle:Role) - > findAll();

foreach($ roles as $ role)
{
$ user-> addRole($ role);
}

return $ user;
}
}

现在在 config.yml services.yml ,记得要通过Doctrine服务:

 服务:
mybundle.factory.user:
类:MyBundle\Entity\UserFactory
参数:['@doctrine']

就是这样...现在,在控制器中,您可以通过执行以下操作创建一个新用户: p>

  public function MyController()
{
$ user = $ this-> get(mybundle.factory .user) - > generateNewUser(someone@email.com,password123);
}


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); // <<--------------
    }
}

解决方案

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");
}

这篇关于教义ORM关联的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆