我有一個PersonType
窗體,然後我有LegalPersonType
和NaturalPersonType
窗體,並且都從PersonType
延伸,因爲它們具有該窗體上的公用字段(映射到實體級別)。例如,這是NaturalPersonType.php
窗體繼承問題
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Tanane\FrontendBundle\DBAL\Types\CIType;
use Tanane\FrontendBundle\Form\Type\PersonType;
class NaturalPersonType extends PersonType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('identification_type', 'choice', array(
'label' => 'Número de Cédula',
'choices' => CIType::getChoices()
))
->add('ci', 'number', array(
'required' => true,
'label' => false,
'attr' => array(
'maxlength' => 8,
))
)
->add('lives_in_ccs', 'checkbox', array(
'label' => false,
'required' => false,
'value' => 1,
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
));
}
public function getName()
{
return 'natural_person';
}
}
然後在SaveFormController/orderAction()我做這個代碼:
$order = new Orders();
$orderForm = $this->createForm(new OrdersType(array($type)), $order, array('action' => $this->generateUrl('save_order')));
但任何時候,我嘗試呈現我得到這個錯誤的形式:
Neither the property "nat" nor one of the methods "getNat()", "nat()", "isNat()", "hasNat()", "__get()" exist and have public access in class "Tanane\FrontendBundle\Entity\Orders".
關係處於實體級別,我如何解決該錯誤?
在此先感謝
1日可能的解決方案
在從用戶這裏我改變建議,在OrderType.php
我的代碼的形式,以這樣的:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Tanane\FrontendBundle\DBAL\Types\SFType;
class OrdersType extends AbstractType {
/**
* @var string
*/
protected $register_type;
public function __construct($register_type)
{
$this->register_type = $register_type;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// here goes $builder with default options remove for see less code
if ($this->register_type[0] == "natural")
{
$builder->add('nat', new NaturalPersonType(), array(
'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
));
}
elseif ($this->register_type[0] == "legal")
{
$builder->add('leg', new LegalPersonType(), array(
'data_class' => 'Tanane\FrontendBundle\Entity\LegalPerson'
));
}
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
'render_fieldset' => FALSE,
'show_legend' => FALSE
));
}
public function getName()
{
return 'orders';
}
}
我已通過添加'mapped' => FALSE
固定我在OrdersType
中添加每個新的FormType,但我不知道這是否正確。另外,如果我在此處定義data_class
,並且NaturalType
將永遠不會直接訪問,只需要低谷OrdersType
我應該從該表單中刪除默認選項,還是應該將它們留在那裏?我現在如何解決這個問題?我錯過了什麼?
嗨,你能看看我的版嗎?在嘗試使用您的建議獲取代碼後,我仍然遇到一些問題 – ReynierPM 2014-09-03 12:03:00
我更新了我的答案 – 2014-09-03 13:33:54
嗯,我現在迷失了,您能否留下代碼示例? – ReynierPM 2014-09-03 19:32:22