Welcome Guest, Not a member yet? Register   Sign In
CI4 Model or Entity with Relation between two tables

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:


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

(This post was last modified: 02-27-2019, 10:56 AM by keulu.)

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 :

  "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


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.

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.

Theme © iAndrew 2016 - Forum software by © MyBB