Zend_Db_Table_Row 创建新行时出错

发布于 2024-10-19 19:39:27 字数 5310 浏览 2 评论 0原文

我有一个表格

--
-- 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['id']

$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

做错了什么?

i have a table

--
-- 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 ;

form

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'))
            )
        ));
    }
}

and model

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();
    }
}

then i wont create new page with 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>';
}

i have error

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

if i unset $pageData['id']

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

i have error

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

What i do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-10-26 19:39:27

我认为问题是由于您自己分配了 $_cols 变量。当您执行此操作时,Zend_Db_Table 未正确初始化。根据 Zend Framework 参考 指定您应该覆盖描述表的列() 方法:

_setupMetadata() 如果表名包含模式则设置模式
“模式.表”;调用describeTable()
获取元数据信息;默认值
$_cols 数组到列
由describeTable() 报告。 作者:
重写这个方法,你可以
指定列

编辑:一些更多的见解。

具体来说,您的 Default_Model_Pages 未正确初始化,因为 Zend_Db_Table_Abstract 的 _setupMetadata() 未执行。通常它在未设置_primary_cols变量时执行。因为您在 Default_Model_Pages 中手动设置了这两个变量,所以 _setupMetadata 不会运行。这也是当您从类中删除 protected $_primary = 'id'; 时一切正常的原因。在这种情况下,未设置 $_primary,随后在 _setupPrimaryKey() 方法中调用 _setupMetadata()

I think that the problem is due to the fact that you assign $_cols variable yourself. When you do this, the Zend_Db_Table is not properly initialized. According to the Zend Framework reference to specify columns you should overwrite describeTable() method:

_setupMetadata() sets the schema if the table name contains the pattern
"schema.table"; calls describeTable()
to get metadata information; defaults
the $_cols array to the columns
reported by describeTable(). By
overriding this method, you can
specify the columns
.

EDIT: Some more insight.

Specifically your Default_Model_Pages is not properly initialized, because the _setupMetadata() of Zend_Db_Table_Abstract is not executed. Normally it is executed when _primary or _cols variables are not set. Because, in your Default_Model_Pages you manually set these two variables, the _setupMetadata is not run. This is also the reason why everything worked when you removed protected $_primary = 'id'; from your class. In that case $_primary was not set, and subsequently, _setupMetadata() was called within _setupPrimaryKey() method.

梦里的微风 2024-10-26 19:39:27

您可以通过以下方式获取列:

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

或者仅在模型文件中使用以下内容:

$this->info(Zend_Db_Table_Abstract::COLS)

you can get the cols via:

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

or just use below in your model file:

$this->info(Zend_Db_Table_Abstract::COLS)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文