使用cakephp删除数据库行

时间:2017-09-20 06:41:59

标签: cakephp cakephp-2.0

我目前正在制作一份简报,让用户订阅和取消订阅。订阅是成功的,但不是取消订阅。你可以帮帮我们吗,这是我的代码......

控制器

class AddsController extends AppController {
    public $helpers = array('Html','Form','Session','Flash');
    public $components=array('Session');

    public function index() {

        if($this->request->is('post')) //posting to db
        {
            $this->Add->create();
            $this->Add->save($this->request->data);
            $this->Flash->set('The user has been saved.', array('element' => 'success'));
            return $this->redirect('index'); //redirects to index.ctp
        }
    }

    /*public function delete($email = null) {
        if($this->request->is('post')) {
            $this->Add->exists($email) {
                $this->Add->delete($email);
                $this->Session->Flash('Unsubscribed');
                return $this->redirect('index');
            }
        }

    function delete(){ //delete html
        $email = $this->request->data['email'];
        if($this->Add->exists($email)){
            $this->Add->delete($email);
            $this->Session->Flash('Unsubscribed');
            //$this->Flash->set('Unsubscribed', array('element' => 'danger'));
            return $this->redirect('index');
        }
    }   */
    function delete() {
    $email = $this->request->data['email'];
    if($this->Add->exists($email)){
        $this->Add->delete($email);
        $this->Session->Flash('Unsubscribed');
        //$this->Flash->set('Unsubscribed', array('element' => 'danger'));
        return $this->redirect('index');
    }
}

}

查看

<?php echo "<section>";
echo $this->Form->create('Add'); //<form > 'modelname'
echo "<label>E-mail:</label>";
echo $this->Form->input('email',array('label'=>false , 'placeholder'=>'your email')); //email input
echo $this->Form->submit('Subscribe'); //subscrib button
echo $this->Form->end();//</form>
echo "</section>";

echo "<section>";
echo $this->Form->delete('Add',array('action'=>'delete')); //<form > 
echo "<label>E-mail:</label>";
echo $this->Form->input('email',array('label'=>false , 'placeholder'=>'your email')); //email input
echo $this->Form->submit('Unsubscribe',array('action'=>'index'),array('style'=>'background:red'));
echo $this->Form->end();//</form>
echo "</section>";

我的观点只是“将数据输入数据库并从数据库中删除现有数据” 的模型

<?php

App::uses('AppModel' , 'Model');
class Add extends AppModel
{
    public $name = "Add";
    public $useTable = 'request';
    public $primaryket = 'id';
    public $useDbConfig = 'default';
}

db tablename:request;只是删除行 提前谢谢!!!

2 个答案:

答案 0 :(得分:1)

尝试使用以下方法(传递用户的唯一ID)希望它能正常工作:

$user = $this-> Add->findByEmail($email);
$this-> Add->delete($user['User']['id'], false);

答案 1 :(得分:0)

替代方式

 $userId = $this->User->field('id', array('User.email' => $email));
 if($userId) {
  $this->User->id = $userId;
  $this->User->delete();
  $this->log('User of '.$email.' record deleted successfully');
  } else {
   $this->log('There is no record present of particular email = '. $email); 
  }