0

I'm editing a object using this code:

public function editAction($id = null)
{
    $em = $this->getDoctrine()->getManager();
    $order = $em->getRepository('FrontendBundle:Orders')->find($id);
    $type = $order->getPerson()->getPersonType() === 1 ? "natural" : "legal";

    ladybug_dump_die($order);

    $orderForm = $this->createForm(new OrdersType(array($type)), $order, array(
        'action' => $this->generateUrl('update-order', array('id' => $id)),
        'method' => 'POST',
        ));

    return  array(
        'entity' => $order,
        "form" => $orderForm->createView(),
        'id' => $id
    );
}

All there works fine except that I'm not know/find how to display object Person values. If you take a look at the picture I've attached here you'll notice that Person comes with values:

Controller Debug

In the other side I did the same but at Twig template and I make a debug to the form var and I get this:

Twig Render

Now at this point I'm confused and come with two possible ideas, that I hope, someone help me to develop or at least understand.

  1. Find a solution and display the right form using all the info from the entity I'm passing to the view. This is the ideal and I'll like to work on this in order to learn, so help?
  2. Get the Person object in controller and create a second form by passing Person object values, this should work but them I'll need a lot of changes in update function since forms will travel separately.

What I need here is to get the NaturalPersonType or LegalPersonType embedded in OrdersType any time I edit a existent Orders because right now I don't know how to display the widget in the twig template. Notice at the end the form I'm talking about in this case is a NaturalPersonType rendered but without values:

enter image description here

Adding OrdersType FormType

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)
    {
        $builder
                ->add('nickname', 'text', array(
                    'required' => FALSE,
                    'label' => "Nickname/Seudónimo",
                    'trim' => TRUE,
                    'attr' => array(
                        'class' => 'nickname'
                    )
                ))
                ->add('email', 'email', array(
                    'required' => TRUE,
                    'label' => "Correo Electrónico",
                    'trim' => TRUE
                ))
                ->add('phone', 'tel', array(
                    'required' => TRUE,
                    'label' => 'Números de teléfono (separados por "/")',
                    'trim' => TRUE,
                    'default_region' => 'VE',
                    'format' => PhoneNumberFormat::NATIONAL
                ))
                ->add('fiscal_address', 'textarea', array(
                    'required' => TRUE,
                    'label' => 'Dirección'
                ))
                ->add('shipping_address', 'textarea', array(
                    'required' => TRUE,
                    'label' => 'Dirección de Envío'
                ))
                ->add('shipping_from', 'choice', array(
                    'label' => 'Compañía de Encomiendas',
                    'choices' => SFType::getChoices(),
                    'empty_value' => '-- SELECCIONAR --'
                ))
                ->add('payment_type', 'entity', array(
                    'class' => 'CommonBundle:PaymentType',
                    'property' => 'name',
                    'required' => TRUE,
                    'label' => 'Forma de Pago',
                    'empty_value' => '-- SELECCIONAR --'
                ))
                ->add('order_amount', 'number', array(
                    'label' => 'Monto',
                    'required' => TRUE,
                    'precision' => 2
                ))
                ->add('bank', 'entity', array(
                    'class' => 'CommonBundle:Bank',
                    'property' => 'name',
                    'required' => TRUE,
                    'label' => 'Banco',
                    'empty_value' => '-- SELECCIONAR --'
                ))
                ->add('transaction', 'text', array(
                    'required' => TRUE,
                    'label' => 'No. Transacción'
                ))
                ->add('comments', 'textarea', array(
                    'required' => FALSE,
                    'label' => 'Comentarios'
                ))
                ->add('secure', 'checkbox', array(
                    'label' => FALSE,
                    'required' => FALSE,
                    'value' => 1,
                    'mapped' => FALSE
                ))
                ->add('lives_in_ccs', 'checkbox', array(
                    'label' => false,
                    'required' => false,
                    'mapped' => FALSE,
                    'value' => 1,
                ))
                ->add('suscribe_mail_list', 'checkbox', array(
                    'label' => FALSE,
                    'required' => FALSE,
                    'value' => 1,
                    'mapped' => FALSE
        ));

        if ($this->register_type[0] == "natural")
        {
            $builder->add('nat', new NaturalPersonType(), array('mapped' => FALSE));
        }
        elseif ($this->register_type[0] == "legal")
        {
            $builder->add('leg', new LegalPersonType(), array('mapped' => FALSE));
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
            'render_fieldset' => FALSE,
            'show_legend' => FALSE,
            'intention' => 'orders_form'
        ));
    }

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

}

Adding NaturalPersonType

<?php

namespace Tanane\FrontendBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Tanane\FrontendBundle\DBAL\Types\CIType;

class NaturalPersonType extends AbstractType {

    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('person', new PersonType(), array('mapped' => FALSE));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Tanane\FrontendBundle\Entity\NaturalPerson'
        ));
    }

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

}
2
  • It's not clear what you're asking. Could you please describe exactly the result you want to get? Also, it would be useful to see your Type's code. Commented Sep 14, 2014 at 17:11
  • @FyodorX I added some extra info take a look and if you need something else let me know Commented Sep 14, 2014 at 17:25

1 Answer 1

1

The problem is in your Type. As you can see, the mapped option is set to false for both nat and leg. This means that Symfony will not connect those fields with your entity, so they are empty when you render the view.

I guess you did it that way because you don't have either of those properties in your model, but you do have person. What you need to do is to map NaturalPersonType or LegalPersonType to person.

The easiest way to do that in your case would be to replace the last lines of your OrdersType's buildForm() with this:

    if ($this->register_type[0] == "natural")
    {
        $builder->add('person', new NaturalPersonType(), array('label' => FALSE));
    }
    elseif ($this->register_type[0] == "legal")
    {
        $builder->add('person', new LegalPersonType(), array('label' => FALSE));
    }
Sign up to request clarification or add additional context in comments.

11 Comments

If I do this then I get this error: 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".. To get a full view for my entities mapping refer to this post since I'm using Doctrine Class Table Inheritance, so how I get this working?
Are you sure both fields are called person? nat shouldn't be around anymore.
in this case yes since this is a testing data loaded from Fixtures and yes all belongs to NaturalPersonType, what you get in mind to fix the issue?
Could you post NaturalPersonType?
Yes, added to main post, take a look
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.