2012-07-27 3 views
2

나는 manyToMany 일방적 인 관계로 두 엔티티, 퀴즈 및 QuizQuestion을 가지고있다. 질문 형식을 퀴즈 형식으로 포함시키고 싶습니다. 2.0 지점에 있습니다. 나는 sonata_type_model을 얻을 수 있는데, ID의 목록을 드롭 다운에 가져와 "추가"버튼을 사용할 수 있습니다. sonata_type_admin를 사용하려고 할 때 그러나, 나는 오류를 받고 있어요 :SonataAdminBundle 포함 된 양식 sonata_admin_type 문제

<?php 

namespace Some\SiteBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 


/** 
* Some\SiteBundle\Entity\Quiz 
* @ORM\Table(name="quiz") 
* @ORM\Entity(repositoryClass="Some\SiteBundle\Entity\QuizRepository") 

*/ 
class Quiz 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 



    /** 
    * @ORM\Column(type="datetime", name="created_at") 
    * 
    * @var DateTime $createdAt 
    */ 
    protected $createdAt; 


    /** 
    * @var string $title 
    * 
    * @ORM\Column(name="title", type="string", length=255) 
    */ 
    private $title; 


    /** 
    * @var string $body 
    * 
    * @ORM\Column(name="body", type="text") 
    */ 
    private $body; 


    /** 
    * @var QuizQuestion questions 
    * @ORM\ManyToMany(targetEntity="QuizQuestion", cascade={"persist", "remove"}) 
    **/ 
    protected $questions; 


    public function __construct() { 
     $this->questions = new ArrayCollection(); 
     $this->createdAt = new \DateTime(); 
    } 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 
    } 

    /** 
    * Get title 
    * 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 


    /** 
    * Get quiz body. 
    * 
    * @return string body 
    */ 
    public function getBody() 
    { 
     return $this->body; 
    } 

    /** 
    * Sets body 
    * 
    * @param string $value body 
    */ 
    public function setBody($body) 
    { 
     $this->body = $body; 
    } 



    /** 
    * Gets an object representing the date and time the quiz was created. 
    * 
    * @return DateTime A DateTime object 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 



    /** 
    * Add questions 
    * 
    * @param Some\SiteBundle\Entity\QuizQuestion $questions 
    */ 
    public function addQuestion(\Some\SiteBundle\Entity\QuizQuestion $question) 
    { 
     $this->questions[] = $question; 
    } 

    /** 
    * set question 
    * 
    * @param Some\SiteBundle\Entity\QuizQuestion $questions 
    */ 
    public function setQuestion(\Some\SiteBundle\Entity\QuizQuestion $question) 
    { 
     foreach ($this->questions as $doc) { 
      $this->questions->removeElement($doc); 
     } 
     $this->questions[] = $question; 
    } 

    /** 
    * Get questions 
    * 
    * @return Doctrine\Common\Collections\Collection 
    */ 
    public function getQuestions() 
    { 
     return $this->questions; 
    } 


    /** 
    * @ORM\PrePersist 
    */ 
    public function beforePersist() 
    { 
     //$this->setCreatedAt(new \DateTime());   
     //$this->setModifiedAt(new \DateTime()); 
    } 


     public function __toString() 
    { 
    return 'Quiz'; 
    } 
} 

그리고 QuizQuestion 엔티티 :

<?php 

namespace Some\SiteBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 


/** 
* Some\SiteBundle\Entity\QuizQuestion 
* @ORM\Table(name="quiz_question") 
* @ORM\Entity 
*/ 
class QuizQuestion 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 



    /** 
    * @ORM\Column(type="datetime", name="created_at") 
    * 
    * @var DateTime $createdAt 
    */ 
    protected $createdAt; 


    /** 
    * @var string $title 
    * 
    * @ORM\Column(name="title", type="string", length=255) 
    */ 
    private $title; 


    /** 
    * @var string $body 
    * 
    * @ORM\Column(name="body", type="text") 
    */ 
    private $body; 

    /** 
    * @var string $answer1 
    * 
    * @ORM\Column(name="answer1", type="text") 
    */ 
    private $answer1; 

    /** 
    * @var string $answer2 
    * 
    * @ORM\Column(name="answer2", type="text") 
    */ 
    private $answer2; 

     /** 
    * @var string $answer3 
    * 
    * @ORM\Column(name="answer3", type="text") 
    */ 
    private $answer3; 

    /** 
    * @var string $answer4 
    * 
    * @ORM\Column(name="answer4", type="text") 
    */ 
    private $answer4; 

    /** 
    * @var string $correctAnswer 
    * 
    * @ORM\Column(name="correct_answer", type="integer", length="1") 
    */ 
    private $correctAnswer; 




    public function __construct() { 
     $this->createdAt = new \DateTime(); 
    } 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 
    } 

    /** 
    * Get title 
    * 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 


    /** 
    * Get question body. 
    * 
    * @return string body 
    */ 
    public function getBody() 
    { 
     return $this->body; 
    } 

    /** 
    * Sets body 
    * 
    * @param string $value body 
    */ 
    public function setBody($body) 
    { 
     $this->body = $body; 
    } 


    /** 
    * Get question answer1. 
    * 
    * @return string answer1 
    */ 
    public function getAnswer1() 
    { 
     return $this->answer1; 
    } 

    /** 
    * Sets answer1 
    * 
    * @param string $value answer1 
    */ 
    public function setAnswer1($answer1) 
    { 
     $this->answer1 = $answer1; 
    } 

     /** 
    * Get question answer2. 
    * 
    * @return string answer2 
    */ 
    public function getAnswer2() 
    { 
     return $this->answer2; 
    } 

    /** 
    * Sets answer2 
    * 
    * @param string $value answer2 
    */ 
    public function setAnswer2($answer2) 
    { 
     $this->answer2 = $answer2; 
    } 

     /** 
    * Get question answer3. 
    * 
    * @return string answer3 
    */ 
    public function getAnswer3() 
    { 
     return $this->answer3; 
    } 

    /** 
    * Sets answer3 
    * 
    * @param string $value answer3 
    */ 
    public function setAnswer3($answer3) 
    { 
     $this->answer3 = $answer3; 
    } 

     /** 
    * Get question answer4. 
    * 
    * @return string answer4 
    */ 
    public function getAnswer4() 
    { 
     return $this->answer4; 
    } 

    /** 
    * Sets answer4 
    * 
    * @param string $value answer4 
    */ 
    public function setAnswer4($answer4) 
    { 
     $this->answer4 = $answer4; 
    } 

    /** 
    * Get question correctAnswer. 
    * 
    * @return string correctAnswer 
    */ 
    public function getCorrectAnswer() 
    { 
     return $this->correctAnswer; 
    } 

    /** 
    * Sets answer1 
    * 
    * @param string $value correctAnswer 
    */ 
    public function setCorrectAnswer($correctAnswer) 
    { 
     $this->correctAnswer = $correctAnswer; 
    } 

    /** 
    * Gets an object representing the date and time the question was created. 
    * 
    * @return DateTime A DateTime object 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 




    public function __toString() 
    { 
    return $this->title; 
    } 
} 

그리고 관련 관리자 클래스

Neither property "title" nor method "getTitle()" nor method "isTitle()" exists in class "Doctrine\ORM\PersistentCollection" 
500 Internal Server Error - InvalidPropertyException 

가 여기 내 퀴즈 엔티티입니다. 첫째 QuizAdmin :

<?php 

namespace Some\SiteBundle; 

use Some\SiteBundle\Form\QuizQuestionType; 
use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Validator\ErrorElement; 
use Sonata\AdminBundle\Form\FormMapper; 
use Sonata\AdminBundle\Show\ShowMapper; 
use Symfony\Component\HttpFoundation\File\File; 
use Symfony\Component\HttpFoundation\Request; 

class QuizAdmin extends Admin 
{ 


    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
       ->add('title', NULL, array('label' => 'tytuł:')) 
       ->add('body', NULL, array('label' => 'opis:', 'required' => false, 'attr' => array(
        'class' => 'tinymce', 'data-theme' => 'simple') 
      )) 
       ->add('questions', 'sonata_type_admin', array(), array('required' => false, 'edit' => 'inline')); 
       //->add('categories', NULL, array('label' => 'kategorie:')) 
     ; 
    } 

    protected function configureDatagridFilters(DatagridMapper $datagridMapper) 
    { 
     $datagridMapper 
      ->add('title') 
      ->add('body') 
     ; 
    } 

    protected function configureListFields(ListMapper $listMapper) 
    { 
     $listMapper 
      ->addIdentifier('id') 
      ->add('title') 
      ->add('_action', 'actions', array(
       'actions' => array(
        'view' => array(), 
        'edit' => array(), 
       ) 
      )) 
      //->add('body') 
     ; 

    } 

    public function validate(ErrorElement $errorElement, $object) 
    { 
     $errorElement 
      ->with('title') 
       ->assertMinLength(array('limit' => 2)) 
      ->end() 
     ; 
    } 



} 

그리고 QuizQuestionAdmin는 :

<?php 

namespace Some\SiteBundle; 

use Some\SiteBundle\Form\QuizQuestionType; 
use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Validator\ErrorElement; 
use Sonata\AdminBundle\Form\FormMapper; 
use Sonata\AdminBundle\Show\ShowMapper; 
use Symfony\Component\HttpFoundation\File\File; 
use Symfony\Component\HttpFoundation\Request; 

class QuizAdmin extends Admin 
{ 


    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
       ->add('title', NULL, array('label' => 'tytuł:')) 
       ->add('body', NULL, array('label' => 'opis:', 'required' => false, 'attr' => array(
        'class' => 'tinymce', 'data-theme' => 'simple') 
      )) 
       ->add('questions', 'sonata_type_admin', array(), array('required' => false, 'edit' => 'inline')); 
       //->add('categories', NULL, array('label' => 'kategorie:')) 
     ; 
    } 

    protected function configureDatagridFilters(DatagridMapper $datagridMapper) 
    { 
     $datagridMapper 
      ->add('title') 
      ->add('body') 
     ; 
    } 

    protected function configureListFields(ListMapper $listMapper) 
    { 
     $listMapper 
      ->addIdentifier('id') 
      ->add('title') 
      ->add('_action', 'actions', array(
       'actions' => array(
        'view' => array(), 
        'edit' => array(), 
       ) 
      )) 
      //->add('body') 
     ; 

    } 

    public function validate(ErrorElement $errorElement, $object) 
    { 
     $errorElement 
      ->with('title') 
       ->assertMinLength(array('limit' => 2)) 
      ->end() 
     ; 
    } 



} 

나는 서비스로 quizQuestion를 등록하려고했으나 그때 얻을 Expected argument of type "Festus\SiteBundle\Entity\QuizQuestion", "Doctrine\ORM\PersistentCollection" given 500 Internal Server Error - UnexpectedTypeException

찾고 것들의 몇 시간 후 모든 솔루션을 찾을 수 없습니다 당신이없는 기존의 방법의 예외를 얻고있는 이유에 오류가 설명처럼

답변

1

... 최대 것 같습니다.

개체 대신 컬렉션을 전달하려고합니다. 아마도 개체 대신 컬렉션에 addQuestion() 또는 setQuestion() 메서드를 호출하면됩니다.

이것은 일반적으로 관계가 채워진 객체에서 발생합니다. 이와

->add('questions', 'sonata_type_admin', array(), array('required' => false, 'edit' => 'inline')); 

: 당신이 방법은, $object->first()->addQuestion() 대신 $object->addQuestion()

+0

예, 오류가 (어느 정도) 자명 것 같다,하지만 오히려 관련 매핑을 폼의 , 그래서 나는 정확히 어떤 물체를 불러야하는지 잘 모르겠다. 폼 매핑 함수의 체인은 엔티티 나 다른 객체 대신 컬렉션을 얻는 것처럼 보이고 예외를 발생시킵니다. 형태의 수집은 임베디드 형태의 sholud가있는 곳입니다. 만약 내가'-> add ('title', NULL, array ('label'=> 'tytuł :'))'를 주석 처리하면 동일한 예외가 발생하지만'body' 속성을 갖습니다. – heliogabal

+0

'add()'메쏘드의 세 번째 인자에 추가 인자를 넘겨 주시겠습니까 - 'by_reference'=> false'? http://symfony.com/doc/current/reference/forms/types/collection.html # by-reference –

+0

다음과 같이'-> add ('questions', 'sonata_type_admin', array(), array ('required'=> false, 'edit'=> '인라인', 'by_reference'=> false)); 아니. 어느 쪽도 작동하지 않습니다. 같은 오류. – heliogabal

4

확인 전화를 시도 것을 사용하는 장소에서

, 나는 그것을이 대체 해결

->add('questions','collection', array('type' => new QuizQuestionType(), 
              'allow_add' => true, 
              'prototype' => true, 
              'by_reference' => true, 
             )); 

하지만, 나를 위해 그것은 솔루션보다는 해결 방법처럼 보인다 : - |

어쨌든, 나는 모든 2.0 분기에, 번들 버전, 심포니 지점을 확인하고 조리 아무것도 찾을 수 없습니다.

PS - QuizQuestionType 클래스 여기에 화려한 누구나 관심이 있다면 ... 아무것도, 그냥 평범한 폼 클래스 :

<?php 

namespace Some\SiteBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 

class QuizQuestionType extends AbstractType 
    { 
     public function buildForm(FormBuilder $builder, array $options) 
     { 
    $builder 
     ->add('title', NULL, array('label' => 'pytanie:')) 
     ->add('body', NULL, array('label' => 'treść:', 'attr' => array(
      'class' => 'tinymce', 'data-theme' => 'simple') 
     )) 
     ->add('answer1', NULL, array('label' => 'odp. 1:')) 
     ->add('answer2', NULL, array('label' => 'odp. 2:')) 
     ->add('answer3', NULL, array('label' => 'odp. 3:')) 
     ->add('answer4', NULL, array('label' => 'odp. 4:')) 
     ->add('correctAnswer', NULL, array('label' => 'prawidłowa odp.:')) 
    ; 
    } 

    public function getName() 
    { 
     return 'quiz_question'; 
    } 

    public function getDefaultOptions(array $options){ 
     return array('data_class' => 'Some\SiteBundle\Entity\QuizQuestion'); 
    } 
}