This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

magnusjackorr23239
magnusjackorr23239

Hello, I have extended the Rainlabs User plugin, to create a new table to list courses taken by each user.

With the model and controller I want to add the user_id from the current user, so that when a course is added the user_id is also added to the table.

What values do I need to added to the columns.yaml and fields.yaml to add a hidden field contain the current user_id?

Any examples and links appreciated.

Ilesyt
Ilesyt

I am assuming that a course can have many users, and a user can have many courses. With that assumption, you would need to create a pivot table for a many to many relation.

You would also need to define that in course model and plugin.php for the User extend plugin (in the boot function) Documentation: https://octobercms.com/docs/database/relations#many-to-many Another useful link: https://github.com/octobercms/october/issues/2643 Might also be helpful: https://vimeo.com/108040919

magnusjackorr23239
magnusjackorr23239

The relationship is one-to-one.

So have table of users and table of courses - all I need to do is stored the user_id in the courses table. The other fields are working it is just not storing the user_id value.

Many thanks.

Ilesyt
Ilesyt

In that case, extend the user plugin with a $model->hasOne for courses And add a relationship of $belongsTo to user in course.php file.

But I warn you that this way, a user can only have 1 course and course will only have 1 user in it.

magnusjackorr23239
magnusjackorr23239

This is what I in my courses.php file:

public $belongsTo = [ 'user' => ['RainLab\User\Models\User' , 'key' => 'user_id'] ];

And this is what I have in my plugin.php file:

public function boot() { UserModel::extend(function($model){ $model->hasOne['Courses'] = ['Ifocus\TutorApp\Models\Courses'];

 });

}

The form creates a course - but the user_id is always zero.

Many thanks for help.

Ilesyt
Ilesyt

For one, 'key' => 'user_id' is redundant, you can leave it out. You're also breaking conventions by using the multiple form of course in the models, although I don't know if it does anything. and use small letters at $model->hasOne['Courses'] to 'courses'

And then for the relation field in the form, name it 'user' and not user_id.

Last updated

magnusjackorr23239
magnusjackorr23239

This is what I have in my field.yaml

user: label: User relation: user select: id

Just presents a blank field. Ideally just need a hidden field with the user id as the value.

Many thanks

Ilesyt
Ilesyt

I see, it is the user that selects it, but it is hidden at the course.

In that case you need to reverse it.

You need a courses_id at the user. And reverse the hasOne, belongsTo

magnusjackorr23239
magnusjackorr23239

The user logins in and has his own id.

The user creates a course and the users id is stored in the course table.

All I want to do is send the users id to the course table. The user doesn't need to select a user_id as it is the users own id that is stored. If it is easier each user could have more than one course.

Many thanks for advice - really like OctoberCMS, so hope can get working.

Ilesyt
Ilesyt

So basically, the user selects a course. but you want the user_id to be stored in the course.

I understand, but with what you want to achieve, you need to store the course id into the users database. but with the relation you would be able to get the user id anyway. twig:

{% for c in courses %} {{ c.user.id }} {% endfor %}

This example code will output all the user id's if there are any that are linked to the courses. And it is like I said in my previous post, you would need to reverse the relation, switch the hasOne and belongsTo. and instead of having a user_id in the courses table, have a courses_id in the user table

magnusjackorr23239
magnusjackorr23239

The user (tutor) creates a course. Hence requirement to store course in its own table with the tutors id. I went down this route as was really easy to set-up user log-in with Rainlab's User plugin.

I just thought it would be easy to add the user id to another table.

Thank you for your patience.

Ilesyt
Ilesyt

This needs a bit more research. Because the user in the user plugin is not the same as the one from backend. You would need to connect the backend user to the front end user. And then you would need to pass the user ID that way to the relation field. You can deactivate the field though, so that people can't edit it.

magnusjackorr23239
magnusjackorr23239

Many thanks - that sounds like the issue. I had presumed the rainlab user plugin was an extension of the backend user.

I will focus on developing a front end-form as this is where data will be added.

Many thanks - was going round in circles!!

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.