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

TOPIC:

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
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 :-)

Please Log in or Create an account to join the conversation.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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 following user(s) said Thank You: dyvel

Please Log in or Create an account to join the conversation.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
ORM = My favorite feature so far
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
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
);

Please Log in or Create an account to join the conversation.

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
How would I get turnups into the equation? I really would like to understand ORM better, but need a bit of help :-)

Please Log in or Create an account to join the conversation.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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

Please Log in or Create an account to join the conversation.

Last edit: by admin.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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

Please Log in or Create an account to join the conversation.

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
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.

Please Log in or Create an account to join the conversation.

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
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.

Please Log in or Create an account to join the conversation.

Last edit: by dyvel.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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 following user(s) said Thank You: dyvel

Please Log in or Create an account to join the conversation.

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Yes, feel free to do that :-)

Please Log in or Create an account to join the conversation.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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

Please Log in or Create an account to join the conversation.

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Thanks for your efforts so far :-)

Please Log in or Create an account to join the conversation.

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

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Hello Chef
Did you figure it out, or can I perhaps ask for some custom help with this in exchange for some $$?

Please Log in or Create an account to join the conversation.

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

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
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

Please Log in or Create an account to join the conversation.

Last edit: by admin.
  • Page:
  • 1
  • 2
Time to create page: 0.086 seconds

For starters it's just so easy to design an app in a way that I'm used to i.e. database first then views followed by customization. The fork system is pure brilliance from a developer standpoint as I can override things and still add and update my projects with minimal effort! Truly amazing to be able to build components in Joomla using incremental refinement without having to do everything by hand. Thanks for the great tool! I am so much more productive now than ever and I can't imagine building components any other way!!!!
Dave (Forum)

Get Started