从cakephp删除多条记录

时间:2014-03-18 07:28:34

标签: cakephp

我是cakephp的新手。我想从复选框中删除多个记录,但我不知道要使用它。下面是我发布的代码但不是复选框值。

<?php  echo $this->Form-enter code here>create(array('action' => 'deleteall'));?>
<?php echo $this->Html->Link('Delete',array('action' => 'deleteall'),array('confirm' => 'Are you sure you want to delete selected record?'));?>
<div class="row">
<?php echo $this->Session->flash();?>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">Subjects</div>
                <div class="table-responsive">
                    <table class="table">
                        <tr>
                            <th><?php echo $this->Form->input('checkbox', array('type' => 'checkbox','name'=>'selectAll','label'=>false,'id'=>'selectAll','hiddenField'=>false));?></td>
                            <th>S.No.</td>
                            <th>Subject Name</td>
                            <th>Action</td>
                            <th>Question Bank Account</td>
                        </tr>
                        <?php foreach ($subject as $post): ?>
                        <tr>
                            <td><?php echo $this->Form->checkbox('subject',array('value' => $post['Subject']['subject_id'],'name' => "data['Subject']['subject_id'][]",));?></td>
                            <td><?php echo $post['Subject']['subject_id']; ?></td>
                            <td><?php echo $post['Subject']['subject_name']; ?></td>
                            <td><a data-toggle="modal" data-href="<?php echo $this->Html->url(array('action'=>'edit', $post['Subject']['subject_id']));?>"><span class="glyphicon glyphicon-pencil"></span> Edit</a></td>
                            <td><?php echo $post['Subject']['subject_id']; ?></td>
                        </tr>
                        <?php endforeach; ?>
                        <?php unset($post); ?>
                        </table>
                </div>

            </div>
        </div>
    </div>
    <?php

echo $this->Form->end();?>

2 个答案:

答案 0 :(得分:0)

在视图中使用subjects_to_delete[]作为变量名称,这将生成一个包含服务器上ID的数组。

<td><?php echo $this->Form->checkbox('subjects_to_delete[]',array('value' => $post['Subject']['subject_id'],'name' => "data['Subject']['subject_id'][]",));?></td>

然后您可以使用deleteAll in CakePHP

$subjectsToDelete = $this->request->data['subjects_to_delete'];
$this->Subject->deleteAll(array('id' => $subjectsToDelete));

答案 1 :(得分:0)

Use index.ctp this code
<td><?php echo $this->Form->checkbox(false,array('value' => $post['Subject']['subject_id'],'name'=>'data[Subject][subject_id][]'));?></td>

Add code in controller
public function deleteall()
    {
        if ($this->request->is('post'))
        {
            foreach($this->data['Subject']['subject_id'] as $key => $value)
            {
                $this->Subject->delete($value);
            }
            $this->Session->setFlash('Your Subject has been deleted.'));
        }        
        $this->redirect(array('action' => 'index'));
    }