Welcome Guest, Not a member yet? Register   Sign In
Using something like a helper in model
#1

Hi there!
How to start... Smile 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)
    {
        // Slug is made of the name/label 
        $slug mb_url_title($name'-'TRUE);

        // check usage
        $chk $this->builder()
            ->select('slug')
            ->like('slug'$slug'after');

        $used $chk->get()->getResult();

        // create an array with all used _n IDs
        $usedDB  = [];
        $i '0';
        foreach ($used AS $str)
        {
            $sub explode('_'$str->slug);

            if (empty($sub['1']))
                array_push($usedDB'0');
            else
                array_push($usedDB$sub['1']);

            $i++;
        }

        // create an array with the content lines
        $rowsCnt range('0'$i+1);

        // compare both arrays and save the missing numbers
        $next array_diff($rowsCnt$usedDB);

        // create slug with the first number of a reseted array
        // and return it
        return mb_url_title($name.'_'.reset($next), '-'TRUE);

    

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!
Reply
#2

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! ***/
Reply
#3

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

namespace App\Models;

class 
BaseModel extends Model
{

    /**
    * Creates a slug from a given name and increase sufix if slug is in use
    * @param $name STRING to make a slug from
    * @return string
    */
    public function crtSlug(string $name)
    {
        /* Stuff like above */
    }


And then extend this at the other three models:
PHP Code:
<?php

namespace App\Models;

class 
PageModel extends BaseModel
{
    /* use $this->crtSlug(); in every method here as you want */

Hmm... This is solved, but a further question on this... We well see us soon :-)

Thank you!
Reply
#4

Cool, glad it solved your problem! Smile
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

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...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB