CakePHP使用FilterComponent"致命错误:无法访问空属性"

时间:2014-05-06 03:41:59

标签: cakephp filter before-filter

我累了。我搜索了几个组件和插件来实现一个带过滤器的简单表。 CakePHP对于那些占主导地位的人来说是美丽的,但对初学者来说却很头疼。我是先生:S

在尝试了几个带有灾难性结果之后,因为没有人按照作者的说法应该有效。 我现在使用James Fairhurst的完全FilterCommponent。

我相信有人可以提供帮助。请!

我得到了致命错误:

错误:无法访问空属性

文件://lib/Cake/View/View.php

行:843

文件AppControlller:

<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {

public function isAuthorized($user) {
// Here is where we should verify the role and give access based on role 
return true;
}
var $_Filter = array();
var $components = array('Session','Filter');
var $_Form_options_datetime = array();

function beforeFilter() {
// for index actions
if($this->action == 'index') {
    // setup filter component
    $this->_Filter = $this->Filter->process($this);
    $url = $this->Filter->url;
    if(empty($url)) {
        $url = '/';
    }
    $this->set('filter_options',array('url'=>array($url)));
    // setup default datetime filter option
    $this->_Form_options_datetime = array('type'=>'date','dateFormat'=>'DMY','empty'=>'-','minYear'=>date("Y")-2,'maxYear'=>date("Y"));
    // reset filters
    if(isset($this->data['reset']) || isset($this->data['cancel'])) {
            $this->redirect(array('action'=>'index'));
    }
}
}

function process_datetime($fieldname) {
    $selected = null;
    if(isset($this->params['named'][$fieldname])) {
        $exploded = explode('-',$this->params['named'][$fieldname]);
        if(!empty($exploded)) {
            $selected = '';
            foreach($exploded as $k=>$e) {
                if(empty($e)) {
                    $selected .= (($k==0) ? '0000' : '00');
                } else {
                    $selected .= $e;
                }
                if($k!=2) {$selected.='-';}
            }
        }
    }
return $selected;
}
}

文件服务控制器:

<?php
App::uses('Component', 'Controller');

App::uses('Sanitize', 'Utility');


class ServicesController extends AppController {
public $helpers = array('Html','Form','Session','Paginator');
public $components = array('Session','Filter');
public $name = 'Services';


public function index() {

$this->set('service', array_merge($this->_Form_options_datetime, array('selected'=>$this->process_datetime('Service.fecha_reg'))));

$this->set('service', array_merge($this->_Form_options_datetime, array('selected'=>$this->process_datetime('Service.fecha_mod'))));

$this->Service->recursive = 0;

$this->set('service', $this->paginate(null, $this->_Filter));

}

文件index.ctp

<?php echo $this->$form->create('Service',array('action'=>'index','id'=>'filters')); ?> //Nuevo codigo

<table>
<thead>
<tr>
    <th><?php echo $this->Paginator->sort('id','ID', $filter_options); ?></th>
    <th><?php echo $this->Paginator->sort('nombre', 'Nombre', $filter_options);?>  </th>
    <th><?php echo $this->Paginator->sort('precio', 'Precio', $filter_options);?>  </th>
    <th><?php echo $this->Paginator->sort('duracion', 'Duración', $filter_options);?>      </th>
    <th><?php echo $this->Paginator->sort('fecha_mod', 'Modificado', $filter_options);?>  </th>
    <th>Acciones</th>       
    </tr>
<tr>
    <th><?php echo $this->form->input('nombre'); ?></th>
    <th><?php echo $this->form->input('precio'); ?></th>
    <th><?php echo $this->form->input('duracion'); ?></th>
    <th><?php echo $this->form->input('fecha_mod', $date_options_created); ?></th>
    //<th><?php echo $this->form->input('modified', $date_options_modified); ?></th>
    <th>
        <button type="submit" name="data[filter]" value="filter">Filtro</button>
        <button type="submit" name="data[reset]" value="reset">Reinicio</button>
    </th>
</tr>
</thead>
<tbody>
<?php $count=0; ?>
<?php foreach($services as $service): ?>                
<?php $count ++;?>
<?php if($count % 2): echo '<tr>'; else: echo '<tr class="zebra">' ?>
<?php endif; ?>

    <td><?php echo $service['Service']['id'] ?></td>

    <td><?php echo $this->Html->link( $service['Service']['nombre'], array('action'=>'ver', $service['Service']['id']),array('escape' => false) );?></td>

    <td><?php echo $service['Service']['precio'] ?></td>
    <td><?php echo $service['Service']['duracion'] ?></td>
    <td><?php echo $service['Service']['fecha_mod']; ?></td>
    <td>

    <?php echo $this->Html->image('ver-24px.png', array('title' => 'Ver registro','alt' => 'Ver registro', 'border' => '0','url'=>array('action' => 'ver',$service['Service']['id']))); ?>
    <?php echo $this->Html->image('editar-24px.png', array('title' => 'Editar registro','alt' => 'Editar registro', 'border' => '0','url'=>array('action' => 'editar',$service['Service']['id']))); ?>      

    <?php 
    echo $this->Form->postLink(
        $this->Html->image('/app/img/borrar-24px.png', array(
            'alt' => 'Borrar registro',
            'title' => 'Borrar registro', 
            'border' => '0')),          
            array('action' => 'eliminar',$service['Service']['id']),
            array('escape' => false,'confirm' => '¿Seguro que quieres elminarlo?')); 
    ?>  

    </td>
</tr>

<?php endforeach; ?>
<?php unset($service); ?>
</tbody>
</table>

1 个答案:

答案 0 :(得分:0)

嗨,如何实施此过滤器,但客户端

我的意思是JavaScript + jQuery + DataTablesjQuery DataTables是一个非常简单的插件,可让您以最简单的方式制作过滤器。

您可以拥有不同类型的数据源(HTML表,JSON对象,甚至可以使用Ajax调用,这意味着您可以在CakePHP Controller中执行操作以检索JSON格式的对象列表)。

我做了一个视频,其中有一个关于如何从Ajax调用中调用CakePHP操作的示例:https://www.youtube.com/watch?v=lV6EFXiljBw&index=7&list=PLSUqL2t8VZCqPhogGoP85QcrvmTgW2_VK

我希望你能找到有用的这些信息。 问候。