我已成功安装了Sonata用户,管理员,媒体和其他一些捆绑包。我想将配置文件pic添加到用户实体,所以我添加了一个$ profilePic属性(使用setter和getter),如下所示:
/**
* @ORM\OneToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media",cascade={"persist"})
* @ORM\JoinColumn(name="profilePic_id", referencedColumnName="id")
**/
protected $profilePic;
public function getProfilePic()
{
return $this->profilePic;
}
public function setProfilePic($profilePic)
{
$this->profilePic = $profilePic;
}
我有一个用于编辑个人资料的表单:
class ProfileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//...
->add('profilePic', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'default'
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Application\Sonata\UserBundle\Entity\User',
'intention' => 'profile',
'label' => 'Edit Profile'
));
}
}
我不需要Admin类,因为每个用户都应该能够从前端编辑他的个人资料。 现在,当我尝试编辑用户的个人资料(http://myweb.dev/app_dev.php/profile/edit-profile)时,我收到以下错误:
Neither the property "profilePic" nor one of the methods "getProfilePic()",
"profilePic()", "isProfilePic()", "hasProfilePic()", "__get()" exist and have
public access in class "Application\Sonata\UserBundle\Entity\User".
你能帮我理解我做错了吗? 谢谢。
P.S。 1.我需要提一下我的用户实体扩展自SonataUserBundle。 2.但是如果我修改我的ProfilrType如下,它可能会解决问题:
class ProfileType extends AbstractType
{
//...
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CoreBundle\Entity\User', //Now it points to my original user entity
//...
));
}
}
但我得到以下错误,我不明白这意味着什么:
The form's view data is expected to be an instance of class CoreBundle\Entity\User,
but is an instance of class Application\Sonata\UserBundle\Entity\User.
You can avoid this error by setting the "data_class" option to null or by adding a
view transformer that transforms an instance of class
Application\Sonata\UserBundle\Entity\User to an instance of CoreBundle\Entity\User.
非常感谢您的帮助......