2012-03-26 60 views
0

我想要在Symfony2中保存基於表單類型的表單,但找不到save()方法。表格不會保存在Symfony2中

的錯誤信息是:

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

調用保存方法如下所示,所述控制器:

<?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())); 
     } 
} 
?> 

的CALLTYPE定義如下:

<?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'; 
    } 
} 
?> 

最後有一個實體類「呼叫」,也可以正常工作:

<?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... 

} 

有沒有人知道爲什麼找不到保存方法? bind()方法不會觸發錯誤,所以必須有一個有效的表單對象,我猜。

回答

4

表單不對持久化對象負責;他們負責輸出帶有來自對象的值的表單字段,並在表單提交時將用戶輸入放入該對象中。

使用Doctrine來保存您的對象。以下是Forms and Doctrine部分的代碼段,適用於您的示例:

if ($call_form->isValid()) { 
    $em = $this->getDoctrine()->getEntityManager(); 
    $em->persist($call); 
    $em->flush(); 
} 
+0

謝謝elnur,確實解決了這個問題。 雖然我必須承認,我不明白爲什麼Symfony2的手冊寫了$ form-> save()。我必須在這裏對兩個單獨的概念感到困惑。 – 2012-03-26 13:51:38