简体   繁体   English

Zend 框架关系数据库

[英]Zend Framework Relational Database

I'm trying to figure out how to program referencial mapping with Zend Framework 1.11.x.我试图弄清楚如何使用 Zend Framework 1.11.x 编写参考映射。 I have 4 tables: person, cities, states, and countries.我有 4 张表:人、城市、州和国家。 Each person has a city_id, state_id, and country_id associated with each row.每个人都有与每一行关联的 city_id、state_id 和 country_id。

I can currently display all the data using fetchAll, but City, State, and Country all show up at numbers rather than their corresponding names.我目前可以使用 fetchAll 显示所有数据,但 City、State 和 Country 都显示为数字而不是相应的名称。 I've tried searching for tutorials, google, etc, but I just can't find a good example of what I want to do.我试过搜索教程、谷歌等,但我找不到一个很好的例子来说明我想做的事情。

Here is the code in my person controller:这是我个人 controller 中的代码:

<?php

class Application_Model_DbTable_Person extends Zend_Db_Table_Abstract
{

protected $_name = 'person';
protected $_primary = 'person_id';

//get individual rows of people
public function getPerson($id)
{
    $id = (int)$id;
    $row = $this->fetchRow('person_id = ' . $id);
    if(!$row) {
        throw new Exception("Could not find row $id");
    }
    return $row->toArray();
}

//adding new persons
public function addPerson($firstName, $lastName, $cityId, $stateId, $countryId, $dob, $zip)
{
    $data = array(
        'first_name' => $firstName,
        'last_name' => $lastName,
        'city_id'   => $cityId,
        'state_id'  => $stateId,
        'country_id'    =>  $countryId,
        'dob'   =>   $dob,
        'zip'   =>  $zip,
    );
    $this->insert($data);
}

//updating an existing person
public function updatePerson($id, $firstName, $lastName, $cityId, $stateId, $countryId, $dob, $zip)
{
    $data = array(
        'first_name' => $firstName,
        'last_name' => $lastName,
        'city_id'   => $cityId,
        'state_id'  => $stateId,
        'country_id'    =>  $countryId,
        'dob'   =>   $dob,
        'zip'   =>  $zip,
    );
    $this->update($data, 'person_id = '. (int)$id);
}

//deleting a person
public function deletePerson($id)
{
    $this->delete('person_id = ' . (int)$id);
}
} 

I've also attached a picture of what I am looking at from a user interface perspective.我还附上了一张我从用户界面角度看的图片。 If I can figure this out, this concept will help me get a lot further with my development.如果我能弄清楚这一点,这个概念将帮助我进一步发展。

I want to avoid using the select();我想避免使用 select(); and focus on Zend's code, I know how to do this with basic SQL, but I'm trying to learn the whole MVC framework.并专注于 Zend 的代码,我知道如何使用基本的 SQL 来做到这一点,但我正在尝试学习整个 MVC 框架。

http://imageshack.us/photo/my-images/29/screenshot20110705at711.png/ http://imageshack.us/photo/my-images/29/screenshot20110705at711.png/

Also, here is my controller code:另外,这是我的 controller 代码:

<?php

class PersonController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
    $person = new Application_Model_DbTable_Person();
    $this->view->person = $person->fetchAll();
}

public function addAction()
{
    // action body
}

public function editAction()
{
    // action body
}

public function deleteAction()
{
    // action body
}
}

Just quickly what I have observed.很快我所观察到的。

From the code that you provided, I assume that you have defined dependent tables for City, State, and Country, ie they should indicate Person model as dependent.根据您提供的代码,我假设您已经为 City、State 和 Country 定义了依赖表,即它们应该将 Person model 指示为依赖表。

One way to solve your problem would be to define custom Row class for a person, eg解决您的问题的一种方法是为一个人定义自定义行 class ,例如

class My_Model_Table_Row_Person extends Zend_Db_Table_Row_Abstract {    

    /**
     * Get City Row for the current person row.
     *
     * @return Zend_Db_Table_Row
     */
    public function getCity() {
        return $this->findParentRow('Application_Model_DbTable_City');
    }

    // similar for State and Country
}

And in your Person model:在你的人 model 中:

class Application_Model_DbTable_Person extends Zend_Db_Table_Abstract {
   // need to add this
   protected $_rowClass = 'My_Model_Table_Row_Person';
}

With this, the fethAll method in your action would return rowsets of My_Model_Table_Row_Person instances.这样,您操作中的fethAll方法将返回 My_Model_Table_Row_Person 实例的行集。 Then for each row you could easly get parent tabtles of a person as follows:然后对于每一行,您可以轻松地获得一个人的父表格,如下所示:

$personRowset = $person->fetchAll();
foreach($personRowset as $person) {
   $cityName = $person->getCity()->name;    
}

Second way would be to define a VIEW in your database, that has the columns you want and you would define a ZF db model for the view.第二种方法是在您的数据库中定义一个视图,它具有您想要的列,您将为视图定义一个 ZF db model。 ZF does not care if it works with actual tables or views, as long as it knows primary id (in a model for the view, you would need to define what is your primary key, ie protected $_primary = array('id'); ). ZF 不关心它是否适用于实际的表或视图,只要它知道主 ID(在视图的 model 中,您需要定义主键是什么,即protected $_primary = array('id'); )。

Third way would be to perform JOIN sql query in your Person model for example:第三种方法是在您的 Person model 中执行 JOIN sql 查询,例如:

  class Application_Model_DbTable_Person extends Zend_Db_Table_Abstract {

     /**
     * Get all persons and their data 
     * 
     * @return Zend_Db_Table_Rowset
     */
    public function fetchAllPersons() {

        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false);

        $select->joinInner('CITY', 'PERSON.city_id = CITY.id', 'CITY.name as cityName')
                 ->joinInner('SATE', 'PERSON.state_id = STATE.id', 'STATE.name as stateName');
                 ->joinInner('COUNTRY', 'PERSON.country_id = COUNTRY.id', 'COUNTRY.name as countryName');


        return $this->fetchAll($select);    
    }

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM