Ask your Symfony questions! Pay money and get answers fast! (more info)

How to add information to many-many connection? Symfony

  • SOLVED

Hi! I have a big question, please help me! I have a many-many relation (below) and I would like to create "service notes". The service notes will be the notes of a service of a partner. I can use embed forms, but I don't know where to put it...

1. If I add embed forms to service/edit like this, it will only save the service-servicenote relation, but the partner relation is not defined. How can I add the partner relation? (i put the partner id to the url like /service/edit/2/partner/5)


ServiceNote:
actAs: { Timestampable: ~ }
columns:
service: { type: integer, notnull: false }
note: { type: text }
relations:
Service:
alias: Service
foreignType: many
foreignAlias: Notes
onDelete: cascade
local: service
foreign: id



2. Should I create a module for many-many connection table? Unfortunately the table has no ID column at all.

Thank you so much in advance, if you can help me to solve this problem/question.


***************************************
MANY-MANY RELATION SCHEMA
***************************************

Partner:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true }
relations:
services: { onDelete: CASCADE, class: Service, refClass: LinkPartnerService, local: partner_id, foreign: service_id }

Service:
columns:
name: { type: string(255) }
deleted: {type: boolean, default: 0 }
relations:
partners: { onDelete: CASCADE, class: Partner, refClass: LinkPartnerService, local: service_id, foreign: partner_id }

LinkPartnerService:
columns:
partner_id:
type: integer
primary: true
service_id:
type: integer
primary: true

Answers (2)

2010-05-05

Eduardo Raffoul answers:

Hi, why don't you just set an id for LinkPartnerService and set the reference on ServiceNote pointing to LinkPartnerService instead the actual reference to Service. It seems to me a problem of design in your schema.yml.

If you do that, you wont need to embed forms or use any hack. You'll just need to fill up a 'regular' form and done.

------ UPDATE -------

As Karol explained on asnwer 2, Doctrine creates automatically an id field for the table. Now, i have to ask you something (as this is more a problem on the design of the model than a symfony question).

In your application ¿A service belongs only to one partner, or many partners may have the same service? If the answer is that a service is owned by one and only one partner, you should look the answer of Karol (answer 2). But if a service is 'provided' (or anything that means a service in your model) to many partners, you have yo follow what i told you, and with the change that you made to your schema, you must be ready to proceed.


-------- UPDATE ---------

As you said, now you have to create a module. The one that you create depends on what you want to do now and whats the purpose of your app (for the concrete functionality of adding a note, i would just set it as an option in the module where you list the services that has the partners). But the most important thing here is that you must be clear that you just need to use an instance of ServiceNoteForm to manage the create/update of a note.

Also, i would recommend you to read the jobeet tutorial [[LINK href="http://www.symfony-project.org/jobeet/1_4/Doctrine/en/"]]here[[/LINK]]


saki comments:

I know that this is a designing problem, but unfortunately I'm a beginner in symfony.:)

Can you show me, how to set an ID for the LinkPartnerService in this situation?

I've modified the ServiceNote schema, is that now ok?


ServiceNote:
actAs: { Timestampable: ~ }
columns:
linkpartnerservice: { type: integer, notnull: false }
note: { type: text }
relations:
LinkPartnerService:
alias: LinkPartnerService
foreignType: many
foreignAlias: Notes
onDelete: cascade
local: linkpartnerservice
foreign: id


saki comments:

Connection table now has ID column, thank you Karol!

I'm -almost- sure that I have to follow the solution you said, Eduardo. Many-many connection in this case...

Partner1 has services: service1, service2
Partner2 has services: service1, service3, service4
Partner3 has services: service2

Services are exactly the same in this situation. As you said, these services are provided to partners...

What is next step? Should I create a "note" module or a "linkpartnerservice" module? I think I should create a linkpartnerservice module where I can embed ServiceNote forms... is that correct?


saki comments:

Works fine, thank you so much for you and Karol!

2010-05-05

Karol Sójko answers:

1.
You should embed the service form in your form class in configure method:

//in ServiceForm.class.php

$this->embedForm('service', new ServiceForm());



If you want the relation to a partner you can always get from the service by

$serviceNote->getService()->getPartner();


because Partner is related to your service which is related to the ServiceNote

2.
The relation seems fine, and as for the id Doctrine automatically will create an id field if you drop the primary:true on both of the relations in linking table