问题
I have installed Sonata User, Admin, Media and a few other bundles successfully. I want to add profile pic to user entity so I've added a $profilePic property to it (with setter and getter) as follow:
/**
* @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;
}
And I have a form for editing profile like this:
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'
));
}
}
I don't need an Admin class for it because each user should be able to edit his profile from frontend. Now when I try to edit profile for a user (http://myweb.dev/app_dev.php/profile/edit-profile) I get following error:
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".
Can you help me realize what I'm doing wrong please? Thank you.
p.s. 1. I need to mention my User entity extends from SonataUserBundle. 2. I though if I modify my ProfilrType as follow it might fix the problem:
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
//...
));
}
}
But I get following error which I don't understand what it means:
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.
Your help is very much appreciated...
来源:https://stackoverflow.com/questions/29983814/sonatauserbundle-neither-property-nor-methods-exist