谢谢cheesemacfly!...我的方法之一
用户实体
命名空间 Hta\CoreBundle\Entity;
使用 Doctrine\ORM\Mapping 作为 ORM;
/** * HtaUser / class HtaUser { / * * @var string */ private $username;
/**
* @var string
*/
private $usernameCanonical;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $emailCanonical;
/**
* @var boolean
*/
private $enabled;
/**
* @var string
*/
private $salt;
/**
* @var string
*/
private $password;
/**
* @var \DateTime
*/
private $lastLogin;
/**
* @var boolean
*/
private $locked;
/**
* @var boolean
*/
private $expired;
/**
* @var \DateTime
*/
private $expiresAt;
/**
* @var string
*/
private $confirmationToken;
/**
* @var \DateTime
*/
private $passwordRequestedAt;
/**
* @var array
*/
private $roles;
/**
* @var boolean
*/
private $credentialsExpired;
/**
* @var \DateTime
*/
private $credentialsExpireAt;
/**
* @var integer
*/
private $id;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $group;
/**
* Constructor
*/
public function __construct()
{
$this->group = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set username
*
* @param string $username
* @return HtaUser
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set usernameCanonical
*
* @param string $usernameCanonical
* @return HtaUser
*/
public function setUsernameCanonical($usernameCanonical)
{
$this->usernameCanonical = $usernameCanonical;
return $this;
}
/**
* Get usernameCanonical
*
* @return string
*/
public function getUsernameCanonical()
{
return $this->usernameCanonical;
}
/**
* Set email
*
* @param string $email
* @return HtaUser
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set emailCanonical
*
* @param string $emailCanonical
* @return HtaUser
*/
public function setEmailCanonical($emailCanonical)
{
$this->emailCanonical = $emailCanonical;
return $this;
}
/**
* Get emailCanonical
*
* @return string
*/
public function getEmailCanonical()
{
return $this->emailCanonical;
}
/**
* Set enabled
*
* @param boolean $enabled
* @return HtaUser
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
return $this;
}
/**
* Get enabled
*
* @return boolean
*/
public function getEnabled()
{
return $this->enabled;
}
/**
* Set salt
*
* @param string $salt
* @return HtaUser
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set password
*
* @param string $password
* @return HtaUser
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set lastLogin
*
* @param \DateTime $lastLogin
* @return HtaUser
*/
public function setLastLogin($lastLogin)
{
$this->lastLogin = $lastLogin;
return $this;
}
/**
* Get lastLogin
*
* @return \DateTime
*/
public function getLastLogin()
{
return $this->lastLogin;
}
/**
* Set locked
*
* @param boolean $locked
* @return HtaUser
*/
public function setLocked($locked)
{
$this->locked = $locked;
return $this;
}
/**
* Get locked
*
* @return boolean
*/
public function getLocked()
{
return $this->locked;
}
/**
* Set expired
*
* @param boolean $expired
* @return HtaUser
*/
public function setExpired($expired)
{
$this->expired = $expired;
return $this;
}
/**
* Get expired
*
* @return boolean
*/
public function getExpired()
{
return $this->expired;
}
/**
* Set expiresAt
*
* @param \DateTime $expiresAt
* @return HtaUser
*/
public function setExpiresAt($expiresAt)
{
$this->expiresAt = $expiresAt;
return $this;
}
/**
* Get expiresAt
*
* @return \DateTime
*/
public function getExpiresAt()
{
return $this->expiresAt;
}
/**
* Set confirmationToken
*
* @param string $confirmationToken
* @return HtaUser
*/
public function setConfirmationToken($confirmationToken)
{
$this->confirmationToken = $confirmationToken;
return $this;
}
/**
* Get confirmationToken
*
* @return string
*/
public function getConfirmationToken()
{
return $this->confirmationToken;
}
/**
* Set passwordRequestedAt
*
* @param \DateTime $passwordRequestedAt
* @return HtaUser
*/
public function setPasswordRequestedAt($passwordRequestedAt)
{
$this->passwordRequestedAt = $passwordRequestedAt;
return $this;
}
/**
* Get passwordRequestedAt
*
* @return \DateTime
*/
public function getPasswordRequestedAt()
{
return $this->passwordRequestedAt;
}
/**
* Set roles
*
* @param array $roles
* @return HtaUser
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
/**
* Set credentialsExpired
*
* @param boolean $credentialsExpired
* @return HtaUser
*/
public function setCredentialsExpired($credentialsExpired)
{
$this->credentialsExpired = $credentialsExpired;
return $this;
}
/**
* Get credentialsExpired
*
* @return boolean
*/
public function getCredentialsExpired()
{
return $this->credentialsExpired;
}
/**
* Set credentialsExpireAt
*
* @param \DateTime $credentialsExpireAt
* @return HtaUser
*/
public function setCredentialsExpireAt($credentialsExpireAt)
{
$this->credentialsExpireAt = $credentialsExpireAt;
return $this;
}
/**
* Get credentialsExpireAt
*
* @return \DateTime
*/
public function getCredentialsExpireAt()
{
return $this->credentialsExpireAt;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add group
*
* @param \Hta\CoreBundle\Entity\HtaGroup $group
* @return HtaUser
*/
public function addGroup(\Hta\CoreBundle\Entity\HtaGroup $group)
{
$this->group[] = $group;
return $this;
}
/**
* Remove group
*
* @param \Hta\CoreBundle\Entity\HtaGroup $group
*/
public function removeGroup(\Hta\CoreBundle\Entity\HtaGroup $group)
{
$this->group->removeElement($group);
}
/**
* Get group
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroup()
{
return $this->group;
} }
集团实体
命名空间 Hta\CoreBundle\Entity;
使用 Doctrine\ORM\Mapping 作为 ORM;
/** * HtaGroup / class HtaGroup { / * * @var string */ private $name;
/**
* @var array
*/
private $roles;
/**
* @var integer
*/
private $id;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $user;
/**
* Constructor
*/
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set name
*
* @param string $name
* @return HtaGroup
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set roles
*
* @param array $roles
* @return HtaGroup
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add user
*
* @param \Hta\CoreBundle\Entity\HtaUser $user
* @return HtaGroup
*/
public function addUser(\Hta\CoreBundle\Entity\HtaUser $user)
{
$this->user[] = $user;
return $this;
}
/**
* Remove user
*
* @param \Hta\CoreBundle\Entity\HtaUser $user
*/
public function removeUser(\Hta\CoreBundle\Entity\HtaUser $user)
{
$this->user->removeElement($user);
}
/**
* Get user
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
} }
$user = $em -> getRepository('HtaCoreBundle:HtaUser' ) -> find($id);
foreach ($user->getGroup() as $group)
{
//this is where you get your groups id
echo $group->getId();
}
注意: unserialize(): 在 D:\symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\ArrayType.php 第 48 行中 5 个字节的偏移量 0 处出错
print_r $用户对象
[owner:Doctrine\ORM\PersistentCollection:private] => Hta\CoreBundle\Entity\HtaUser Object
(
[username:Hta\CoreBundle\Entity\HtaUser:private] => admin
[usernameCanonical:Hta\CoreBundle\Entity\HtaUser:private] => admin
[email:Hta\CoreBundle\Entity\HtaUser:private] => admin@yahoo.com
[emailCanonical:Hta\CoreBundle\Entity\HtaUser:private] => admin@yahoo.com
[enabled:Hta\CoreBundle\Entity\HtaUser:private] => 1
[salt:Hta\CoreBundle\Entity\HtaUser:private] =>
[password:Hta\CoreBundle\Entity\HtaUser:private] =>
[lastLogin:Hta\CoreBundle\Entity\HtaUser:private] =>
[locked:Hta\CoreBundle\Entity\HtaUser:private] =>
[expired:Hta\CoreBundle\Entity\HtaUser:private] =>
[expiresAt:Hta\CoreBundle\Entity\HtaUser:private] =>
[confirmationToken:Hta\CoreBundle\Entity\HtaUser:private] =>
[passwordRequestedAt:Hta\CoreBundle\Entity\HtaUser:private] =>
[roles:Hta\CoreBundle\Entity\HtaUser:private] => Array
(
[0] => ROLE_ADMIN
)
[credentialsExpired:Hta\CoreBundle\Entity\HtaUser:private] =>
[credentialsExpireAt:Hta\CoreBundle\Entity\HtaUser:private] =>
[id:Hta\CoreBundle\Entity\HtaUser:private] => 2
[group:Hta\CoreBundle\Entity\HtaUser:private] => Doctrine\ORM\PersistentCollection Object
递归
)
如何打印连接表输出?([组:Hta\CoreBundle\Entity\HtaUser:private] => Doctrine\ORM\PersistentCollection 对象
RECURSION
)