3

I am trying to create a custom class that can be called to convert dates and times for MySQL.

I am using the Yii2 Basic template

I have created a folder and a file in components called Convert.php

<?
namespace app\components;

use Yii;

class Convert
{
    const DATE_FORMAT = 'php:Y-m-d';
    const DATETIME_FORMAT = 'php:Y-m-d H:i:s';
    const TIME_FORMAT = 'php:H:i:s';

    public static function toMysql($dateStr, $type='date', $format = null) {
        if ($type === 'datetime') {
              $fmt = ($format == null) ? self::DATETIME_FORMAT : $format;
        }
        elseif ($type === 'time') {
              $fmt = ($format == null) ? self::TIME_FORMAT : $format;
        }
        else {
              $fmt = ($format == null) ? self::DATE_FORMAT : $format;
        }
        return \Yii::$app->formatter->asDate($dateStr, $fmt);
    }   
}

?>

I then try and call this method in my controller

use app\components\Convert;

...

public function actionCreate()
{
    ...
    $model->date_of_birth = Convert::toMysql($model->date_of_birth);
    ...
}

However I am getting the following error

Unable to find 'app\components\Convert' in file: /var/www/html/portal/components/Convert.php. Namespace missing?

I am probably missing something simple, but I cannot see it.

Thanks for your help.

Liam

From the comments, I have found that the error was a simple mistake, the opening tag should have been

<?php
5
  • 1
    In Yii configuration file you have to say to Yii to import folder from your Components folder, otherwise your class is not visible. Commented Oct 2, 2015 at 9:47
  • 1
    Sorry but you are wrong MrD, you don't need to import class in Yii2, it will be autoloaded Commented Oct 2, 2015 at 10:18
  • 1
    It is may be just a short open tag problem, you should always use <?php Commented Oct 2, 2015 at 10:20
  • soju, i think you may be right here.. will let you know, i have more errors lol Commented Oct 2, 2015 at 10:28
  • OK, thanks soju, the issue from the above code is the opening tag, what an idiot i am. soju, do you want to add an answer and i will tick it Commented Oct 2, 2015 at 10:31

2 Answers 2

1

This error simply means php cannot find the namespace in this file, and since the namespace seems to be correct, it is probably a php opening tag error.

Depending on your server php configuration, short open tags could be disabled, you should always use <?php.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to extend base Component from Yii2:

<?php
namespace app\components;
use yii\base\Component;

use Yii;

class Convert extends Component
{
    const DATE_FORMAT = 'php:Y-m-d';

And then put in config/web.php

'components' => [
    'convert' => [
       'class' => 'app\components\Convert',
     ],
]

and access it as

$model->date_of_birth = Yii::$app->convert->toMysql($model->date_of_birth);

8 Comments

Hiya, thanks for the answer, I saw this, I just thought i could include by the use; at the top, i am still learning about namespaces - What about then including the file in the index.php as a require, or is this going against the way Yii2 works? sorry for sounding dumb lol
OK, I followed the instructions and i have this error now - Getting unknown property: yii\web\Application::Convert
Try Yii::$app->convert , with lowercase
If I use Yii::$app->convert->toMysql($model->date_of_birth) (lower case) and I get this error - Unable to find 'app\components\Convert' in file: /var/www/html/portal/components/Convert.php. Namespace missing?
@soju what do you mean?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.