I'm trying to figure out how to program referencial mapping with Zend Framework 1.11.x. I have 4 tables: person, cities, states, and countries. Each person has a city_id, state_id, and country_id associated with each row.
I can currently display all the data using fetchAll, but City, State, and Country all show up at numbers rather than their corresponding names. 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:
<?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(); 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.
http://imageshack.us/photo/my-images/29/screenshot20110705at711.png/
Also, here is my controller code:
<?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
}
}