Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 原则2-实体通过密钥持有者表收集其他实体_Php_Symfony_Orm_Doctrine Orm - Fatal编程技术网

Php 原则2-实体通过密钥持有者表收集其他实体

Php 原则2-实体通过密钥持有者表收集其他实体,php,symfony,orm,doctrine-orm,Php,Symfony,Orm,Doctrine Orm,我希望实现这种类型的关系: 类SimpleUser必须具有“languages”类属性,该属性应包含绑定到UserLanguages表中当前用户的所有SimpleLanguage-s的列表 请看以下课程: public class SimpleUser { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedVa

我希望实现这种类型的关系:

类SimpleUser必须具有“languages”类属性,该属性应包含绑定到UserLanguages表中当前用户的所有SimpleLanguage-s的列表

请看以下课程:

public class SimpleUser {

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

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

    // what do I type here? I want to have SimpleLanguage[] here
    private $languages;
}



首先删除$userId;和$languageId来自您的类UserLanguages!它们是无用的,它们是管理数据库中实体id的属性$user和$language

下面介绍如何在SImpleUser中管理languages属性:

use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleUser
{
    //...

    /**
     * @ORM\OneToMany(targetEntity="UserLanguages", mappedBy="user")
     */
    protected $languages;

    public function __construct()
    {
        $this->languages = new ArrayCollection();
    }
}
use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleUser
{
    //...

    /**
     * @ORM\ManyToMany(targetEntity="SimpleLanguage", mappedBy="users")
     */
    protected $languages;

    public function __construct()
    {
        $this->languages = new ArrayCollection();
    }
}
在SimpleLanguage中类似:

use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleLanguage
{
    //...

    /**
     * @ORM\OneToMany(targetEntity="UserLanguages", mappedBy="language")
     */
    protected $languages;

    public function __construct()
    {
        $this->languages = new ArrayCollection();
    }
}
use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleLanguage
{
    //...

    /**
     * @ORM\ManyToMany(targetEntity="SimpleUser", mappedBy="languages")
     * @JoinTable(name="UserLanguages")
     */
    protected $users;

    public function __construct()
    {
        $this->users = new ArrayCollection();
    }
}
编辑:


我不想收集“用户语言”,而是直接收集 从“用户语言”表中收集“SimpleLanguage”

好的,目前这是不可能的,我想我想要一个多人关系而不是第三实体用户语言(你的问题不是很清楚)

删除无用的实体UserLanguages,然后在SimpleUser中:

use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleUser
{
    //...

    /**
     * @ORM\OneToMany(targetEntity="UserLanguages", mappedBy="user")
     */
    protected $languages;

    public function __construct()
    {
        $this->languages = new ArrayCollection();
    }
}
use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleUser
{
    //...

    /**
     * @ORM\ManyToMany(targetEntity="SimpleLanguage", mappedBy="users")
     */
    protected $languages;

    public function __construct()
    {
        $this->languages = new ArrayCollection();
    }
}
和简单语言:

use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleLanguage
{
    //...

    /**
     * @ORM\OneToMany(targetEntity="UserLanguages", mappedBy="language")
     */
    protected $languages;

    public function __construct()
    {
        $this->languages = new ArrayCollection();
    }
}
use Doctrine\Common\Collections\ArrayCollection;

// ...

class SimpleLanguage
{
    //...

    /**
     * @ORM\ManyToMany(targetEntity="SimpleUser", mappedBy="languages")
     * @JoinTable(name="UserLanguages")
     */
    protected $users;

    public function __construct()
    {
        $this->users = new ArrayCollection();
    }
}
它将生成第三个表UserLanguages,该表具有user\u id和language\u id

您将能够从用户类中获得一次使用链接的所有语言的列表:

$languages = $user->getLanguages();
与语言相反:

$users = $language->getUsers();

您正在寻找与联接表的单向一对多关系,该联接表在带有@manytomy注释的条令中描述。这可能导致混乱

您甚至不需要粘合UserLanguages类。。。条令会帮你收拾桌子的

简化程序

简单语言

现在,条令将为您创建UserLanguages表,并将id(user\u id,language\u id)保存在其中


阅读有关关联映射与联接表的更多信息。

我不希望收集
“用户语言”
,而是希望直接收集“用户语言”表中的
“SimpleLanguage”
。请查看我的编辑您可能想要多个关系@Sybio-您与@nifr同时回答,但他的回答更正确,因为首先我遵循了您的回答,错误是使用了
mappedBy
而不是
inversedBy
。不过还是要谢谢你!