Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a system that have lots of users each has his own information so I needed to create a model for each. On the other hand, all those users have a common user model where their credentials being collected form those requirements it was suitable to have a polymorphic relation between all user types and the user model i.e., coordinator as a model and the user as a model I did the following

class Coordinator extends Model
{
    protected $fillable= ['userid', ...];
    ...

    public function user()
    {
        return $this->morphOne(User::class, 'userable');
    }
}


class User extends Model
{
    ...

    public function userable()
    {
        return $this->morphTo();
    }
}


class CreateUsersTable extends Migration
{
    public function up()
    {
        $table->bigIncrements('id');
        ...
        $table->morphs('userable');
    }
}


class CreateCoordinatorsTable extends Migration
{
    public function up()
    {
        $table->bigIncrements('coordid');
        ...
       $table->foreign('userid')->references('ID')->on('wp_users')->onDelete('cascade');
    }
}


After migration I noticed that columns userable_type and userable_id not allowing null. How come I create a coordinator entity with its associated user entity?

What I have tried:

Now I changed the columns nullability to allow null and store the user model then get its id and use it when storing the coordinator.
Posted
Updated 31-Dec-18 12:35pm
v2
Comments
Richard MacCutchan 31-Dec-18 10:42am    
Why not just have a single "user" model that contains all the information. One element of the content should be a flag or flags which indicate the user type (ordinary user, administrator, special user etc).
Dave Kreskowiak 31-Dec-18 11:04am    
You could also just have a single User object that contains all the common information for every user and then a "property bag" for the custom stuff for each user. This would be just a key/value pair collection.
Amr Mohammad Rashad 31-Dec-18 14:25pm    
I am using PHP specifically its MVC framework i.e., laravel and its ORM framework eloquent and I am using the ORM to store/fetch and according to my knowledge it does not work that way
Dave Kreskowiak 31-Dec-18 15:31pm    
Ummmm... You just said that your ORM cannot do handle a Master/Detail relationship, like a customer order or invoice. That's just not possible for an ORM that's worth it's weight.

You're over-thinking, and over-complicating, this. You would end up with two tables. One for Users and one for UserProperties, where you store your individual-specific property data. That is data that doesn't show up as a specific property in a class, but as a collection of "loose" properties.
Amr Mohammad Rashad 31-Dec-18 18:23pm    
The case not a simple master\detail because eloquent definitely handle this but what I am talking about many tables\models one for each type (i.e., coordinator, hospital, shipper etc.) each has its own id column and a user table\model has columns stores id value and a type value according to the type that user entity relates to. For example, I stored a coordinator within a coordinator table with an id value of 10 then within the user table a column called userable_id will have the value 10 and a column called userable_type will have the value 'coordinator' if I stored a hospital within the hospital table with an id value of 5 then within the user table a column called userable_id will have the value 5 and a column called userable_type will have the value 'hospital'...

1 solution

I got it after searching Laravel One to Many Polymorphic Relationship - Create Records.

The idea is not as I thought at the beginning. When I used the
PHP
$table->morphs('userable');
the user table had two columns, userable_id and userable_type, and each allows no null by default and by ORM convention and I was thinking that they consider the user table is the master and other tables (i.e., coordinator, educator, shipper etc.) each as the detail table. According to this initial wrong understanding I was adding a userid column at each table to store the related user id for each specific user type and I was wondering how I am going to save the user that needs the userable_id and userable_type to be filled by saving the specific user first to get its id that will be provided to the user's userable_id the case resembles a deadlock situation as each table needs a piece of information that will be known after storing data on each to be able to save data to each table, wired!!!.

However, when I read the article in the above link I figured that it considers the table in an opposite way, unlike my thinking was (i.e., coordinator, shipper, etc. are the masters and the user table is the detail). That is for guys who use the models directly but for those who use repository package it is a little bit tricky and you need to do an extra work...
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900