We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How can i call an instance of my model class from the DI? Can i?

Is there a way to avoid wording like $model = new ModelName() all the time? Can i call my models from Dependency Injector without actually creating a new instance like mentioned above?

First of all - for what you need new instance ? If you are doing new ModelName() then it means you need to save it then, then yes - you have to call it every time, but if you need to select some model of this class there are modelsManager/ModelName::find/ModelName::findFirst/ModelName::query



14.9k
edited Apr '16

Why we don't create an instance for View for example, we just call it $this->view and it works. My models are already in bootstrap, as well as View, why i can't call them like view, request, etc classes? Or i can? Then how i can do that? :)



145.0k
Accepted
answer

But why you want to do that ? $this->view is just one object, you never creating new instance of it. Why you need only one instance of your class in di ? I don't get it.



14.9k

uh, i did not know there is always only one object in di. :( So, there is no way to get a pool of objects that can be instantiated as new objects?

edited Apr '16

But why you need it ? If you need object of some class you do new Class() like in any normal language. Just tell me why you need this behaviour and we maybe can figure something out. To create new object use new Class(), i don't see any logical behaviour for creating new instance of model calling di, no sense in it.



14.9k

it was just a thought of how i can get rid of many new Class() in my code

edited Apr '16

But why you want get rid of it ? What is your use case ? When you are using it ? For what ? Etc etc.

If you need to create for example new user and create it in database - then you should aways use new SomeClass(). If you mean something else - then tell me what.



14.9k

Doing some refactoring and thought i can use new instances for my models from di and without writing new Class() to have more static look of the code. Sorry, nothing special behind the curtain :) Thank you very much for you help. I learned a lot!

edited Apr '16

It's really bad behaviour and you should NEVER do it. new Class() give you stubs from IDE, you can jump to declaration etc etc. What you want won't be user friendly, and also you would make sure you always have this service setted in di. Also just for instance of course you can do it:

$di->setShared('somename',function(){
    $user = new User();
    return $user;
});

and then in code:

$di->getShared('somename')

But it's really, really, really bad and you shouldn't use it. Di is for storing services etc, not for creating object instances, especially not for creating model instances.

You can do it perhaps in ControllerBase if your child controllers extend one main parent controller.



14.9k

Di is for storing services etc, not for creating object instances Thank you, Wojciech! There has been my misconception about DI. I thought, it is used to create object instances. Understood a lot from you today!

Well, when you access something rom di it's calling function which can create object instance and return it. But it's it shouldn't be used for creating model objects, or just any object.