2012-07-31 1 views
0

나는 내 질문에 완전히 다른 말을하기로 결정했습니다. 다행히도이 문제는 훨씬 더 명확합니다.Symfony2.1 임베디드 양식 및 외래 키 관계

엔티티 내에 외래 키 필드를 나타내는 양식을 어떻게 포함합니까? 예를 들어, 속성에는 상태 테이블 (소유, 사용 가능, 판매용 등 ...)에 대한 외래 키가 있습니다. 임베디드 양식을 사용하여 내 임베디드 양식 (이 경우 Status)을 부모 엔터티가 포함하는 방법을 이해할 수 없으므로 양식을 제출할 때 속성의 상태를 작성/변경하면 외래 키 관계 만 변경됩니다. 속성을 쿼리하고 $ property-> setStatus ($ status)를 호출하여 상태를 변경할 수있어서 교리 관계가 정확하다고 믿습니다. 폼의 상태를 변경하려고 할 때 지금

, 나는이 오류가 제출 :

Catchable Fatal Error: Object of class Test\Bundle\SystemBundle\Entity\Status could not be converted to string in /home/vagrant/projects/test.dev/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php line 1118 

내 양식 작성 :

$form = $this->createForm(new PropertyType(), $property); 

내 부동산 기업의 상태에 대한 재산권의 실체 관계 :

/** 
* @var Status $status 
* 
* @ORM\ManyToOne(targetEntity="Test\Bundle\SystemBundle\Entity\Status") 
* @ORM\JoinColumn(name="StatusId", referencedColumnName="Id", nullable=false) 
*/ 
protected $status; 

다음은 StatusType을 포함하는 내 PropertyType 클래스의 줄입니다 클래스 : 여기

->add('status', new StatusType()) 

그리고 내 StatusType 양식 클래스입니다 :

class StatusType extends AbstractType 
{ 
public $statusType = null; 

public function __construct($statusType) 
{ 
    $this->statusType = $statusType; 
} 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 

    $builder->add('name', 'entity', array('label' => 'Status Name', 
      'class'  => 'Test\Bundle\SystemBundle\Entity\Status', 
      'property' => 'name')); 

} 

public function getParent() 
{ 
    return 'form'; 
} 

public function getDefaultOptions(array $options) 
{ 
    return array('data_class' => 'Test\Bundle\SystemBundle\Entity\Status'); 
} 

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

답변

1

당신의 Status 실체를 보지 않고, 당신이 그것에 __toString() 방법을 추가 할 필요가 같은 소리. Symfony가 엔티티를 텍스트로 렌더링하려면 표시 할 내용을 알아야합니다. 이게 뭔가 ...

class Status 
{  
    public $title; 

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

내 상태 엔티티에 toString 메소드가 있습니다. 이 문제는 선택 목록을 표시하지 않습니다. Symfony가 StatusType 클래스에 PropertyType과의 관계가 있음을 확인하는 중입니다. 선택 상자는 모든 상태를 표시하지만 속성의 키를 기반으로 값을 미리 선택하지 않으며 다른 상태의 양식을 제출할 때 관계 대신 상태 레코드를 업데이트하려고합니다. Property to Status 관계와 폼을 만드는 방법을 보여주기 위해 코드를 업데이트했습니다. – mhoff

0

하나의 해결책은 상태의 PropertyType에 모든 로직을 넣는 것입니다.

->add('status', 'entity', 
      array('class' => 'Test\Bundle\SystemBundle\Entity\Status', 
       'property' => 'name', 
       'query_builder' => function(EntityRepository $er){ 
        return $er->createQueryBuilder('status') 
        ->orderBy('status.name', 'ASC'); 
       })) 

대신 StatusType 내장의 :

->add('status', new StatusType()) 

를 상태를 사용하는 모든 기업이 중복 것입니다하지만 난 방법을 알아낼 때까지 당분간 작동하기 때문에 나는이 방법을 좋아하지 않는다 그것을 작동 시키십시오.