0

i have problem with use in my form for different input different Entity.

Symfony 3.1.

What i mean : I have task to do form to save information about client and his order, i want that client will be in Client table and order in order, but form of one.

Now i have error with Neither the property "name" nor one of the methods ... because it search this method in order entity..

/**
 * Order
 *
 * @ORM\Table(name="order_work")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\OrderWorkRepository")
 */
class OrderWork
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Client", cascade={"persist"})
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
     */
    private $client;

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

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

I'm not show all code, but in entity i have all getter and setter.

    /**
 * Client
 *
 * @ORM\Table(name="client")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ClientRepository")
 */
class Client
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

and in controller ...

public function createAction(Request $request)
{
    $em = $this->getDoctrine()->getEntityManager();
    $prefixId = 1;
    $date = date('Y-m-d');
    $now = date('Y-m-d H:i:s');
    $orderNumber = $this->generateOrderNumber($em, $prefixId, $date);

    $orderType = $request->get('orderType');

    $order = new OrderWork();
    $order->setOrderNumber($orderNumber);

    $order->setDate($date);
    $order->setOrderDate($now);
    $order->setreturnDate('');
    $order->setOrderType($orderType);
    $order->setStatus(1);

    $form = $this->createFormBuilder($order)
        ->add('orderNumber', TextType::class, ['label' => 'Užsakymo numeris', 'attr' =>
            ['class' => 'form-control', 'style' => 'margin-bottom:15px; width:250px', 'disabled' => true]])
        ->add('date', HiddenType::class)
        ->add('orderDate', HiddenType::class)
        ->add('returnDate', HiddenType::class)

        ->add('name', EntityType::class, ['label' => 'Vardas', 'attr' => [
              'style' => 'margin-bottom:15px; width:250px'],
              'class' => 'AppBundle:Client'])

        ->add('device', TextType::class, ['label' => 'Prekė', 'attr' =>
            ['class' => 'form-control', 'style' => 'margin-bottom:15px; width:250px']])
        ->add('workPrice', NumberType::class, ['label' => 'Pristatyta kaina', 'attr' =>
            ['class' => 'form-control', 'style' => 'margin-bottom:15px; width:250px']])
        ->add('deadLine', TextType::class, ['label' => 'Numatomi terminai', 'attr' =>
            ['class' => 'form-control', 'style' => 'margin-bottom:15px; width:250px']])

        ->add('complect', TextType::class, ['label' => 'Komplektacija', 'required' => false, 'attr' =>
            ['class' => 'form-control', 'style' => 'margin-bottom:15px; width:250px']])
        ->add('description', TextType::class, ['label' => 'Gedimo aprašymas', 'required' => false, 'attr' =>
            ['class' => 'form-control', 'style' => 'margin-bottom:15px; width:250px']])
        ->add('screenPass', TextType::class, ['label' => 'Įrenginio ekrano užraktas / slaptažodis',
            'required' => false, 'attr' => ['class' => 'form-control', 'style' => 'margin-bottom:15px; width:250px']])

        ->add('surrogatephone', EntityType::class, ['label' => 'Pakaitinis telefonas',
            'attr' => ['class' => 'form-control' ,'style' => 'margin-bottom:15px; width:250px'],
            'class' => 'AppBundle:SurrogatePhone',
            'query_builder' => function ($em) {
                return $em->createQueryBuilder('s')
                    ->orderBy('s.id', 'ASC');
            },
            'placeholder' => 'Pasirinkite',
            'choice_label' => 'name',])
        ->add('save', SubmitType::class, ['label' => 'Sukurti', 'attr' =>
                ['class' => 'btn btn-primary', 'style' => 'margin-top:15px']]

        )->getForm();

    $form->handleRequest($request);

sorry for my english :)

2 Answers 2

1

In your OrderWork entity you have mapped Client entity with variable $client so in form also it should be

      `->add('client', EntityType::class, ['label' => 'Vardas''attr' => [
          'style' => 'margin-bottom:15px; width:250px'],
          'class' => 'AppBundle:Client'])`

instead of

      ->add('name', EntityType::class, ['label' => 'Vardas', 'attr' => [
          'style' => 'margin-bottom:15px; width:250px'],
          'class' => 'AppBundle:Client'])
Sign up to request clarification or add additional context in comments.

6 Comments

and i catch this error Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in orders/new_order.html.twig at line 29
i try in template form.name form.client.name and always have error.
try form.client instead of form.name or form.client.name
when i get html element select with name form[client] , but i want get all input with name from this entity , how i can to do it ? )
You should implement "public function __toString(){ (String)$this->name }" in Client Entity .
|
0

k , Now i understand your requirement . For this you have to embed ClientType form into OrderType .

Suppose You have made both ClientType and OrderType classes then you can add this like this

use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Form\ClientType;

public function buildForm(FormBuilderInterface $builder, arra $options)
{
    // ...

    $builder->add('client', ClientType::class);
}

For more detail you can follow the link How to Embed Forms . I hope this will help you a lot ..

Comments

Your Answer

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