2012-03-26 9 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'; 
    } 
} 
?> 

そして最後にエンティティクラス "Call"もあります。

<?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を使用してください。

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

ありがとうございましたelnurさん、ありがとうございました。実際にこの問題を解決しました。このコードスニペットはForms and Doctrineセクションから抜粋したものです。 私はSymfony2のマニュアルが$ form-> save()を書く理由を理解できませんが、私はここで2つの別々の概念について混乱させなければならない。 –

関連する問題