I have a test project and I need some help. This is my URL("localhost/ums/relationscls/courseLecturer/1"). When I input "1" on the URL the controller will find student whose id is "1". It goes same with 2 or 3.
I have tables named as
courses
course_name|course_code|course_credit
students
student_name|student_number
lecturers
id|lecturer_name
relationscls
course_id|lecturer_id|student_id
I want to get student's id from the URL and match it from the relationscls table, then fetching course_id and lecturer_id from the same table.
After that, I want to fetch course_name, course_code and credit from the course table with the course_id which we found it from the relationscls table before.
How can I make it possible? Can anyone help, its emergency situation. Thanks.
You're probably looking for CakePHP's Containable Behavior. It lets you do a find then specify which related data you want to automatically retrieve.
You are probably looking to bind and unbind models dynamically. There are models behavior for this.
http://cakebaker.42dh.com/2008/02/02/attaching-and-detaching-model-behaviors-on-the-fly/ might help for you.
You'd probably like to check the HasAndBelongsToMany and 'hasMany-through' model associations in the cookbook, It might prove useful in your case.
Related
I have users, meetings and comments tables and I want users to be able to post comments on users profile and meetings.
I know how to make one to many relationships for users and comments tables, but I want all comments to be saved in one table and show comments on users profile and meetings.
This is my table structure:
users
id
name
comment_id
meetings
id
name
comments
id
user_id
comment
TL;DR The solution is to use polymorphic relationships which Laravel makes really easy.
IMHO I don't think that a many-to-many relationship will do the job. It would mean that:
a user can post multiple comments. Okay
a comment can belong to multiple user. Weird
a meeting can have multiple comments. Okay
a comment can belong to multiple meetings. Weird
Plus you would likely need two comments tables to achieve that. For instance a comment_user table and a comment_meeting.
The kind of relationship that would fit the most your situation is the polymorphic one. The name can be scary because it comes from ancient greek. But the concept behind the name is very simple. And Laravel makes polymorphic relationships very easy. You will certainly recognize your situation in the example given in the Laravel documentation ;)
While saving comment, also keep a field named model (or anyone you like) to identify users profile and meetings
HTH
I'm trying to figure out what is the best practice with selecting data from two related model.
I have a model (and a db table) "person" and then i have a model (and a db table) comment. There may be multiple comments for one person and comment has "person_id" column.
I have two cases in particular.
First i need to show persons profile with all his comments.
In my controller do I select the needed person through my person model and then select all the comments through my comments model? Or is it more correct for person model already return person with all comments?
I myself would guess that first option is ok.
Second case is where i need to show all latest comments with name of the person who made the comment.
So in my controller is it correct to select all latest comments from comments model and then select a person for each or them? Or is it more correct for the comments model to return comment with person name included?
In this case i would guess that second option is better. It seems really strange to first select comments and then iterate them and select a person for each of them.
So im kind of confused because case 1 and 2 seem similar but i would use different solutions for them. Which one is correct?
Heya I am novice web dev or actually I am still in education.
I got this situation Where I have 3 tables lets say : Students, Groups and a join table Student_group.
I put my data from Students in the student model and from groups I put its data in the Group Model so I can use it my application. But I store a date in the Student_group table because I need to know when a student changed from a group.
So my question is in which model do I put this date? Do i need to make a new model for the combined tables or do I need to add another attribute to the student model?
Thanks in advance ;D
That depends. Will the student be in many groups, or one?
If one (one to one relationship), you can decide where to put it. The column could be in either the Student table, or the Student_group. In this case, though, it may be advisable to flatten the data and simply add group columns in your Student table. You decide that as well - if it seems unnecessary to have a join for a one to one relationship (usually it is, not always), then flatten it. In either case, the data should stay in its respective model. That said, you should use the Student model if you handle it in the Student table.
If many (one to many relationship), I'd advise putting it in the Student_group table and leaving it in that model as well.
All in all, the model should be a direct reflection of the data it's representing. You could make some methods inside your Student model to make it easier to get the date, for example. However, I'd personally handle that date inside of the proper model, Student_group. As mentioned, the model should be a direct representation of the data. Again, though, there's nothing wrong with making things a bit easier by creating some methods that help out the developer.
Ok so I have an app that searches through course applications which each belong to an applicant. When I search the applications for a course title it returns the required result set but I would like to sort it by the applicant's date of birth.
How can I do this? I've tried sorting the collection doing $applications->sortBy('applicant.dob') but this seems to just order it by each applicant not the overall collection.
Edit
Here's all of my code... http://laravel.io/bin/PD81z
Any one have an idea how I can approach this?
Thanks!
OK
Your table course and table applicant are actually many-to-many relationship
For this to work, table course_applitions serves as a proxy to link the two table.
see the post, especially the part about many-to-many relationships
http://scotch.io/tutorials/php/a-guide-to-using-eloquent-orm-in-laravel
When fetching results from related tables using the above way, you can always add sort options.
Cheers!
I know this is probably really easy to do, but I can not figure it out. And I didn't find an example on Google.
I have a table 'statuses' and a table 'events'. 'events'.'statuses_id' connects to 'statuses'.'id'. Now when I make a find('all') on the event model i still get the id i entered into the database. I would like to display the field 'statuses'.'name'.
Could someone help me on how to do this? I know i have to somehow declare the fk-connection in the model, but how?
Use the belongs_to reference ("Event belongsTo Statuses") and cake automatically will bring you both the name and the id