Welcome Guest, Not a member yet? Register   Sign In
Factories::models breaks __callStatic function arguments passed to the class
#1

Hey everyone just a quick note that within framework/system/Config/Factories.php there's public static function models that calls __callStatic where new $class (...$arguments) is called and the only option for $arguments is to be object ConnectionInterface or null yet only arrays and Traversables can be unpacked.

Screenshot:
[Image: image-2022-09-28-113037055.png]

I have many projects relying on this feature that pass real array of arguments into model constructors yet now the only way is to pass ConnectionInterface object.
This issue is not critical on my end i already worked around it by using __callStatic directly, thank god it's a public function.
Reply
#2

What do you mean?

It seems Model constructor accept only ConnectionInterface and ValidationInterface.
https://github.com/codeigniter4/CodeIgni...l.php#L132
Reply
#3

I don't understand?

After array shifting $name and $options from the $arguments array, what remains is the ConnectionInterface object wrapped in an array. So unpacking will still work.

See https://3v4l.org/NCRCC
Reply
#4

I used to call my models trough CI Factory -> Models which previously allowed me to pass custom arguments, here's what my typical CI model looks (stripped comments for this thread):
PHP Code:
class Envelope extends Core
{
    public function __construct(
        protected string|int $context,
        protected string $to,
        protected string $subject,
        protected string $body,
        protected array|object $dataset = []
    ) {
        parent::__construct(
            'email_envelope',
            primaryKey'envelope_id',
            returnType'MyPackage\Email\Entity\Envelope',
          ... 

Previously i could call model(options, arguments) and extract $arguments into new model constructor.

PHP Code:
Factories::model($name, ['instanceOf' => $path'preferApp'  => false'getShared'  => false], $arguments

But now in updated code that i mentioned in creation of this thread this cannot work anymore. Alright, no big deal.

Just try not to create constraints for developers Smile i always keep it in mind and ask my self "by implementing this: am i limiting anything/creating a constraint".
Reply
#5

Factories::model() is a workaround. If anyone knows the solution, please send a PR.

Quote:    * This method is only to prevent PHPStan error.
    * If we have a solution, we can remove this method.
    * See https://github.com/codeigniter4/CodeIgniter4/pull/5358
    https://github.com/codeigniter4/CodeIgni...hp#L71-L73
Reply




Theme © iAndrew 2016 - Forum software by © MyBB