I am trying to find an error in my application for days now, maybe some of you can give a hint:
I am using Symfony2, FOSUserBundles and Bootstrap for the forms. In my controller I create within the createAction
a form for entering a small ad depending on the user:
$smallAd = new SmallAd();
$user = $this->getUser();
$smalladForm = new SmallAdType();
$smalladForm->setUser($user);
$form = $this->createForm($smalladForm, $smallAd);
After creating an instance of the form I set the user because I need the user ID for the formbuilder. In a former version I set user via constructor, which makes no difference on my local machine.
The form class SmallAdType looks like this (I removed most of the form fields):
public $user;
public function setUser($user)
{
$this->user = $user;
}
public function buildform(FormbuilderInterface $builder, array $options)
{
var_dump($this->user->getId());
$builder
...
->add('user', 'entity', array(
'class' => 'AppBundle\Entity\User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->where('u.id = :id')->setParameter('id',$this->user->getId());
},
'label' => 'User',
'required' => true
))
.....
->add('save', 'submit', array('label' => 'Save the Ad'));
}
My problem: on my local develop environment everythings works fine, on the prod server I get the following error message:
int(2)
Fatal error: Using $this when not in object context in xxxxxxx/Form/Type/SmallAdType.php on line 74
the line with int(2)
comes from the var_dump($this->user->getId());
function and is in this case the correct value from my user. I can dump also the complete user object which means for me, that it exists at this point.
I checked the code and configuration on my local and the production server a few times and I can find no difference. Furthermore I don't have any idea where the look now. Any advice is highly welcome.