akeeba / fof

Rapid Application Development framework for Joomla!™ 3 and 4
0 stars 0 forks source link

Relation 1-N #588

Closed Eighke closed 8 years ago

Eighke commented 8 years ago

587

Nic,

Maybe I should give you more informations. I was never able to use Model field for this component. The Model field is eating my memory like crazy:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 77 bytes) in libraries/joomla/database/driver/pdo.php on line 828

I need to add 200 Mo per Model field used in the XML (I have 3...). I'm currently migrating the component to FOF3 and I have nothing but XML form. The only way to decrease the memory leak is to decrease the number of row in the database...

I had this problem on many components, so I tried to find a solution. And using the relation work great. Even with 1 millions rows. :)


The second thing, even if I had low row in my table, it think it is more natural to use the relation than the model field.

There. :)

nikosdion commented 8 years ago

Neither is correct in your use case. You just need to use the built-in relations support in browse forms (NOT the Relation form field which is something else entirely)

  1. Create a relation in your Model's constructor, e.g. $this->hasOne('user', 'Users', 'user_id', 'user_id');
  2. Enable eager loading in your Model's constructor, e.g. $this->with['user']); Eager loading consumes more memory (loads the N rows visible in the browse page) but is faster, lazy loading in a browse form will end up consuming the same or more memory and is much, much slower.
  3. In your form reference the field of the relation as name="user.username"

Regarding edit views, I'm afraid none of the built-in fields will make sense for tables with thousands to millions of rows. Remember, both the Relations and the Model field are designed to create drop-down lists. What you need is to open a popup form, e.g. like we do with user selection. There is no form field to do that mainly because it requires a special view template in your view and some Javascript on your form page.

Eighke commented 8 years ago

Wait wait, you can loading relation using name="user.username" in the XML form? I stay in a cavern for too long... Thanks for the explanations. :)

(it is mainly for the browse view, in the edit view I had to use Select2 + autocompletion to limit the number of result).

nikosdion commented 8 years ago

Yup :D Go to https://github.com/akeeba/fof/wiki/XML-Forms#optional-attributes and look for relation_depth in that documentation chunk for more information.

Eighke commented 8 years ago

Oh nice. Thanks :)