No właśnie z tą zmienną mam problem!
Poza tym właśnie korzystam z książki Pro Zend Framework Techniques. I stwierdziłem, że coś jest namieszane.
Tak wyglądają tabele do których się odwołuje.
http://imageshack.us/photo/my-images/198/skrin1.png/http://imageshack.us/photo/my-images/42/skrin2.png/A tak wygląda kod który pobiera zapisuje... z bazy danych.
<?php
class Model_Page extends Zend_Db_Table_Abstract
{
/**
* The default table name
*/
protected $_name = 'pages';
protected
$_dependentTables = array('Model_ContentNode'); protected
$_referenceMap = array('Page' => array('columns' => array('parent_id') , 'refTableClass' => 'Model_Page' , 'refColumns' => array('id') , 'onDelete' => self::CASCADE , 'onUpdate' => self::RESTRICT)); public function createPage ($name, $namespace, $parentId = 0)
{
//create the new page
$row = $this->createRow();
$row->name = $name;
$row->namespace = $namespace;
$row->parent_id = $parentId;
$row->date_created = time(); $row->save();
// now fetch the id of the row you just created and return it
$id = $this->_db->lastInsertId();
return $id;
}
public function updatePage ($id, $data)
{
// find the page
$row = $this->find($id)->current();
if ($row) {
// clear any cache records which are tagged to this page
$cache = Zend_Registry::get('cache');
$tag = 'page_' . $id;
$cache->clean(Zend_Cache
::CLEANING_MODE_MATCHING_TAG, array($tag)); // update each of the columns that are stored in the pages table
$row->name = $data['name'];
$row->parent_id = $data['parent_id'];
$row->save();
// unset each of the fields that are set in the pages table
unset($data['parent_id']); // set each of the other fields in the content nodes table
$mdlContentNode = new Model_ContentNode();
foreach ($data as $key => $value) {
$mdlContentNode->setNode($id, $key, $value);
}
}
} else {
throw new Zend_Exception('Could not open page to update!');
}
}
public function deletePage ($id)
{
// find the row that matches the id
$row = $this->find($id)->current();
if ($row) {
$row->delete();
return true;
} else {
throw new Zend_Exception("Delete function failed; could not find page!");
}
}
public function getRecentPages ($count = 10, $namespace = 'page')
{
$select = $this->select();
$select->order = 'date_created DESC';
$select->where('namespace = ?', $namespace);
$select->limit($count);
$results = $this->fetchAll($select);
if ($results->count() > 0) {
//cycle through the results, opening each page
foreach ($results as $result) {
$pages[$result->id] = new CMS_Content_Item_Page($result->id);
}
return $pages;
} else {
return null;
}
}
}
<?php
class Model_ContentNode extends Zend_Db_Table_Abstract {
/**
* The default table name
*/
protected $_name = 'content_nodes';
protected
$_referenceMap = array( 'columns' => array('page_id') , 'refTableClass' => 'Model_Page' ,
'refColumns' => array('id') , 'onDelete' => self::CASCADE ,
'onUpdate' => self::RESTRICT)
);
public function setNode ($pageId, $node, $value)
{
// fetch the row if it exists
$select = $this->select();
$select->where("page_id = ?", $pageId);
$select->where("node = ?", $node);
$row = $this->fetchRow($select);
//if it does not then create it
if (! $row) {
$row = $this->createRow();
$row->page_id = $pageId;
$row->node = $node;
}
//set the content
$row->content = $value;
$row->save();
}
}
?>
<?php
class CMS_Content_Item_Page extends CMS_Content_Item_Abstract
{
public $id;
public $name;
public $headline;
public $image;
public $description;
public $content;
}
?>
<?php
abstract class CMS_Content_Item_Abstract
{
public $id;
public $name;
public $parent_id = 0;
protected $_namespace = 'page';
protected $_pageModel;
const NO_SETTER = 'setter method does not exist';
public function __construct ($page = null)
{
$this->_pageModel = new Model_Page();
if (null != $page) {
$this->loadPageObject($page);
}
}
public function loadPageObject ($page)
{
if (is_object($page) && $page instanceof Zend_Db_Table_Row
) { $row = $page;
$this->id = $row->id;
} else {
$row = $this->_getInnerRow();
}
if ($row) {
if ($row->namespace != $this->_namespace) {
throw new Zend_Exception('Unable to cast page type:' . $row->namespace . ' to type:' . $this->_namespace);
}
$this->name = $row->name;
$this->parent_id = $row->parent_id;
$contentNode = new Model_ContentNode();
$nodes = $row->findDependentRowset($contentNode);
if ($nodes) {
$properties = $this->_getProperties();
foreach ($nodes as $node) {
$key = $node['node'];
// try to call the setter method
$value = $this->_callSetterMethod($key, $nodes);
if ($value === self::NO_SETTER) {
$value = $node['content'];
}
$this->$key = $value;
}
}
}
} else {
throw new Zend_Exception("Unable to load content item");
}
}
protected function _getInnerRow ($id = null)
{
if ($id == null) {
$id = $this->id;
}
return $this->_pageModel->find($id)->current();
}
protected function _getProperties ()
{
$propertyArray = array(); $class = new Zend_Reflection_Class($this);
$properties = $class->getProperties();
foreach ($properties as $property) {
if ($property->isPublic()) {
$propertyArray[] = $property->getName();
}
}
return $propertyArray;
}
protected function _callSetterMethod ($property, $data)
{
//create the method name
$method = Zend_Filter::filterStatic($property, 'Word_UnderscoreToCamelCase');
$methodName = '_set' . $method;
if (method_exists($this, $methodName)) {
return $this->$methodName($data);
} else {
return self::NO_SETTER;
}
}
public function toArray ()
{
$properties = $this->_getProperties();
foreach ($properties as $property) {
$array[$property] = $this->$property;
}
return $array;
}
public function save ()
{
$this->_update();
} else {
$this->_insert();
}
}
protected function _insert ()
{
$pageId = $this->_pageModel->createPage($this->name, $this->_namespace, $this->parent_id);
$this->id = $pageId;
$this->_update();
}
protected function _update ()
{
$data = $this->toArray();
$this->_pageModel->updatePage($this->id, $data);
}
public function delete ()
{
$this->_pageModel->deletePage($this->id);
} else {
throw new Zend_Exception('Unable to delete item; the item is empty!');
}
}
}
?>