The following code associates an employee with a project using a many-to-many relation.
$project->sharedEmployeeList = $employee;
The project and the employee are linked by a link bean. Link beans have their own type, in this case: employeeProject. In the database, these beans are stored in the employee_project table.
Sometimes you want to qualify a relationship. For instance, in the case of projects and employees, you might want to add a 'role' property to the relation:
list($e, $p) = R::dispenseAll('employee,project');
$p->link( 'employee_project', [
'role' => 'director'
] )->employee = $e;
While this is quite handy, it's often better to introduce the missing concept:
participant in this case. Often, when you find yourself qualifying a relationship,
you might have missed an important part of your data model. A relation or link is not a
very good substitute for this.
Be careful with adding to much properties and logic to relations, make sure you haven't missed an important concept in your domain model.
Accessing link beans
Since a many-to-many relation can be viewed as a combination of two one-to-many relations you can access the link beans through the ownList on either side of the relation. In the case of a project-employee relations you can access the intermediate bean like this:
To remove the intermediate beans upon assigning an empty array open the list in exclusive mode:
back to main menu
Donate to RedBeanPHP using Monero:
(remove spaces and new lines)
Performance monitor: this page has been generated in 0.015697002410889s. Is the performance lacking? Please drop me an e-mail to notify me!