3

i'm searching for a strategy to create a one to many raltionship in ZF2, between two tables using TableGateways. Table one is called "workers" and one worker has many "departments".

Now i want to have a form for the workers which inlcudes a special DepartmentFieldset (Textfield to enter an ID).

I was thinking about this way: The worker Model has one more type array for the departmentIDs, but there is no field in the table for the departmentIDs. Also there is a getter and setter for this field.

EDIT Action: In the editAction() of the workers TableGateway, i have to fill the normal form + the form collection for the IDs, so i need data from both tables.

$worker = $this -> getWorkerTable() -> getWorker($id);
$departments = $this -> getDepartmentWorkerTable() -> getDepartments($id);
$worker -> setDepartmentIds($departments);

What type must the departmentIDs field in the worker Model be, that i now can use

$form = new WorkerForm();
$form -> bind($worker);

?

Let's now asume, data is posted and validated (Validation is set with the getInputFilter() in my Model). I want to save all the data:

$this -> getWorkerTable() -> saveWorker($worker);
$this -> getDepartmentWorkerTable() -> saveDepartments($worker);

Is that the right way to do it? I think if this would work, the ADDAction is not the problem.

So the main questions are:

How can i set up a Model to handle one to many relationships? How must the exchangeArray() function be written?

How can i fill up a Form with ElementCollections with the Data from the Many Realtionship Table?

I think the main problem is, how to use an entity filled by the TableGateway which additionally contains an extra field for the relationship.

Some valuable tips would be great!

Regards, Yuri

Yuri
  • 33
  • 4

0 Answers0