views:

410

answers:

3

Hi there is there a way how to use Zend_Db relations for setting related objects? I am looking for something like following code:

$contentModel = new Content();    
$categoryModel = new Category();

$category = $categoryModel->createRow();
$category->setName('Name Category 4');

$content = $contentModel->createRow();
$content->setTitle('Title 4');

$content->setCategory($category);
$content->save();

this provides small library: http://code.google.com/p/zend-framework-orm/

does somebody have experience with that? Isn't there a plan for something similar in ZF ? Or is there something better for use? (I don't wnat to use doctrine ORM or something external)

thanks

A: 

http://framework.zend.com/manual/en/zend.db.table.relationships.html

explains how to establish table relationships in Zend Framework.

tharkun
Hi, thank's for answer, but ZF provides only getting related rows, not setting. I would like to use it also for setting it, as in example in the question $content->setCategory($category);$category is Zend_Db_Row
harvejs
A: 

I always override Zend_Db_Table and Zend_Db_Table_Row and use my own subclasses. In my Db_Table class I have:

protected $_rowClass = 'Db_Table_Row';

In my Db_Table_Row I have the following __get() and __set() functions:

public function __get($key)
{
 $inflector = new Zend_Filter_Word_UnderscoreToCamelCase();

 $method = 'get' . $inflector->filter($key);

 if(method_exists($this, $method)) {
  return $this->{$method}();
 }

 return parent::__get($key);
}

public function __set($key, $value)
{
 $inflector = new Zend_Filter_Word_UnderscoreToCamelCase();

 $method = 'set' . $inflector->filter($key);

 if(method_exists($this, $method))
  return $this->{$method}($value);

 return parent::__set($key, $value);
}

Bascially that just tells the class to look for methods called getFoo() and setFoo() or whatever. You could then pretty much make up your own fields as long as your write your own logic behind. In you case maybe:

public function setCategory($value)
{
     $this->category_id = $value->category_id;
}
smack0007
hi, thank's for answer, but I don't need setters only for values, or to define own functions.$content->setCategory($category);$content->save();this provides creating new row in related table and bind it on related foreign keys
harvejs
A bit off topic, but I'm investigating into `Zend_Db`. Would you happen to have some links to articles or projects that replaces the off-the-shelf `Zend_db_Table_Row`?
troelskn
+1  A: 

I designed and implemented the table-relationships code in Zend Framework.

A foreign key ($content->category in your example) contains the value of the the primary key in the parent row it references. In your example, the $category doesn't contain a primary key value yet because you haven't saved it (assuming it uses an auto-incrementing pseudokey). You can't save the $content row until you populate its foreign key, so referential integrity is satisfied:

$contentModel = new Content();                  
$categoryModel = new Category();

$category = $categoryModel->createRow();
$category->setName('Name Category 4');

$content = $contentModel->createRow();
$content->setTitle('Title 4');

// saving populates the primary key field in the Row object
$category->save();

$content->setCategory($category->category_id);
$content->save();

It would do no good to pass the Row object to setCategory() if it doesn't have the primary key populated. $content->save() will fail if it doesn't have a valid primary key value to reference.

Since you need that primary key field to be populated in any case, it's not so difficult to access the field when you call setCategory().

Bill Karwin