-1

Scenario:

Customer table: id(PK),customer_name,address

Customer_detail table: id(PK),customer_id(FK),phone,email

Credit table: id(PK),customer_id(FK), credit_code, description

There is unidirectional one-to-one relation in customer detail and credit with customer table.

I have created entity class and form type for both. I am able to insert into separate form.

I want to show form like this and the value entered should be inserted into respective tables.

Customer Name:___________________

Address: ________________________

Phone: __________________________

Email: __________________________

Credit Code: ____________________

Credit Description: _____________

I find out that I have to write $builder->add('customer',new CustomerType()); this code in customer detail form and I was trying at customer form. But, in case of three entity how can I merge the form ?

The credit table has no relation with customer detail table ?

Table Diagram:

https://i.stack.imgur.com/NXioQ.png

3

1 Answer 1

1

I don't see any problem here, 1. create a CustomerType form:

...
$builder->add('customer_name')->add('address', 'textarea');
...

'data_class' => 'Vendor\Bundle\Entity\Customer'

2. create a CustomerDetailType:

...
$builder->add('customer', new CustomerType())->add('phone')->add('email', 'email');
...

'data_class' => 'Vendor\Bundle\Entity\CustomerDetail'

3. create a MyFormType:

...
$builder->add('customer_details', new CustomerDetailType()/* this field requires 'mapped' => false option and it will be handled manually */)->add('credit_code')->add('description', 'textarea');
...

'data_class' => 'Vendor\Bundle\Entity\Credit'

Also you should look at Doctrine's cascade options..

PS// In such situations I usually do like this: Customer has relationship with CustomerDetails, CustomerDetails has a relationship with Credit:

Customer:
  - field: customerDetails

CustomerDetails:
  - field: credit

after that embedded form will be look like this:

Credit:

...
$builder->add('credit_code')->add('description', 'textarea');
...

'data_class' => 'Vendor\Bundle\Entity\Credit'

CustomerDetails:

...
$builder->add('phone')->add('email', 'email')->add('credit', new CreditType());
...

'data_class' => 'Vendor\Bundle\Entity\CustomerDetail'

CustomerType:

...
$builder->add('customer_name')->add('address', 'textarea')->add('customerDetail', new CustomerDetailType());
...

'data_class' => 'Vendor\Bundle\Entity\Customer'

with cascade persist option FormComponent and Doctrine automatically save the entities..

4
  • I think you misunderstood my question...Customer has 1-1 relationship with Customer Detail and Customer has 1-1 relationship with Credit
    – sudin
    Nov 6, 2014 at 11:38
  • @sudin I understood your question, but I think your relationships have been configured a little improperly: I thnk the better way would be Customer -> CustomerDetails -> Credit, not Customer -> (Customer Details and Credit), because, think that I have an Account (say xurshi29 as username) in one website, and I can create a profile with my personal information, account is for system, profile is for me.. Everything is blonged to me should be attached to my profile.. In your case profile is like your CustomerDetails..
    – xurshid29
    Nov 6, 2014 at 12:02
  • I have added the table diagram. What's your suggestion on this ? There will be more tables with 1-to-1 relation
    – sudin
    Nov 7, 2014 at 4:33
  • I think I need to define bidirectional one to one relation
    – sudin
    Nov 7, 2014 at 9:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.