• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4 Model or Entity with Relation between two tables

#1
Hello Guys,

i love the new style of models. But i think one of needed thing is to set relation between to tables(models).


When u have two tables (or more) like this:
table1
id
users_id
content

users
id
username

So when i make a model on "table1" it should be possible to join table "users". This was possible with grocery_crud. Is this already possible and i couldnt find it?

I think the moste time you will have some relation between two tables or more. I know i could do it by $afterFind, but it wouldnt the performance would be really bad...

Hope i could explain, what i want.

Thanks for your Help
Reply

#2
you can set your join in the before find and explode data in the after find.

for example in cakePHP, when you use join in a query builder, this is what it return :

Code:
{
  "User": {
    "id": 1,
    "username": "foo",
    "Table1": {
      "id": 1,
      "users_id": 1,
      "content": "bar"
    }
  }
}

but in your query, you will have just 1 row.

So how does it work ?

the magic is in the fields name. cakephp prepare his query with a prefix for his fields name in all before find.

NOT : SELECT * FROM [...] LEFT JOIN [...] where users.id = 1

BUT

SELECT users.id as User__id, users.username as User__username, table1.content as Table1__content FROM [.....]

and he explode new formated fields name in his afterFind to reconstruct a correct & complete object.
Reply

#3
There is no intention at the moment to turn it into a full ORM. That's a huge job, and very complex one to do right, and we're still trying to get a final out for 4.0.

The easiest solution is to simply create new methods in your model to retrieve those for one or more. This isn't any different than would have been done in prior versions of CodeIgniter.

If however, you're feeling adventurous and want to create an ORM on top the existing tools, I know we'd all love it. I've started playing with the concepts over here but won't have time to explore it any more for quite some time, if ever. Feel free to take that as a starting point, and let us know when you get something up and running. I know I'd be very interested in seeing it.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.