1
votes

How to use relations with data mappers? The example in the quick start is missing a little info how would I work with a datamapper if I am working with relational data.. For example the

(table)User (table)Articles (table)Tags

User -one_to_many- Articles -many_to_many- Tags

If I get one user through the data mapper what would be the best way to get the related articles and tags? Should they be defined in the model too? Any example or best practice?

1

1 Answers

1
votes

If you have a class for each of these "Article", "User" and "Tag" then you can certainly add extra methods to your classes. For example you might have:


class Article {
  public function getTags() {
    // Talk to your DAO object or table data gateway or whatever
    // and instantiate them and return an array of Tag objects 
  }
}

class User {
  public function getArticles() { }
}

Or you can implement them in static gateway classes like:


class Article_Gateway {
  public static function fetchByTag( $tagName ) { }

  public static function fetchByUser( $userName ) { }
}

As for how to get actually get the data rows to use in your models well you can use the relationships features of Zend_Db_Table to do that ( http://framework.zend.com/manual/en/zend.db.table.relationships.html )

eg:


$accountsTable = new Accounts();
$accountsRowset = $accountsTable->find(1234);
$user1234 = $accountsRowset->current();

$bugsReportedByUser = $user1234->findDependentRowset('Bugs');