15

How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?

If so, please let me know how I can do so

hakre
  • 193,403
  • 52
  • 435
  • 836
user765368
  • 19,590
  • 27
  • 96
  • 167

3 Answers3

27

It is possible but pretty bad practice in a MVC framework. You should re-think and re-organize your code if you think you need to use the component in a model because something is cleary wrong then.

A component is thought to share code between controllers, only between controllers.

To share re-usable code between models it would be a behavior. For a view it would be a helper.

If you have some really generic code it should be a lib or put it in the Utility folder / namespace or create a new namespace. Check the existing classes there to get an idea what to put in there.

No code was provided so it is not possible to give any real recommendation on how to refactor it. However the way you want to use the existing code won't work in the MVC context, so time to rethink your approach of whatever you try to do.

floriank
  • 25,546
  • 9
  • 42
  • 66
11

It is possible to use a component inside a model (but I cannot comment if this is a recommended or a best-practice).

Inspired from original source, an example to use a component called ‘Geocoder’ in a model:

App::import('Component','GeoCoder');
$gc = new GeoCoderComponent(new ComponentCollection);

Then you can use $gc to call the functions of the component.

--

P.S.: I do not want to encourage bad programming practices, but sometimes the pressure of deadlines (in real world projects) may force a developer to make such decisions.

  • Some time our senour also pressure us becase thay dont know what is mvc arch(I think). – Debasis Mar 13 '15 at 12:05
  • 2
    "Deadlines" are a *very bad* excuse for not writing proper code. It's nothing that requires "work" just put it in the right place. Not doing it properly is playing Jenga: Move code around and pile up crap until it crashes. It's a matter of minutes of moving code in the right place except the whole "architecture" is already completely screwed up. You'll just end up with an unmaintainable mess that will become horrible expensive to maintain and debug at some point. – floriank Apr 01 '15 at 14:27
  • You mistake my intentions. I used to read Code Complete 2 in college, so that I will write better code professionally. I read PSR standards in own time, so that I can implement them at my day job. I hope I am making clear where I am coming from - I am an 'OCD perfectionist programmer', and I would not _knowingly_ cut corners with my code. I learn something new daily, and the same task I do six months later will be more efficient, this I accept. –  Apr 02 '15 at 16:39
  • As per your idea about deadlines, kindly refer to the Iron Triangle. **To produce anything good requires time**. I wish this were not true but sometimes inefficient techniques have to be adopted - can't eat your cake and have it too! And you should see how offices in off-shore countries work, to get a feel of fear and panic driven development in which less time is given to less experienced people to complete more complex tasks. –  Apr 02 '15 at 16:42
  • "And you should see how offices in off-shore countries work" - I don't want to see that. I had to do code reviews on these software ruins that were mostly a complete collection of how to *not* do something. My recommendation was almost always the same: Get a company with real programmers to rewrite the application. Fixing it didn't made any sense from an economical perspective. Writing good code and follow good practice *doesn't* require time, as you just do and follow it. Your iron triangle becomes a Bermuda triangle by sacrificing good practice in the first place. – floriank Aug 07 '15 at 08:25
  • I appreciate your frustration in the code reviews of projects created in most of the off-shore companies, something like being deceived. It's not difficult understand how foreign exchange rate, inability of client to _personally_ visit the premises and **unscrupulous character of the contracting agency** combine to promote **hiring un-skilled or less-skilled people (often freshers) who work straight on the client's project without training**, all the while deceiving client to the skills and experience of workers. But then, _foreign exchange difference is the reason why work comes to them_. –  Aug 09 '15 at 02:21
  • Or to put it concisely, churning out projects becomes the goal rather than churning out quality projects (which can be done by spending on skilled personnel, on training etc). –  Aug 09 '15 at 02:26
  • Some times cake only provides access to a functionality through a component. Like AuthComponent and AclComponent. – Yisrael Dov Oct 22 '15 at 21:00
  • 3
    @YisraelDov which should be taken as a hint that you don't need those classes in your models, and should look for a different solution. – AD7six Oct 23 '15 at 09:38
-2

@AD7six

// Use anywhere
AuthComponent::user('id')

// From inside a controller
$this->Auth->user('id');

From the cake PHP documentation they provide AuthComponent::user('id') so that it can be used in places other than a controller.

Maybe I need a bigger hint, but why shouldn't my model be able to access ACL information ?

Yisrael Dov
  • 2,369
  • 1
  • 16
  • 13