Doctrine 如何获取FOSUserBundle中自定义实体的所有属性?
我用的是FOSUserBundle 我创建了一个扩展baseuser的用户实体,并添加了一个名为$apiKey的受保护属性。登记表很好用 然后,我创建了一个扩展控制器的userController,在编辑我的用户的方法中,我有:Doctrine 如何获取FOSUserBundle中自定义实体的所有属性?,doctrine,fosuserbundle,symfony,Doctrine,Fosuserbundle,Symfony,我用的是FOSUserBundle 我创建了一个扩展baseuser的用户实体,并添加了一个名为$apiKey的受保护属性。登记表很好用 然后,我创建了一个扩展控制器的userController,在编辑我的用户的方法中,我有: $em = $this->getDoctrine()->getManager(); $user = $em->getRepository('AppBundle:User')->find($id); my$user有apiKey属性,但它是空的(
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')->find($id);
my$user有apiKey属性,但它是空的(当然数据库中的字段不是空的)
有什么想法吗
谢谢
更新:用户实体
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Group")
* @ORM\JoinTable(name="fos_user_user_group",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
/**
* @ORM\Column(type="string", unique=true, nullable=true)
*/
protected $apiKey;
/* public function __construct()
{
parent::__construct();
// your own logic
//$this->roles = array('ROLE_USER'); //default role for new users
}*/
public function __construct()
{
parent::__construct();
// your own logic
$this->groups = new ArrayCollection();
}
/**
* @return mixed
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* @param mixed $apiKey
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}
/**
* @return mixed
*/
public function getGroups()
{
return $this->groups;
}
/**
* @param mixed $groups
*/
public function setGroups($groups)
{
$this->groups = $groups;
}
}
您可以显示您的用户实体吗?@smarber message updated代替
$em->getRepository('AppBundle:user')->find($id)代码>试试这个$em->getRepository('AppBundle:User')->findOneById($id)代码>我已经试过了,无论如何,不是整个$user都是空的,而是我添加的属性。看起来我必须将它映射到其他地方…?这很奇怪,因为您的实体看起来很好,请尝试映射它/***@ORM\Column(name=“api\u key”,type=“string”,unique=true,nullable=true)*/protected$apiKey
您可以显示您的用户实体吗?@smarber message updated代替$em->getRepository('AppBundle:user')->find($id)代码>试试这个$em->getRepository('AppBundle:User')->findOneById($id)代码>我已经试过了,无论如何,不是整个$user都是空的,而是我添加的属性。看起来我必须将它映射到其他地方…?这很奇怪,因为您的实体看起来很好,请尝试映射它/***@ORM\Column(name=“api\u key”,type=“string”,unique=true,nullable=true)*/protected$apiKey代码>