views:

41

answers:

1

Hi, i have a question about Zend Framework and mainly the model part. I know there is no abstract model class and understand why. I red a lot of blog posts about it, but couldn't find any example that would clearly explain it to me. I'm building a very basic application. Lets imagine we have just three tables. company(ID, name, street, streetNo, Town), meta(ID, name, description), company2meta(ID, companyID, metaID, value, note). All types of realtionship are there (1n n1, nm). The a need also services (amf) with Value Objects. So, here is what I think:

  1. I would create three table classes under /model/dbTables/(Company.php, Meta.php, Company2Meta.php).

  2. Then in models/, I would add User.php (basic class), which should has methods such saveCompany..., deleteCompany, getCompanyMeta..., and operates above selected tables.

  3. The service class (/services/CompanyService.php), would create instance of the model in constructor and has same methods as model.

And here is my question. How should data flow and what should be done where? For example when i call getCompany in service clas i want it to return value object, which contains all info about company and about its metas. if there wouldnt be the model class a could just get row form company and rowset from mate2company and transform it to value object, and when i want to update it i need to save info about company and also all its metas (need to find new ones (insert), old (update) and delete the deleted...) So where should this be done? in model or in service? I hope you can understend what i am asking.

ps: sorry for my english...

A: 

your user model should like this:

class App_Model_User
{
    protected $_data = array(
        'id' => null,
        'username' => null,
        'companyId' => null,
        'company' => null
    );

public function setId($id)
{
    $this->_data['id'] = $id;
}

public function getId()
{
    return $this->_data['id'];
}

//Other setters getters..

public function setCompany(Cms_Model_Company $company)
{
    $this->_data['company'] = $company;
}

public function getCompany()
{
    if (null == $this->_data['company']) {
        $this->setCompany(Cms_Service_Company::find($this->_data['companyId']));
    }

    return $this->_data['company'];
}
}

Because a user and a company are another thnigs, they must be another models.

cnkt