CodeIgniter Forums
Factories::models breaks __callStatic function arguments passed to the class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Factories::models breaks __callStatic function arguments passed to the class (/showthread.php?tid=83463)



Factories::models breaks __callStatic function arguments passed to the class - stopz - 09-28-2022

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.


RE: Factories::models breaks __callStatic function arguments passed to the class - kenjis - 09-28-2022

What do you mean?

It seems Model constructor accept only ConnectionInterface and ValidationInterface.
https://github.com/codeigniter4/CodeIgniter4/blob/979baa1ea838cf8e6a78b76fd1ed016dad9e61c3/system/Model.php#L132


RE: Factories::models breaks __callStatic function arguments passed to the class - paulbalandan - 10-05-2022

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


RE: Factories::models breaks __callStatic function arguments passed to the class - stopz - 10-27-2022

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".


RE: Factories::models breaks __callStatic function arguments passed to the class - kenjis - 10-27-2022

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/CodeIgniter4/blob/develop/system/Config/Factories.php#L71-L73