ZF2 - DI into Models not directly within a controller

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2 - DI into Models not directly within a controller

intellix
This post has NOT been accepted by the mailing list yet.
Hey, I thought I had DI down until I failed to DI my tables into a model. It seems that it only works when the model is used within a controller?

I have for instance:

Register Controller > UserMapper->register() > RandomModel->doOtherStuff();

It seems I can't DI into the RandomModel as it's not directly within the controller but is needed within the UserMapper.

This is when using the EDP ZF2 Skeleton... is there any way to do this or am I going mental?

I thought this was the whole point of DI... so I can just declare what needs what outside of the Models using my config file and then when they're pulled out then the DI injects what I specify into them... It's trying to use my Model without having its dependencies injected into there.

Thanks, Dominic

Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - DI into Models not directly within a controller

SpiffyJr
Di is just a tool to configure stuff. It's not a magic ball that can foresee what you want. You have to *tell* it what you want.

On a sidenote: it's a good idea to keep your models pretty dumb and put the logic into an appropriate service. This isn't the *only* way but it sure keeps things clean. Then you would make sure you setup Di so that the service has everything it needs and the service would use the model directly.

Sorry if my answer is rather vague but it was hard to understand what you wanted to do from your post.
Kyle S
blogs @ www.spiffyjr.me
github @ www.github.com/spiffyjr
follow @ www.twitter.com/spiffyjr
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - DI into Models not directly within a controller

intellix
Thanks for the reply :) I've received no answers on any question regarding ZF2 on stackoverflow or here so it's pretty hard to figure out things like I used to for ZF1. Information is scarse! :D

I've basically got a TableRow design for a bit of my application. I have buildings in a game and all their data is kept inside the database like their name, type, costs etc.

So I pretty much have a dumb Building model that holds all the data and then my BuildingMapper fetches the data from the database and returns the filled Building model.

When you register to the game you a starting building created for you inside your town. So I was using the BuildingMapper inside my UserMapper class to create it. That's pretty much why it's not used directly in the controller as its not directly needed there.

So I have in my DI all the stuff I thought would inject the dependencies and I thought it would work something like this:

Controller needs UserMapper.
UserMapper needs UserTable, BuildingMapper
BuildingMapper needs BuildingTable.

Then I thought as stuff was needed, they all had their dependencies injected as the point they were called :)

How would I go about giving my BuildingMapper class the Table? I'm already passing the Table it's connection.

Thanks :)
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - DI into Models not directly within a controller

SpiffyJr
Take a peak at EdpUser... he uses a data mapper pattern as well.

https://www.github.com/evandotpro/edpuser
Kyle S
blogs @ www.spiffyjr.me
github @ www.github.com/spiffyjr
follow @ www.twitter.com/spiffyjr
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - DI into Models not directly within a controller

intellix
Just had a long look through it all, it only seems like hes using Doctrine and ZendDb to login/register a user. I couldn't really see anything related to the problem I'm having though. He's injecting into 1 model that's used in his controller (user).

The problem I have is that I need to inject into a model that's not part of the controller but required by the User.

I've not seen any example of this yet, all the examples I've seen so far are basic 1 model, 1 controller concepts like AlbumController, getAlbums where DI makes perfect sense, it all ties into it perfectly.
Not really seen anything with more of a backend doing stuff not directly associated with the controller.

When I first got DI working I sort of had a 'oooo aaah, that makes perfect sense!' moment where you specify all the dependencies for everything and as soon as they're needed they're given them but that only works when directly in the controller and now I'm confused as to why it's not how I thought it would be.

Surely DI was made to be a little more complex than to give access to a UserMapper from the Login/Register page and give access to AlbumMapper from the displayAlbums page?

I do like his DiDbAdapter class though as in the akrabat ZF2 tutorial he injects the DbAdapter directly into the table... but I have about 90 tables so I'd have to copy and paste the DI code 90 times right? I guess EDP injects it into one, sets the default adapter there and then uses it throughout
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - DI into Models not directly within a controller

intellix
This post has NOT been accepted by the mailing list yet.
I've figured it out! It is in fact possible to keep it going without having it directly within the Controller and I know why I originally thought that.. my code went something like (which was wrong):

RegisterController needs UserMapper so I injected it in

UserMapper {
       $buildingMapper = new BuildingMapper();
}

I guess it works by chaining them..... so doing new BuildingMapper() is the issue... it bypasses the DI chain.

So to fix this:

RegisterController needs UserMapper so I inject it in
UserMapper needs BuildingMapper so I inject it in

UserMapper {
     $this->buildingMapper->doStuff();
}

I had the injection defined in my config file all along but it wasn't injecting into BuildingMapper because I was using the 'new' keyboard to create a new object... I see that it works as I thought all along and this is the reason it wasn't working :) I see it all chains and I can now ban myself from ever using the 'new' keyword again as DI holds that instance of it with its dependencies created if needed.

Thanks :)