Zend DB Update未更新

时间:2009-12-03 13:35:10

标签: zend-framework zend-db-table

我不知道为什么这个特定的代码根本没有更新,尽管在其他地方成功地使用了这种编码方式。没有异常被抛出。

CREATE TABLE `accounts` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `accounts_username` varchar(60) DEFAULT NULL,
  `accounts_fullname` varchar(600) DEFAULT NULL,
  `accounts_email` varchar(600) DEFAULT NULL,
  `accounts_password` varchar(600) DEFAULT NULL,
  `accounts_status` varchar(600) DEFAULT NULL,
  `accounts_roles` varchar(600) DEFAULT NULL,
  `accounts_comments` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

在我的表单中,我有代码

public function __construct($options = null)
    {
        parent::__construct($options);

        $optionsrole = array(
                'guest' => 'guest',
                'user' => 'user',
                'writer' => 'writer',
                'admin' => 'admin'              
                );

        $optionsstatus = array(
                'active' => 'active',
                'pending' => 'pending'              
                );

        $this->setName('account');
        $id = new Zend_Form_Element_Hidden('id');
        $username = new Zend_Form_Element_Text('accounts_username');
        $username->setLabel('Username')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $fullname = new Zend_Form_Element_Text('accounts_fullname');
        $fullname->setLabel('Fullname')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $email = new Zend_Form_Element_Text('accounts_email');
        $email->setLabel('Email')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addValidator(new Zend_Validate_EmailAddress());
        $password = new Zend_Form_Element_Password('accounts_password');
        $password->setLabel('Password')
                    ->setRequired(true)
                    ->addFilter('StripTags')
                    ->addFilter('StringTrim')
                    ->addValidator('NotEmpty');
        $status = new Zend_Form_Element_Select('accounts_status');
        $status->setLabel('Status')
                ->setRequired(true)
                //->addMultiOptions(array('Active','Pending'));
                ->addMultiOptions($optionsstatus);

        $role = new Zend_Form_Element_Select('accounts_roles');
        $role->setLabel('Role')
                ->setRequired(true)
                //->addMultiOptions(array('guest','user','writer','admin'));
                ->addMultiOptions($optionsrole);

        $comments = new Zend_Form_Element_Textarea('accounts_comments');
        $comments->setLabel('Comments')
                //->setAttrib('size',200)
                ->setAttrib('rows',20)
                ->setAttrib('cols',50);             

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
        $this->addElements(array($id, $username, $fullname,$email,
        $password,$status,$role,$comments,$submit));
    } 

在我的帐户控制器中

public function editAction()
    {
        $this->view->title = "Edit User Account";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Form_Account();
        $form->submit->setLabel('Save');
        $accounts = new Model_DbTable_Account();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                /*
                if($accounts->checkUnique($formData['accounts_username'])){
                    $this->view->errorMessage = "Username already taken. Please choose another one.";
                    return; 
                }
                */
                $id = (int)$form->getValue('id');
                $username = $form->getValue('accounts_username');
                $fullname = $form->getValue('accounts_fullname');
                $email = $form->getValue('accounts_email');
                $password = $form->getValue('accounts_password');
                $status = $form->getValue('accounts_status');
                $roles = $form->getValue('accounts_roles');
                $comments = $form->getValue('accounts_comments');
                //$accounts = new Model_DbTable_Account();
                $accounts->updateaccount($username, $fullname,$email,
                $password,$status,$roles,$comments);
                $this->_redirect('/account');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $account = new Model_DbTable_Account();
                $form->populate($account->getaccount($id));
            }
        }
    }

在我的模特中

public function updateaccount($id,$username, $fullname, $email,
                $password,$status,$roles,$comments)
    {
        $data = array(
                'accounts_username' => $username,
                'accounts_fullname' => $fullname,
                'accounts_email' => $email,
                'accounts_password' => md5($password),              
                'accounts_status' => $status,
                'accounts_roles' => $roles,             
                'accounts_comments' => $comments,
        );
        $this->update($data, 'id = '. (int)$id);
    }

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我确实发现了问题。我没有通过$ id标准,因此没有更新记录。

$accounts->updateaccount($username, $fullname,$email,
                        $password,$status,$roles,$comments);

应该改为阅读

$accounts->updateaccount($id,$username, $fullname,$email,
                        $password,$status,$roles,$comments);