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

Bug: [Factories] preferApp does not work for classes with the same short name #7699

Closed
kenjis opened this issue Jul 13, 2023 · 2 comments
Closed

Comments

@kenjis
Copy link
Member

kenjis commented Jul 13, 2023

Related: #7694

When we load a class in non-App namespace ('Acme\Blog\Models\UserModel') with 'preferApp' => false,
and load a class which has the same short name ('UserModel' or 'App\Models\UserModel') with 'preferApp' => true,
the instance in non-App namespace will be returned.

preferApp | boolean | Whether a class with the same basename in the App namespace overrides other explicit class requests.
https://codeigniter4.github.io/CodeIgniter4/concepts/factories.html#factories-options

Steps to Reproduce

--- a/app/Config/Autoload.php
+++ b/app/Config/Autoload.php
@@ -42,6 +42,7 @@ class Autoload extends AutoloadConfig
     public $psr4 = [
         APP_NAMESPACE => APPPATH, // For custom app namespace
         'Config'      => APPPATH . 'Config',
+        'Acme\Blog'   => ROOTPATH . 'acme/Blog',
     ];
 
     /**
<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table = 'users';
}
<?php

namespace Acme\Blog\Models;

class UserModel
{
}
<?php

namespace App\Controllers;

use CodeIgniter\Config\Factories;

class Home extends BaseController
{
    public function index()
    {
        $users1 = Factories::models('Acme\Blog\Models\UserModel', ['preferApp' => false]);
        $users2 = Factories::models('UserModel', ['preferApp' => true]);

        return get_class($users1) . ', ' . get_class($users2);
    }
}

Results:

Acme\Blog\Models\UserModel, Acme\Blog\Models\UserModel
@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 13, 2023
@kenjis
Copy link
Member Author

kenjis commented Jul 26, 2023

Although difficult to understand, this may not be a bug.

Once an instance is held in Factories, it is a specification that the shared instance will be returned.

When 'UserModel' is specified, the shared UserModel instance is returned, so the option 'preferApp' => true passed at runtime is ignored.

Anyway #7733 will solve this issue.

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 27, 2023
@kenjis
Copy link
Member Author

kenjis commented Aug 26, 2023

Closed by v4.4.0.

@kenjis kenjis closed this as completed Aug 26, 2023
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

1 participant