Zend DB 更新未更新

发布于 2024-08-13 11:43:44 字数 5953 浏览 8 评论 0原文

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

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

我缺少什么吗?

I dont know why this particular bit of code is simply not updating despite using this style of coding elsewhere succesfully.No exception is being thrown.

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

In my form I have the code

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

In my accounts controller

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

In my model

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

Am i missing something?

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

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

发布评论

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

评论(1

地狱即天堂 2024-08-20 11:43:44

我确实发现了问题。我没有传递 $id creteria,因此没有更新记录。

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

它应该改为读取

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

I did find the problem.I was not passing the $id creteria and therefore no record was been updated.

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

It should have instead read

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