在与Symfony2中的两个不同供应商的捆绑关系时显示“未找到实体”的实体关系

时间:2015-04-27 11:35:05

标签: php symfony doctrine-orm

我使用两个不同的供应商名称创建了两个捆绑包。两个包名称是 -

  • SystemUsersBundle
    • 命名空间 = SystemUsersBundle \ 命名空间
  • 的appbundle
    • 命名空间 = AppBundle \ 命名空间

现在,我在两个bundle中创建了两个Entity。下面给出的实体名称 -

  • Comment.php - 在AppBundle下创建此实体及其命名空间 是AppBundle \ Entity。
  • Users.php - 在SystemUsersBundle下创建的这个实体就是它 命名空间是SystemUsersBundle \ Entity。

我的目录结构是 -

enter image description here

现在,我想在Users.php和Comment.php实体之间建立关系。为此,我根据Doctrine ManyToOne关系的规则建立了它们之间的关系。

下面给出的Users.php和Comment.php文件 -

Users.php

<?php

namespace SystemUsersBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Users
 *
 * @ORM\Table("users")
 */
class Users implements AdvancedUserInterface
{

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

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

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

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

    /**
     * @Gedmo\Slug(fields={"name"}, updatable=false)
     * @ORM\Column(length=255, unique=true)
     */
    private $slug;

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

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

    /**
     * @var string
     */
    private $plainpassword;

    /**
     * @var string
     */
    private $resetPassword;

    /**
     * @var array
     *
     * @ORM\Column(name="roles", type="array")
     */
    private $roles = array();

    /**
     * @var boolean
     *
     * @ORM\Column(name="app_status", type="boolean")
     */
    private $appStatus;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_active",type="boolean")
     */
    private $isActive;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    private $updatedAt;

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

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->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 name
     *
     * @param string $name
     * @return Users
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * 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 plainpassword for the application
     *
     * @param type $plainpassword
     */
    function setPlainpassword($plainpassword)
    {
        $this->plainpassword = $plainpassword;
    }

    /**
     * Get plainpassword for the application
     *
     * @return type
     */
    function getPlainpassword()
    {
        return $this->plainpassword;
    }

    /**
     * Set resetPassword for the application
     *
     * @param type $resetPassword
     */
    function setResetPassword($resetPassword)
    {
        $this->resetPassword = $resetPassword;
    }

    /**
     * Get plainpassword for the application
     *
     * @return type
     */
    function getResetPassword()
    {
        return $this->resetPassword;
    }

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

        return $this;
    }

    /**
     * Get roles
     *
     * @return string
     */
    public function getRoles()
    {
        $roles   = $this->roles;
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    /**
     *  set salt property of user
     *
     * @return type
     */
    function getSalt()
    {
        return $this->salt;
    }

    /**
     * Get salt value of user
     *
     * @param type $salt
     */
    function setSalt($salt)
    {
        $this->salt = $salt;
    }

    /**
     * Set appStatus
     *
     * @param boolean $appStatus
     * @return Users
     */
    public function setAppStatus($appStatus)
    {
        $this->appStatus = $appStatus;

        return $this;
    }

    /**
     * Get appStatus
     *
     * @return boolean
     */
    public function getAppStatus()
    {
        return $this->appStatus;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return Users
     */
    public function setCreatedAt($createdAt)
    {

        $this->createdAt = $createdAt;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Get Is active property
     *
     * @return type
     */
    function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Set Is active property
     *
     * @param \SystemUsersBundle\Entity\type $isActive
     * @return $users
     */
    function setIsActive($isActive)
    {
        $this->isActive = $isActive;
        return $this;
    }

    /**
     * erase plain password credentials
     */
    public function eraseCredentials()
    {
        $this->setPlainpassword(null);
    }

    /**
     * Checks whether the user's account has expired.
     *
     * Internally, if this method returns false, the authentication system
     * will throw an AccountExpiredException and prevent login.
     *
     * @return bool true if the user's account is non expired, false otherwise
     *
     * @see AccountExpiredException
     */
    public function isAccountNonExpired()
    {
        return true;
    }

    /**
     * Checks whether the user is locked.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a LockedException and prevent login.
     *
     * @return bool true if the user is not locked, false otherwise
     *
     * @see LockedException
     */
    public function isAccountNonLocked()
    {
        return true;
    }

    /**
     * Checks whether the user's credentials (password) has expired.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a CredentialsExpiredException and prevent login.
     *
     * @return bool true if the user's credentials are non expired, false otherwise
     *
     * @see CredentialsExpiredException
     */
    public function isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * Checks whether the user is enabled.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a DisabledException and prevent login.
     *
     * @return bool true if the user is enabled, false otherwise
     *
     * @see DisabledException
     */
    public function isEnabled()
    {
        return $this->getIsActive();
    }

Comment.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SystemUsersBundle\Entity\Users;

/**
 * Comment
 *
 * @ORM\Table("comment")
 * @ORM\Entity
 */
class Comment
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer")
     */
    private $userId;

    /**
     * @var boolean
     *
     * @ORM\Column(name="status", type="boolean")
     */
    private $status;

    /**
     *
     * @ORM\ManyToOne(targetEntity="Users")
     */
    protected $owner;

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

    /**
     * Set title
     *
     * @param string $title
     * @return Comment
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set status
     *
     * @param boolean $status
     * @return Comment
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return boolean 
     */
    public function getStatus()
    {
        return $this->status;
    }
}

在comment.php文件中,使用

建立关系
    /**
     *
     * @ORM\ManyToOne(targetEntity="Users")
     */
    protected $owner;

它会出现以下错误 -

enter image description here

但是,如果我想在同一个Bundle(如SystemUsersBundle或AppBundle)下与两个实体建立关系,那么它的效果非常好。如果您需要更多信息,请告诉我们。

2 个答案:

答案 0 :(得分:0)

提供的语法@ORM\ManyToOne(targetEntity="SystemUsersBundle\Entity\Users") 表示给定实体与当前文件位于同一名称空间。事实并非如此。你的映射应该是这样的:

global $user;
$account = user_load($user->uid);
print_r($account);

当实体位于不同的命名空间中时,您应始终提供完整路径。

答案 1 :(得分:0)

如果您需要在两个实体之间设置关系,只需将它们全部放在一个包中,为什么如果它们相互依赖则使用两个包。 看看this

捆绑包是指可以作为独立软件重用的东西。如果不能使用UserBundle&#34;按原样#34;在其他Symfony应用程序中,它不应该是自己的捆绑包。此外,如果InvoiceBundle依赖于ProductBundle,那么拥有两个单独的包没有任何优势。