Using something like a helper in model |
Hi there!
How to start... I have three tables with, let's say, "comments", "pages" and "somethingelse". All have a colmn "slug", which is the ID in the URL to access them directly. For all this three tables I have a model and in all three models is the same method "createSlug()" which is doing exactly the same: creating a slug, check it against the database an increment a sufix to the next one or a free one between others. PHP Code: public function crtSlug(string $name, $test = FALSE) My first Idea was to create a helper, but as you see, the method uses the model and the helper has no access to the "builder()". So my question is: how to create global method which are only works local, in the specific model? Thanks!
You can create a base model class that contains crtSlug() and make your three models extend this base class.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Oh, that was easy :-) I thought, it would be a problem to use the builder.
For all other noobs like me, here is what I did: First, I extend the Model: PHP Code: <?php PHP Code: <?php Thank you!
Cool, glad it solved your problem!
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
And I found more ;-)
My method to create a slug is always used on INSERT and UPDATE. In that case, you can make use of events: https://codeigniter.com/user_guide/model...del-events It's on my todo-list for my program... |
Welcome Guest, Not a member yet? Register Sign In |