2013-10-17 45 views
0

如何從mappipng表中獲取映射表的ID?symfony2如何獲得映射表的ID?

我有用戶和組表映射在一起 我想在登錄時獲取組ID,但我沒有得到它。

我有三個表 用戶,組,user_groupname

- >第一表

用戶

ID,名稱

- >第二表

ID,名稱

- >第三表

組名

USER_ID,GROUP_ID

第一實體是如下

/Entity/groupname.php 

class groupname 
{ 


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

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

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ORM\ManyToMany(targetEntity="role", inversedBy="groupname") 
    * @ORM\JoinTable(name="groupname_role", 
    * joinColumns={ 
    *  @ORM\JoinColumn(name="groupname_id", referencedColumnName="id") 
    * }, 
    * inverseJoinColumns={ 
    *  @ORM\JoinColumn(name="role_id", referencedColumnName="id") 
    * } 
    *) 
    */ 
    private $role; 

    /** 
    * @ORM\ManyToMany(targetEntity="\Dashboard\SecurityBundle\Entity\User", mappedBy="groupname") 
    */ 
    private $users; 

} 

小號的Econd實體如下

/Entity/User.php 
<?php 

namespace Dashboard\SecurityBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\User\AdvancedUserInterface; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* Dashboard\SecurityBundle\Entity\User 
* 
* @ORM\Table(name="users") 
* @ORM\Entity(repositoryClass="Dashboard\SecurityBundle\Repository\UserRepository") 
* @ORM\HasLifecycleCallbacks() 
*/ 
class User implements UserInterface, \Serializable, AdvancedUserInterface 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=25, unique=true) 
    */ 
    protected $username; 

    */ 

    /** 
    * @ORM\ManyToMany(targetEntity="\Dashboard\AdminManageUserBundle\Entity\groupname", inversedBy="users") 
    * 
    */ 
    public $groupname; 

    /** 
    * @var Dashboard\SecurityBundle\Entity\UserPhoto UserPhoto 
    * 
    */ 
    private $userPhoto; 

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

    } 

    /** 
    * Add groupname 
    * 
    * @param \Dashboard\AdminManageUserBundle\Entity\groupname $groupname 
    * @return User 
    */ 
    public function addGroupname(\Dashboard\AdminManageUserBundle\Entity\groupname $groupname) 
    { 
     $this->groupname[] = $groupname; 

     return $this; 
    } 

    /** 
    * Remove groupname 
    * 
    * @param \Dashboard\AdminManageUserBundle\Entity\groupname $groupname 
    */ 
    public function removeGroupname(\Dashboard\AdminManageUserBundle\Entity\groupname $groupname) 
    { 
     $this->groupname->removeElement($groupname); 
    } 

    /** 
    * Get groupname 
    * 
    * @return \Doctrine\Common\Collections\ArrayCollection 
    */ 
    public function getGroupname() 
    { 

     return $this->groupname; 
    } 
} 

和我就在Controller文件

$userEntity = $this->getDoctrine()->getRepository('DashboardSecurityBundle:User')->findOneBy(array('username' =>$usernames)); 

下面的代碼,我有print_R($userEntity) PC得到了上吊

回答

0

$ userEntiry是UserEntity

的對象

所以,你的電腦被絞死了。

請試試這個。

print_r($userEntity->getUsername()); 
+0

我已經allready嘗試這個print_r($ userEntity-> getUsername());但火狐掛起 並沒有得到我的組名稱ID –