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


Messages In This Thread
Using something like a helper in model - by sprhld - 10-26-2021, 12:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB