Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:21:06+00:00 2026-06-14T07:21:06+00:00

I’m building a Zend Framework application wherein the Model layer is separated into Services

  • 0

I’m building a Zend Framework application wherein the Model layer is separated into Services and Models. Controller actions call service methods, which in turn call model methods.

For instance: LanguagesController::addAction() checks if a form is submitted and valid. If so, it passes the form data to Service_Language::add(), where some business logic is applied to the data before calling Model_Language::add(), which effectively adds the record to the database.

This means that most controller actions will need an instance of a service class, and most methods in the service class will need an instance of a model class.

I used to do it like this (example of a Service class)

class Service_Language
{
    public function add()
    {
        $languageModel = new Model_Language;

        // perform some business logic and add record
    }

    public function edit()
    {
        $languageModel = new Model_Language;

        // perform some business logic and edit record
    }

    public function delete()
    {
        $languageModel = new Model_Language;

        // perform some business logic and delete record
    }
}

Not only does it become cumbersome, in more complex applications where your controller actions call multiple Service methods, there’s going to be multiple instances of the same Model class, which is just unnecessary.

A colleague told me to look into two options:

  • keep a Model instance in a property of the Service
  • keep a Model instance in Zend_Registry

I think the best solution would be the first option. The reason being that Zend_Registry acts as a global container. We don’t want our Model instances to be available in our Controller actions, it’s bad architecture. What are your opinions on this?

The first option could be implemented as follows:

class Service_Language
{

    protected $_model = null;

    function setModel()
    {
        $this->_model = new Model_Language();
    }

    function getModel()
    {
        if($this->_model == null)
        {
            $this->setModel();
        }

        return $this->_model;
    }
    public function add()
    {
        $languageModel = $this->getModel();

        // perform some business logic and add
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:21:07+00:00Added an answer on June 14, 2026 at 7:21 am

    From your explanation it sounds like your services classes require tightly coupled models.

    In which case I don’t think public a public getter/setter for your model is necessary – would there ever realistically be a situation where you would need to set another model for the service?

    In which case, assigning the model to a property makes sense – why not do this in the constructor?

    class Service_Language
    {
        protected $_model = null;
    
        public function __construct()
        {
            $this->_model = new Model_Language();
        }
    
        public function add()
        {
            // perform some business logic and add
            $this->_model->add($data);
        }
    
        public function edit()
        {
            // perform some business logic and add
            $this->_model->edit($data);
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y’all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.