1

Let say i have a function:

function getCarName() {
   return array(1 => 'BMW', 2 => 'MERCEDEZ BENZ', 3 => 'RENAULT');
}

I will use this function on both model and view, should i create two same function on AppHelper and Car Model? What is correct way to achieve this?

1
  • You would want to keep it DRY (dont repeat yourself). Therefore your approach will not work. Either make it a normal model method and use the controller to pass it down. Or use static model methods you can access similar to this approach: dereuromark.de/2010/06/24/… - It seems like you got here some enum like array - which you also might want to work with in your forms etc. Commented Apr 9, 2013 at 10:05

1 Answer 1

4

Having two functions doing the same thing is usually a bad idea as it violates the don't repeat yourself principle.

Instead, I would add the function to your Car model and then pass the data from your controller(s) to your view(s) with something like:

$this->set('carNames', $this->Car->getCarNames());

The car names are then available in the view via the $carNames variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.