Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Using string clean in setters

Hello,

I have situation where Im importing some data from XML in my database... Now I have to make slugs also for few tables. So I have added slug colmun in few tables and now I have to import slugs also when importing data from XML. Basicly Im making slugs from titles and names .. so I just use this metod to transfrom names and titles in slugs :

    public static function cleanString($str) {
        setlocale(LC_ALL, 'en_US.UTF8');
        $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
        $clean = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $clean);
        $clean = strtolower(trim($clean, '-'));
        $clean = preg_replace("/[\/_| -]+/", "-", $clean);
        return $clean;
    }

And when im using this method in my Models like this:

    public function setSlug($slug)
    {
        $this->slug = StringUtili::cleanString($slug);
    }

Im getting this error:

PHP Fatal error:  Class 'Commons\StringUtility' not found in /var/www/myproject/app/Models/Cars.php on line 221

I have "use Commons\StringUtility" at top of my model so thats is not the problem. Also this method works on every other place in my code ..Controlers, Transformer. Actualy even in my Models work but when I use this method for clean string in getters like this:

   public function getSlug()
    {
        return  StringUtili::cleanString($this->slug);
    }

Could it be a namespace issue? Should there be a leading \?



8.6k
edited Apr '16

Could it be a namespace issue? Should there be a leading \?

Same thing friend .. This is so confusig, since the clean method dosent work only in setters..



8.6k
Accepted
answer
edited Apr '16

I have used PhalconUtils and its working very well...