CodeIgniter Forums
Dynamic $allowedFields? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Dynamic $allowedFields? (/showthread.php?tid=78226)



Dynamic $allowedFields? - blaasvaer - 12-17-2020

Hi,

I've created a dynamic API where the schema for the resource is built on the client based on DB field types (think Content Types in a headless cms). Now, all this dynamic breaks, because I 'have to' hard code the 'allowed_fields' property in the model (I'm actually passing them in from a config in the Controller, but anyway).

Now, are there ANY clever way – apart from passing the fields from the client as an array and somehow inject them into the Controller, and then pass them on to the Model, which is the only way I can see at the moment – to make this dynamic in a (built in/feature) way?

I'm thinking a way to 'override' or 'ignore' the $allowFields all together. As I understand from the docs, all fields NOT defined here will be ignored.


RE: Dynamic $allowedFields? - paulbalandan - 12-17-2020

There is one, but is in the development version. This is yet for release in 4.0.5 or 4.1.

PHP Code:
// use setAllowedFields()
$model model('MyModel');
$model->setAllowedFields($theNewAllowedFieldsArray); 



RE: Dynamic $allowedFields? - blaasvaer - 12-17-2020

Thanks, that's what I'm trying to implement myself. But I need access to the $request in my __construct, as that it where I setup everything based on configurations.

And I don't get the idea behind initController() and therefore I cannot use it. I can't implement something that doesn't make any sense to me.


RE: Dynamic $allowedFields? - tgix - 12-17-2020

(12-17-2020, 09:40 AM)blaasvaer Wrote: Thanks, that's what I'm trying to implement myself. But I need access to the $request in my __construct, as that it where I setup everything based on configurations.

And I don't get the idea behind initController() and therefore I cannot use it. I can't implement something that doesn't make any sense to me.
What needs to be setup in __construct() of a Controller that cannot be setup in initController?
You say you setup "everything", could you give an example?


RE: Dynamic $allowedFields? - blaasvaer - 12-17-2020

It’s a matter of what’s ‘standard’ in PHP ...


RE: Dynamic $allowedFields? - tgix - 12-17-2020

(12-17-2020, 12:54 PM)blaasvaer Wrote: It’s a matter of what’s ‘standard’ in PHP ...
Fair enough, but initController is the standard of the CI4 framework.

I do much of my programming in a JavaScript framework and many of the techniques they use in that framework to initialize objects and run code doesn't rely on me using native JavaScript functions, but instead use factory functions and other design patterns. In JavaScript (and especially while IE11 was around) this removes much of the hassle with browsers that act differently.
Anyhow - wishing you all the best in your efforts.


RE: Dynamic $allowedFields? - InsiteFX - 12-17-2020

PHP Code:
$request = \Config\Services::request();