Laravel ORM: unique combination of fields from pivot table - mysql

This question extends the example Eloquent : Working With Pivot Tables provided in the Laravel Documentation.
The relationship here is that a User has many Role objects that it can relate to, and each Role is made up of a number of Task's.
The relationships between these models are:
User and Role:
Each Role may only relate to one User, however a User may have many Role's
A one-to-many relationship
Role and Task
Each Role can have many Task's associated with it, and each Task can belong to several Roles
A many-to-many relationship
In terms of a database schema (MySql), we therefore have:
A users table that has no foreign keys
A roles table with a user_id foreign key
A tasks table
A role-task pivot table
In plain English, the constraint I need to apply is that a user can not be associated with multiple roles that are made up of exactly the same tasks, however two role's can be made up of exactly the same task's
Is there an in-built way to apply this constraint using Laravels Eloquent ORM system? If not, what would be a tidy workaround?
edit:
Eloquent-like description:
User hasMany Role
Role belongsToMany Task
Question: How to prevent assigning to a user multiple roles having exactly the same related collection of tasks

Related

Managing many-to-many relationship in react-admin

React-admin enterprise edition provides a module called ra-relationships. This gives the ability to deal with many-to-many relationships in the interface. I'm building an app with users and roles as separate resources (i.e. a user can have multiple roles and vice-versa).
From what I can see, the ra-relationships module relies on the premise that your primary keys in all tables (user, roles and user_roles) are simply named "id". However, this particular app has the primary keys as user_id, role_id and user_role_id.
Has anyone come across a method whereas the module can work with this kind of set up?

Can 2 relationships have a relationship between them (Database)?

Consider the scenario between a Project, Task, and a User (ER diagram attached included):
A project can have multiple tasks and a task can be in multiple projects (thus the many-many relationship ProjectTask)
A project can have multiple users and a user can be in multiple projects (thus the many-many relationship ProjectUser)
Check the above example's ER Diagram here
Now a ProjectTask can be assigned to multiple ProjectUsers and a ProjectUser can be assigned multiple ProjectTasks, basically a TasksAssigned relationship
How will I go about storing that information in a database? Can two relationships have a relationship? If yes, is there a better way to showcase this scenario? And if no, how do you show this relationship?
You can have a three-way relationship, in your case:
user
project
task
You probably want to guarantee that the user and task have the same project. That can be handled by declaring composite foreign keys to the projectTasks and projectUsers tables. The structure guarantees that the user and task are in the same project (because it is explicitly mentioned in the row in this table).

MySQL modelling task

I have next task to do in MySQL:
We have Accounts, Farms, Customers, and Users
An account belongs to a Customer
A User has access to one or more accounts
A User has access to one or more farms tied to that account
A farm is tied to one account
Here is ER Model:
Can someone take a look and check whether I made this correct?
For this SQL modeling task, you have three types of entity relationships: one-to-one, one-to-many and many-to-many (How to implement one-to-one, one-to-many and many-to-many relationships while designing tables? for more detail on their implementation).
In your toy model, if "a User has access to one or more accounts," this is modeled as a one-to-many relationship between the user and the accounts, which requires a foreign id on the many side (i.e. every account has a user_id) but user does not have an account_id.
The account-customer relationship appears to be well-modeled, because it is one-to-one (so either table can hold the foreign key), but your one-to-many relations appear to have some errors.

Intermediate SQL Table : What for?

I'm managing to create my first complicated J2E Solution and in every tutorial I find some sort of intermediary tables usage, like here :
Tables : User, User_Roles, Roles
While logic would simply add a key to user Table referring to it's role on Roles table, why the usage of that intermediary table ?
I thought it's one or two developpers choice, but everywhere I look for a tutorial, I find this sort of sql schema.
Is it better ? Does it help in something particular ? Speed, security ? Cause from a logic point of view, using one table User and a foreign key to Roles is better.
Thank you
This is a common database relationship modeling called M-N (Many To Many). A User can have many Roles, and a Role can be assigned to many Users, so you need the intermediary table. Here's another example: a Teacher can teach many Classes, and each Class can be taught by many teachers (during different semesters, for example). In this case you need a Teacher-Class intermediary table.
A different kind of relationship is 1-N (one to N). A User can have many Telephones, but each Telephone is owned by a single User. In this case, a User's primary key (PK) is exported as a foreign key (FK) into the Telephones table. No need for an intermediary table.

CakePHP: Using a join table on non-HABTM relationship

I have a relation represented in MySQL that is
Classified belongsTo Category
Category hasMany Classifieds
Previously, I have had Classified.category_id set, but some Classifieds do not have Categories, and many Categories do not have any ads. This relationship is optional.
I have normalized the database to represent the database in a third table, categories_classifieds.
categories_classifieds
----
classified_id (primary, not null)
category_id (not null)
The reason this was normalized this way is to avoid null values, and also to
I am trying to create Models for the database in CakePHP 2, and there does not seem to be support for this simple relationship. Is this possible? Does the categories_classifieds join table need to be made into its own Model?
Thanks in advance for any insight.
Here are crow's-feet notation ER diagrams of the current, and proposed schemas:
http://i.stack.imgur.com/1BBVV.jpg
This is a hasAndBelongsToMany relation.
Make sure to follow cake's conventions though and make a seperate id field as primary key for the connecting model. You may not even need to have a model created for it and let it all to automagic after building the necessary relations in the models.