How to use some dao (data access objects) in many controllers

Hi all,

I am wondering how to use / code DAO (data access object) in cake.

Actually I have the controller methods an some method to retrieve database data/ objects in the same class
I have some intenal methods to retrieve objects and I use them at many place in my controller : example listActiveUsers() in my UsersController…
I would like to separate the “real” controllers methods and the one that query the database. it will allow me to use the domain method in other class than the controller itself, separate things, decrease code, simplify tests, and more

but how use the listActiveUsers() in some other controllers or anywhere else. I think that instantiate a new UserController is not a good idea.
I have think about create a class to do that but do I need to call loadModels() (it is only available in controllers I think)
I am also a java developper and use spring + hibernate with dependency injection, it is very easy : you create a dao and it is injected directly in other class. it is not necessarily the way I want to do in cake but is there any good practice for that ? how people that have the same problem have done ?


listActiveUsers sounds like something to go into the Users model. In fact, the “real” Cake way would be to create a custom finder like findActive(...), then your UsersController would call $active = $this->Users->find('active');. A different controller might use the table registry to get the Users table and call this finder on it.