Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Sth3Model loading from Sth2Model when Sth3Model is in subdiretory (Subdir1) #605

Closed
nowackipawel opened this issue Jul 13, 2017 · 7 comments

Comments

@nowackipawel
Copy link
Contributor

nowackipawel commented Jul 13, 2017

Inside Models directory I've got:

  • Sth1Model
  • Sth2Model
  • Subdir1
    • Sth3Model

inside one method of Sth2Model I'm creating Sth3Model
$st3 = new \Subdir1\Sth3Model();

after update my project to the newest version (commit: a5b6d93) one problem appears:

Error Class 'Subdir1\Sth3Model' not found

in that line from Sth1Model:

$sthSth = new \Subdir1\Sth3Model();

@lonnieezell
Copy link
Member

Hmm. I think there was only one change in that commit that might have affected this, but it was fixing an existing bug in the FileLocater algorithm, I believe.

How are your model's namespaced, and your namespace defined within Config/Autoload? Or are they not namespaced at all?

It looks to me like the call should be: $st3 = new \App\Models\Subdir1\Sth3Model.

@nowackipawel
Copy link
Contributor Author

nowackipawel commented Jul 13, 2017

Ofc I tried also sth like \App\Models... so I had to changed my code.
Anyway I created Subdir1 again with class AbcModel

` namespace Subdir1;
class AbcModel extends \CodeIgniter\Model
{

protected $table = 't_abc';

protected $primaryKey = 'abc_id';

public function __construct() {
    parent::__construct();
    return $this;
}

} `

and from other class which is 1 level up (root of models directory) i tried to create AbcModel()

new \App\Models\Subdir1\Abc();

but it ended with the same exception.

There is nth special in Autoload:

`
$psr4 = [
'Config' => APPPATH.'Config',
APP_NAMESPACE => APPPATH, // For custom namespace
'App' => APPPATH, // To ensure filters, etc still found
];

	$classmap = [
	    'PHPMailer' => APPPATH . 'ThirdParty/vendor/phpmailer/phpmailer/class.phpmailer.php'
    ];

`

@lonnieezell
Copy link
Member

The namespace of the AbcModel class is wrong. It should be App\Models\Subdir1.

@nowackipawel
Copy link
Contributor Author

nowackipawel commented Jul 13, 2017

Yeah, sorry for that, but I'm sure it was working in that way before cause I've got copy with previous code where namespace was only Subdir1 . Sth has been changed from previous version.

@lonnieezell
Copy link
Member

Did the previous version actually have a namespace defined in the class? It's possible that it might have worked without a namespace, though I honestly haven't tried that in a while. With a namespace though, it's not possible for it to work that way.

@nowackipawel
Copy link
Contributor Author

nowackipawel commented Jul 13, 2017

Real code:

root@p:/home/pi# cat backup/application/Models/Admin/UserLoginModel.php 
<?php
namespace Admin;

class UserLoginModel extends \CodeIgniter\Model
`
```

@lonnieezell
Copy link
Member

What you're showing there should not work. With a namespace of Admin and a class name of UserLoginModel, and no separate Admin namespace created, it would look for the file at /Admin/UserLoginModel.

Don't know what to tell you other than that's simply how namespacing and the PSR4 autoloading work. There's no magic $this->load->model method anymore, it's simply new SomeModel() so the rules are pretty simple. The only possible other way is if there was no namespace on the class at all, then it would look for files in the "legacy" CI3 folders of Application/Controllers, Application/Libraries, and Application/Models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants