2011-03-01 43 views
3

我有一個表一個Zend_Db_Table_Row錯誤創建新行

-- 
-- Table structure for table `pages` 
-- 

CREATE TABLE IF NOT EXISTS `pages` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `name` varchar(200) NOT NULL, 
    `title` varchar(200) NOT NULL DEFAULT '', 
    `text` longtext, 
    `enabled` smallint(1) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ; 

形式

class Admin_Form_Page extends Zend_Form 
{ 
    public function __construct($options = null) 
    { 
     parent::__construct($options); 
     $this->setDisableLoadDefaultDecorators(true) 
      ->setElementDecorators(array(
       'ViewHelper', 
       'Label', 
       'Errors', 
       array('HtmlTag', array('tag' => 'p')) 
      )); 

     $this->setDecorators(array(
      'FormElements', 
      'Form' 
     )); 

     // Add Elements 

     $this->addElement('hidden', 'id', array('decorators' => array('ViewHelper'))); 

     $this->addElement('text', 'name', array(
      'label' => 'Название страницы', 
      'required' => true 
     )); 

     $this->addElement('text', 'title', array(
      'label' => 'Заголовок страницы', 
     )); 

     $this->addElement('textarea', 'text', array(
      'label' => 'Текст страницы' 
     )); 

     $this->addElement('checkbox', 'enabled', array(
      'label' => 'Страницы включена' 
     )); 

     $this->addElement('submit', 'save', array(
      'label' => 'Сохранить', 
      'decorators' => array(
       'ViewHelper', 
       array('HtmlTag', array('tag' => 'p')) 
      ) 
     )); 
    } 
} 

和模型

class Default_Model_Pages extends Zend_Db_Table 
{ 
    protected $_name = 'pages'; 

    protected $_cols = array('id', 'name', 'title', 'text', 'enabled'); 

    protected $_primary = 'id'; 

    public function getPagesList($enabled = true) 
    { 
     $sql = $this->select(); 
     if (true === $enabled) $sql->where('enabled = 1'); 
     return $this->fetchAll($sql)->toArray(); 
    } 
} 

然後我不會用一個Zend_Db_Table_Row

... 
$pageData = $pageForm->getValues(); 
$id = (!empty($pageData['id'])) ? (int) $pageData['id'] : null; 

$pageRow = (null === $id) 
    ? $pageTable->createRow($pageData) 
    : $pageTable->fetchRow($pageTable->select()->where('id = ?', $id))->setFromArray($pageData); 

if ($pageRow->save()) { 
    $this->view->content = '<p class="info">Save Ok</p>'; 
} else { 
    $this->view->content = '<p class="error">Save error</p>'; 
} 
創建新頁面

我有錯誤

Fatal error: Uncaught exception 'Zend_Db_Table_Row_Exception' with message 'Cannot refresh row as parent is missing' in /home/ergallm/www/gps.local/library/Zend/Db/Table/Row/Abstract.php on line 764 Zend_Db_Table_Row_Exception: Cannot refresh row as parent is missing in /home/ergallm/www/gps.local/library/Zend/Db/Table/Row/Abstract.php on line 764 Call Stack: 0.0002 657224 1. {main}() /home/ergallm/www/gps.local/public/index.php:0 0.0509 8922848 2. Zend_Application->run() /home/ergallm/www/gps.local/public/index.php:16 0.0509 8922848 3. Zend_Application_Bootstrap_Bootstrap->run() /home/ergallm/www/gps.local/library/Zend/Application.php:366 0.0509 8922984 4. Zend_Controller_Front->dispatch() /home/ergallm/www/gps.local/library/Zend/Application/Bootstrap/Bootstrap.php:97 

如果我取消設置$ pageData [ '身份證']

$pageData = $pageForm->getValues(); 
$id = (!empty($pageData['id'])) ? (int) $pageData['id'] : null; 
unset($pageData['id']); 

我有錯誤

Notice: Undefined index: id in /home/ergallm/www/gps.local/library/Zend/Db/Table/Row/Abstract.php on line 745 Call Stack: 0.0002 657224 1. {main}() /home/ergallm/www/gps.local/public/index.php:0 0.0448 8922848 2. Zend_Application->run() /home/ergallm/www/gps.local/public/index.php:16 0.0448 8922848 3. Zend_Application_Bootstrap_Bootstrap->run() /home/ergallm/www/gps.local/library/Zend/Application.php:366 0.0449 8922984 4. Zend_Controller_Front->dispatch() /home/ergallm/www/gps.local/library/Zend/Application/Bootstrap/Bootstrap.php:97 0.0467 9404896 5. Zend_Controller_Dispatcher_Standard->dispatch() /home/ergallm/www/gps.local/library/Zend/Controller/Front.php:954 0.0497 9895472 6. Zend_Controller_Action->dispatch() /home/ergallm/www/gps.local/library/Zend/Controller/Dispatcher/Standard.php:295 0.0497 9903880 7. Admin_IndexController->pagesAction() /home/ergallm/www/gps.local/library/Zend/Controller/Action.php:513 0.0643 13499456 8. Zend_Db_Table_Row_Abstract->save() /home/ergallm/www/gps.local/application/modules/admin/controllers/IndexController.php:29 0.0643 13499456 9. Zend_Db_Table_Row_Abstract->_doInsert() /home/ergallm/www/gps.local/library/Zend/Db/Table/Row/Abstract.php:438 0.0656 13504264 10. Zend_Db_Table_Row_Abstract->_refresh() /home/ergallm/www/gps.local/library/Zend/Db/Table/Row/Abstract.php:497 0.0656 13504264 11. Zend_Db_Table_Row_Abstract->_getWhereQuery() /home/ergallm/www/gps.local/library/Zend/Db/Table/Row/Abstract.php:759 

我該怎麼辦了?

+0

如果刪除表保護$ _primary = 'ID' 所有的工作很好,但爲什麼它不適用於主鍵? – ErgallM 2011-03-01 05:33:09

回答

4

我認爲這個問題是由於你自己分配了$_cols變量。當你這樣做時,Zend_Db_Table沒有正確初始化。按照Zend Framework reference指定列,你應該覆蓋describeTable()方法:

_setupMetadata()設置模式如果表名包含模式 「schema.table」;調用describeTable() 來獲取元數據信息;將default_ $ _cols數組設置爲由describeTable()報告的列 。 按 覆蓋此方法,即可 指定列

編輯:一些更多的見解。

具體而言,您的Default_Model_Pages未正確初始化,因爲Zend_Db_Table_Abstract的_setupMetadata()未執行。通常在_primary_cols變量未設置時執行。因爲在你的Default_Model_Pages你手動設置這兩個變量,_setupMetadata沒有運行。這也是爲什麼當你從你的班級中刪除protected $_primary = 'id';的原因。在這種情況下,$_primary未被設置,並且隨後在_setupPrimaryKey()方法中調用了_setupMetadata()

0

,您可以通過獲得的cols:

$table = new YourTable(); 
$cols = $table->info(Zend_Db_Table_Abstract::COLS); 

以下只是使用模型文件:

$this->info(Zend_Db_Table_Abstract::COLS)