yii2ubuntu-16.04yii2-active-records

yii2: model relations error can't find another class in ubuntu server 16.04


I have upload my YII2 project to ubuntu 16.04. My source is no problem when run on localhost on my computer, but when I run it on the server ubuntu 16.04 with network, it has a problem. The model source can't find another relation model

public function getLokasiAwal()
{
    return $this->hasOne(KotaBandara::className(), ['id_kota' => 'lokasi_awal']);
}

and i have error

Class 'backend\models\TypeNonstaf' not found

I have found the solution, I added the following code:

use backend\models\Kotabandara;

On top in model file but, in my source in localhost, I do not need to add that code

Can someone explain that issue??


Solution

  • As @rob006 pointed out, it appears that you had been working/running your app on a Windows local file system, which is case-preserving, but not case-sensitive.

    When you first call upon a namespaced class directly or via the use operator, it passes this full class name as $className into yii\BaseYii\autoload::($className) (Yii2's global class autoloading handler), which in turn attempts to include the corresponding class file, if found.

    So, on your Windows local machine, when you use backend\models\KotaBandara, it will find and include any file associated with the corresponding path alias in a case-insensitive manner, thus it will find any of:

    There can be only 1 target file with this sequence of paths/characters anyway.

    However, when you migrate this code to a Ubuntu system, which is both case-preserving and case-sensitive, there is a distinct difference between KotaBandara.php and kotabandara.php and in fact both files can exist side by side, unlike on Windows.

    So, you have to be precise here - on Ubuntu, use backend\models\KotaBandara will trigger the autoloader to find only the file whose path AND case matches, i.e. KotaBandara.php. If you named the file kotabandara.php, it will be found on Windows, but not on Ubuntu!