Doctrine2 Relationships(ManyToMany)在ZF3中不起作用

时间:2018-03-23 19:43:48

标签: php doctrine-orm orm zend-framework3

我在这上花了2天时间仍然找不到解决方案为什么它不起作用。 我使用this教程在Zend Framework 3中实现了Doctrine。this站点是实体中ManyToMany关系的实现。我在我的项目(用户角色)中以1:1使用它但它不起作用。每当我想获得用户的所有角色( $ user-> getRoles())时,我都会获得 NULL

用户实体

ngAnimate

角色实体

<?php

namespace User\Entity;

use Doctrine\ORM\Mapping as ORM;
use Development\Entity\AbstractEntity;
use Development\Entity\Traits\EntityId;
use Development\Exception\EntityException;
use Doctrine\Common\Collections\ArrayCollection;
use Utils\StringUtils;

/**
 * @ORM\Entity(repositoryClass="User\Repository\UserRepository")
 * @ORM\Table(name="user")
 */
class User extends AbstractEntity
{
use EntityId;

/**
 * @ORM\Column(name="name")
 */
protected $name;

/**
 * @ORM\Column(name="surname")
 */
protected $surname;

/**
 * @ORM\Column(name="position")
 */
protected $position;

/**
 * @ORM\Column(name="phone")
 */
protected $phone;

/**
 * @ORM\Column(name="login")
 */
protected $login;

/**
 * @ORM\Column(name="password")
 */
protected $password;

/**
 * @ORM\ManyToMany(targetEntity="User\Entity\Role")
 * @ORM\JoinTable(name="user_role",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 *      )
 */
private $roles;

public function __construct()
{
    $this->roles = new ArrayCollection();
}

public function __toString()
{
    return $this->getFullName();
}

public function addRole($role)
{
    $this->roles[] = $role;
}

public function getRoles()
{
    return $this->roles;
}

public function removeRole($role)
{
    $this->roles->removeElement($role);
}

/**
 * @param integer $mbCase
 *
 * @return string
 *
 */
public function getFullName($mbCase = MB_CASE_TITLE)
{
    if (!StringUtils::isEmpty($this->surname)) {
        $fullName = mb_convert_case($this->name . ' ' . $this->surname, $mbCase, "UTF-8");
    } else {
        $fullName = mb_convert_case($this->name, $mbCase, "UTF-8");
    }

    return trim($fullName);
}

/**
 * @return string
 *
 */
public function getLogin()
{
    return $this->login;
}

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

/**
 * @param mixed $login
 */
public function setLogin($login)
{
    $this->login = StringUtils::toLower($login);
}
}

我正在使用最新的Doctrine ORM模块和Zend3。 你知道我应该在哪里寻找解决方案吗?如果需要,我会在接下来的8小时内添加代码。 这种学说关系的重要性是什么?其他关系很好。

3 个答案:

答案 0 :(得分:0)

您确定要在数据库中针对用户保存角色吗?我先检查一下。

这可能会有所帮助:Symfony2-Doctrine: ManyToMany relation is not saved to database

答案 1 :(得分:0)

我尝试了你的例子,它对我有用。夫妻俩要考虑的事情。 在Doctrine中,默认情况下,关联被标记为 Lazy ,这意味着关联的整个集合对象在第一次访问时会被填充。因此,为了填充您的角色,您必须执行以下操作:

    foreach ($user->getRoles() as $role) {
        echo $role->getName();
    }

或者,您可以在关联注释中使用fetch="EAGER"

/**
 * @ORM\ManyToMany(targetEntity="User\Entity\Role", fetch="EAGER")
 * @ORM\JoinTable(name="user_role",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 *      )
 */
private $roles;

结帐question,查看fetch="EAGER"fetch="LAZY"

之间的区别

答案 2 :(得分:0)

您确定角色实体是否正确:我何时使用:

{balance: 10000}
{balance: 10000}
{balance: 10000}
{balance: 10000}
{balance: 10000}
{balance: 10000}
{balance: 5000}
{balance: 5000}
{balance: 5000}
{balance: 5000}

我收到以下错误

./vendor/bin/doctrine-module orm:validate-schema

当我尝试使用orm时:相同:schema-tool:update --force

相关问题