still getting to grips with Symfony so please excuse me if this is already asked. I don't quite understand how to send data to forms to be processed properly so am need of a bit of guidance.
My issue is i wish to save my logged in user's ID into the database when they are creating a new gecko (so that this gecko's profile shows for only this user logged in). I am using FOSUserBundle by the way.
I know i can get the current user's id by using something like $user = $this->getUser()->getId(); However, i would like to save this into the database.
createAction:
public function createAction(Request $request)
{
$entity = new Gecko();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('gecko_show', array('name' => $entity->getName())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
GeckoType:
<?php
namespace Breedr\GeckoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class GeckoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('aquisitionDate')
->add('morph')
->add('sex', 'choice', array(
'choices' => array(
'Male' => 'Male',
'Female' => 'Female'
)
))
->add('genetics')
->add('bio')
->add('bred', 'checkbox', array(
'required' => false
))
->add('hatchling', 'checkbox', array(
'required' => false
))
->add('clutch', 'text', array(
'data' => 0,
'required' => false
))
->add('imageFile', 'file', array(
'required' => false
))
->add('user_id', 'hidden', array(
'data' => $user,
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Breedr\GeckoBundle\Entity\Gecko'
));
}
/**
* @return string
*/
public function getName()
{
return 'breedr_geckobundle_gecko';
}
}
I would really appreciate any help with this. I just can't understand how the data is used/sent. Thanks in advance :)
Andy