Many_Many Relation Crud Operation With Json Api

Hello,

i have the following problem:

There are three database tables: user, role and userrole. These are the model relations:

User:




return array(

    'roles' => array(

        self::MANY_MANY,

        'Role',

        'userrole(user_id, role_id)'

        ),

    );



Role:




return array(

);



UserRole:




return array(

    'user_id'=>array(self::BELONGS_TO, 'User', 'user_id'),

    'role_id'=>array(self::BELONGS_TO, 'Role', 'role_id')

);



So i want to create the user with his roles. If i delete the user the foreign keys from the userrole table should be deleted and updated if the user exists already. Can you help me if this works this way or do i have to change anything?

I want to use this with a JSON API to CRUD the user.

For example i want to GET this:




{

    id: "1",

    username: "admin",

    firstname: "Admin",

    lastname: "Istrator",

    roles: [

        {

            id: "1",

            name: "first role"

        },

        {

            id: "2",

            name: "second role"

        },

        {

            id: "3",

            name: "third role"

        }

     ]

}



And this should CREATE a new one with his roles and write the data in user and userrole table:




{

    "username": "admin",

    "firstname": "Admin",

    "lastname": "Istrator",

    "roles": [

        {

            "id": "1",

            "name": "first role"

        },

        {

            "id": "2",

            "name": "second role"

        },

        {

            "id": "3",

            "name": "third role"

        }

    ]

}



Any ideas for the JSON API?

I hope you understand the problem. If you have a question, don’t hesitate to ask.

Thanks in advance!