【问题标题】:Delete Multiple Record from cakephp从 cakephp 中删除多条记录
【发布时间】:2014-03-18 07:28:33
【问题描述】:

我是 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();?>

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    在视图中使用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));
    

    【讨论】:

    • 注意(8):未定义索引:subjects_to_delete
    • 如果您的模型是 Subject,则应该是 $this-&gt;request-&gt;data['Subject']['subjects_to_delete']。你可以var_dump($this-&gt;request-&gt;data)查看具体情况
    • 您现在可能已经解决了这个问题,但我遇到了同样的问题。您不需要数据名称中的单引号,这就是 cakephp 格式化它的方式,例如 'name' =&gt; data[Subject][subject_id][] 然后在控制器中 $this-&gt;request-&gt;data[Subject][subject_id][]
    【解决方案2】:
    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'));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2013-01-30
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多