Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: ORM with 2 relations

ORM with 2 relations 02 Apr 2019 07:39 #15697

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Hi

I'm having a lot of trouble of getting my query populated with data from 2 related tables.
In my team model I have this:
$this->orm->select(array(
	'access',
	'access.title',
	'alias',
	'created_by',
	'created_by.name',
	'description',
	'end_date',
	'image',
	'modified_by',
	'modified_by.name',
	'ordering',
	'start_date',
	'teamnumber',
	'title',
	'venue',
	'venue.title',
	'turnups.student',
	'turnups.date'
));

$this->orm->relation('students', array(
	'select' => 'first_name,last_name,id',
	
));

But I also need a relation to turnups so my student array will contain turnup data, but I don't know how. Tried so many things now. Help needed :-)
See www.dropbox.com/s/qwjminr9ace2snj/database.png?dl=0 for database structure.

Appreciate all the help I can get :-)
The administrator has disabled public write access.

ORM with 2 relations 02 Apr 2019 12:02 #15698

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
First of all, you need to have the "students" relation declared in the model's constructor.

The declaration is automatically created by the builder when you are using it somewhere. Otherwise, will not be created.
Can you post here the code for this declaration ?

If missing, open any "team" layout, drag/drop a "students" relation (N:1 / N:m), then download it and find the "students" relation declaration in the "teams" model (plural)
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: dyvel

ORM with 2 relations 02 Apr 2019 12:03 #15699

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
ORM = My favorite feature so far
Coding is now a piece of cake
The administrator has disabled public write access.

ORM with 2 relations 02 Apr 2019 13:13 #15700

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
I am using students as a pivot table in my team layout. This is the code in my teams.php model constructor
$this->belongsToMany('students', // name
	'students', // foreignModelClass
	'id', // localKey
	'id', // foreignKey,
	'teamstudents', // pivotModelClass,
	'team', // pivotLocalKey
	'student' // pivotForeignKey
);
The administrator has disabled public write access.

ORM with 2 relations 05 Apr 2019 07:28 #15701

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
How would I get turnups into the equation? I really would like to understand ORM better, but need a bit of help :-)
The administrator has disabled public write access.

ORM with 2 relations 05 Apr 2019 23:02 #15702

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
It is all good.

I tried for you.
Downloaded your component + dataset

But then I had to create all layouts / forms manually
Why don't you use the builder for instancing these datas ?
Do you know how to do it ?

In the dataset of the sandbox, your N:m links are empty in the pivot table.
Do you have correctly populated your pivots with datas ? (using multiple lists in the "student" form / or "team" form)

You example is interresting because it uses Multiples Tunnels.
www.j-cook.pro/index.php/docs/versions/293-3-1-5-multi-tunnels

Your project is missing a lot, I had to create :
- multiple list field in the student form for turnups N:m
- load the relation "students_team" in the "student" model (for populating the previous multiple list)
- Place the same code above from your post ("teams" model)
- Write the html/php in the template grid
- population pivots datas
Coding is now a piece of cake
Last Edit: 05 Apr 2019 23:11 by admin.
The administrator has disabled public write access.

ORM with 2 relations 05 Apr 2019 23:11 #15703

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
If you don't use the builder, it is quite exhaustive work

If you goal is to discover / learn ORM, then you don't need to build all these pages (forms / layouts), just populate your database, and debug the datas.

This can help you for debug :
Place this in the model you want to debug SQL
$this->setState('debug.query', true);

But this will NOT show you the relations.
The relations are created into "loadRelations()" and "loadRelation()"
There you can dump your SQL for understanding ORM and helping you to debug your problem.

So... For helping you better, please first use the builder for designing these relations (forms / layouts), and then populate some datas in the pivot tables (in the sandbox)
Once you've done that, I can have further look.
Normaly you don't need to write a line of code (100% handled by the builder)
Coding is now a piece of cake
The administrator has disabled public write access.

ORM with 2 relations 07 Apr 2019 15:54 #15704

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
I am using the builder as much as I can. I am wondering of you was looking at the correct project? The project is called CPHDans.
I will try to populate the sandbox with data for testing.
The administrator has disabled public write access.

ORM with 2 relations 08 Apr 2019 10:01 #15705

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
What I'm looking to do is to have a list of students in a team. For each student a button with "register turnup" that populate a turnup pivot table. After a turnup is registered, I want to remove the button, and it's the last part I have issues with.
I want to avoid an extra mysql query if possible.

I tried to add both team students and turnups as pivot tables on team view in a fly page, that doesn't seem to work.
Last Edit: 08 Apr 2019 10:02 by dyvel.
The administrator has disabled public write access.

ORM with 2 relations 08 Apr 2019 10:54 #15706

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Exceptionaly, I can edit your project in the builder. I usualy never do that, but I am struggling understanding what is not working from the builder.
Can I do that ?
I will edit ONLY the back-end :
- the forms for editing the pivots (multi-select lists) - (both teams and students)
- the layouts for showing the N:m (both list and collections)
I simple terms, will place the maximum possible, then just remove / delete the unnecesary

You are using having multi-tunnels in an interresting design, there is maybe a bug somewhere, we will sort out together
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: dyvel

ORM with 2 relations 08 Apr 2019 11:33 #15707

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Yes, feel free to do that :-)
The administrator has disabled public write access.

ORM with 2 relations 09 Apr 2019 19:29 #15708

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
There is effectlively a problem.
More complex.
It is a "Multiple Tunnel" issue.
The relation names are conflicting
Keep in touch
Coding is now a piece of cake
The administrator has disabled public write access.

ORM with 2 relations 09 Apr 2019 20:02 #15709

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Thanks for your efforts so far :-)
The administrator has disabled public write access.

ORM with 2 relations 21 Apr 2019 15:06 #15722

  • dyvel
  • dyvel's Avatar
  • Offline
  • Elite Member
  • Posts: 200
  • Thank you received: 11
  • Karma: 10
Hello Chef
Did you figure it out, or can I perhaps ask for some custom help with this in exchange for some $$?
The administrator has disabled public write access.

ORM with 2 relations 27 Apr 2019 22:58 #15729

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Got it.

You are set to version 3.1.10 (beta)

The consequences of this new version is renaming of all relations.
It can create a mess in your forks.

Just "search / rename" your old relations.

The new way of naming relations is WAY BETTER, it will break compatibilities, but this is IMPORTANT one.
I will documentate it, at the moment the version is in beta.

Naming convention :
N:1
foreignTable_remoteKey

M:m
pivotTable_ourPivotKey_theirPivotKey

This new way of naming is not intuitive, but the path is contained in the name to avoid collisions.
Coding is now a piece of cake
Last Edit: 27 Apr 2019 23:35 by admin.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.111 seconds

Get Started