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

I have created two bundle using two different vendor names. Two bundle names is-

  • SystemUsersBundle
    • Namespace= SystemUsersBundle\Namespace
  • AppBundle
    • Namespace= AppBundle\Namespace

Now, I have created two Entity inside two bundle. Entity name given below-

  • Comment.php – this entity created under AppBundle and it’s namespace is AppBundle\Entity.
  • Users.php – this entity created under SystemUsersBundle and it’s namespace is SystemUsersBundle\Entity.

My directory Structure is-

enter image description here

Now, I wanted to make a relationship between Users.php and Comment.php entity. For this purpose I make a relation between them according to the rule of Doctrine ManyToOne relationship.

Users.php and Comment.php file given below-

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

In comment.php file, make relationship using

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

it's give the following error-

enter image description here

But if I wanted make relationship with two entity under same Bundle(like SystemUsersBundle or AppBundle) it works perfectly ok. If you need more information let me know.

The provided syntax @ORM\ManyToOne(targetEntity="Users") means that given entity is at the same namespace as of the current file. Which is not the case. Your mapping should be like this:

@ORM\ManyToOne(targetEntity="SystemUsersBundle\Entity\Users")

When entities are in different namespace, you should always provide the full path.

If you need to set a relation between your two entities just make it all in one bundle, why you use two bundle if they depends on each other. take a look at this :


A bundle is meant to be something that can be reused as a stand-alone piece of software. If UserBundle cannot be used "as is" in other Symfony apps, then it shouldn't be its own bundle. Moreover, if InvoiceBundle depends on ProductBundle, then there's no advantage to having two separate bundles.