S
S
s0lar2018-04-01 14:57:00
symfony
s0lar, 2018-04-01 14:57:00

How to properly use EntityType in Symfony4?

I have two Entiny: "Member" and "DepartmentPo".
"Member" contains a poId field that points to "DepartmentPo".

namespace App\Entity;
 
class Member
{ 
 /**
   * @ORM\Column(type="integer", nullable=true)
   */
  private $poId;

When creating a new "Member" through the form, I add the "poId" field with the EntityType pointing to "DepartmentPo" I add the
field.
$builder
->add('poId', EntityType::class, array(
    'class' => DepartmentPo::class,
    'choice_label' => 'title',
    'placeholder' => '..не выбрано..',
    'empty_data' => null,
    'required' => false,
    'label' => 'Первичное отделение',
))

In the form, the "DepartmentPo" list loads fine. When submitting the form, the selected object is substituted in the "poId" field
Member {#7045 ▼
  -id: null
  -poId: DepartmentPo {#9884 ▼
    -id: 23000009
    -moId: 23000
    -title: "Абинское №9"
    -titleMo: "23000"
  }
}

But when I try to save a new record "Member" to the database, an error is thrown
Notice: Object of class App\Entity\DepartmentPo could not be converted to int

And the request itself
'INSERT INTO member (ro_id, mo_id, po_id)
VALUES (?, ?, ?, ?)'
with params [23, 23000, {}, 6]:

And it turns out that I have an object, but there should be an object ID.
In connection with these, I have a question, how to use EntityType correctly so that saving works?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-04-01
@s0lar

s0lar , JoinColumn and Column are incompatible annotations, one overwrites the other. For the EntityType to work correctly, the field must have a ManyToOne or OneToOne relationship.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question