I have want to save a form in Symfony2, based on a form type, but the save() method is not found.
The error message is:
Fatal error: Call to undefined method Symfony\Component\Form\Form::save() in C:\xampp\htdocs\Xq\src\Xq\LogBundle\Controller\LogController.php on line 44
The controller that invokes the save method looks as follows:
<?php
namespace Xq\LogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityRepository;
use Xq\LogBundle\Entity\Call;
use Xq\LogBundle\Form\Type\CallType;
class LogController extends Controller
{
public function callAction(Request $request)
{
#create call object
$call = new Call();
$now = new \DateTime("now");
$call->setTimestamp($now);
$call_form = $this->createForm(new CallType(), $call);
#check form input
$request = $this->get('request');
if ($request->getMethod() == 'POST')
{
$call_form->bindRequest($request);
if ($call_form->isValid())
{
**$saved_call = $call_form->save();**
}
}
return $this->render('XqLogBundle:log:call.html.twig', array('call_form'=>$call_form->createView()));
}
}
?>
The CallType is defined as below:
<?php
namespace Xq\LogBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class CallType extends AbstractType
{
public function buildForm(Formbuilder $builder, array $options)
{
//here all fields are defined, and they are rendered fine
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Xq\LogBundle\Entity\Call');
}
public function getName()
{
return 'callform';
}
}
?>
And finally there is an entity class “Call” that works fine as well:
<?php
#ORM mapping of a call
namespace Xq\LogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
*
* Xq\LogBundle\Entity\Call
*
* @ORM\Table(name="calls")
* @ORM\Entity
*/
class Call
{
#id of the call
/**
* @ORM\Id
* @ORM\Column(type="integer", length="7")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
#caller
/**
* @ORM\Column(name="caller", type="integer", length="3", nullable="false")
* @ORM\OneToOne(targetEntity="caller")
* @Assert\type(type="Xq\LogBundle\Entity\Caller")
*/
protected $caller;
// and so on ....
#getters and setters
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
// and so on...
}
Does anybody know why the save method is not found? The bind() method does not trigger an error, so there must be a valid form object I guess.
Forms are not responsible for persisting objects; they are responsible for outputting form fields with values from an object and putting user input into that object on form submit.
Use Doctrine to persist your objects. Here is the code snippet from the Forms and Doctrine section, adapted for your example: