0

我开始使用 Cakephp 2.6.4 的个人足球比赛网络项目。(我也是从 CakePHP 开始的)。我实现了 CakeDC 搜索插件,它使用输入框正确执行搜索。现在,我想添加一个过滤器以根据所选组显示团队。我从https://github.com/CakeDC/search阅读了插件的文档,但我没有设法在选择框中加载组。在这里我给你一张图片截图,希望你能帮助我。谢谢

//这里是EquiposController.php的代码

<?php
App::uses('AppController', 'Controller');
/**
 * Equipos Controller
 *
 * @property Equipo $Equipo
 * @property PaginatorComponent $Paginator
 */
class EquiposController extends AppController {

/**
 * Components
 *
 * @var array
 */     
        public $components = array('Paginator', 'Session', 'Search.Prg');
        public $helpers = array('Paginator', 'Session');


/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Equipo->recursive = 0;
        $this->Prg->commonProcess(null, array(
                    'paramType' => 'querystring'
                ));
            $this->Paginator->settings = array(
                'Equipo' => array(
                    'paramType' => 'querystring',
                    'conditions' => $this->Equipo->parseCriteria($this->Prg->parsedParams()),
                    'limit' => 5,
                    'order' => array(
                        'Equipo.nombre' => 'asc'
                    )
                )
            );
            $this->set('equipos', $this->Paginator->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if (!$this->Equipo->exists($id)) {
            throw new NotFoundException(__('Invalid equipo'));
        }
        $options = array('conditions' => array('Equipo.' . $this->Equipo->primaryKey => $id));
        $this->set('equipo', $this->Equipo->find('first', $options));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->Equipo->create();
            if ($this->Equipo->save($this->request->data)) {
                $this->Session->setFlash(__('El Equipo ha sido registrado.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The equipo could not be saved. Please, try again.'));
            }
        }
        $grupos = $this->Equipo->Grupo->find('list');
        $this->set(compact('grupos'));
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        if (!$this->Equipo->exists($id)) {
            throw new NotFoundException(__('Invalid equipo'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Equipo->save($this->request->data)) {
                $this->Session->setFlash(__('The equipo has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The equipo could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Equipo.' . $this->Equipo->primaryKey => $id));
            $this->request->data = $this->Equipo->find('first', $options);
        }
        $grupos = $this->Equipo->Grupo->find('list');
        $this->set(compact('grupos'));
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        $this->Equipo->id = $id;
        if (!$this->Equipo->exists()) {
            throw new NotFoundException(__('Invalid equipo'));
        }
        $this->request->allowMethod('post', 'delete');
        if ($this->Equipo->delete()) {
            $this->Session->setFlash(__('The equipo has been deleted.'));
        } else {
            $this->Session->setFlash(__('The equipo could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }
}



    <?php
App::uses('AppModel', 'Model');
/**
 * Equipo Model
 *
 * @property Grupo $Grupo
 * @property Jugadore $Jugadore
 */
class Equipo extends AppModel {

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'nombre';

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'nombre' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Grupo' => array(
            'className' => 'Grupo',
            'foreignKey' => 'grupo_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

        /**
 * hasOne associations
 *
 * @var array

    public $hasOne = array(
        'Entrenador' => array(
            'className' => 'Entrenador',
            'foreignKey' => 'entrenador_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );*/


/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Jugadore' => array(
            'className' => 'Jugadore',
            'foreignKey' => 'equipo_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

        public $actsAs = array(
            'Search.Searchable'
        );

        public $filterArgs  = array(
            'nombre' => array(
                'field' => 'Equipo.nombre',
                'type' => 'like'
            ),
            'grupo_id' => array(
                'type' => 'lookup',
                'field' => 'Equipo.grupo_id',
            )
        );

/**
        public function filterGrupo($data, $field = null)
        {
            if (empty($data['grupo_id']))
            {
                return array();
            }
            $grupo = '%' . $data['grupo_id'] . '%';
            return array(
                'OR' => array(
                    $this->alias . '.grupo LIKE' => $grupo,
                )
            );
        }
 * */

}

这里模型Equipo(团队)

<?php
App::uses('AppModel', 'Model');
/**
 * Equipo Model
 *
 * @property Grupo $Grupo
 * @property Jugadore $Jugadore
 */
class Equipo extends AppModel {

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'nombre';

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'nombre' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Grupo' => array(
            'className' => 'Grupo',
            'foreignKey' => 'grupo_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

        /**
 * hasOne associations
 *
 * @var array

    public $hasOne = array(
        'Entrenador' => array(
            'className' => 'Entrenador',
            'foreignKey' => 'entrenador_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );*/


/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Jugadore' => array(
            'className' => 'Jugadore',
            'foreignKey' => 'equipo_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

        public $actsAs = array(
            'Search.Searchable'
        );

        public $filterArgs  = array(
            'nombre' => array(
                'field' => 'Equipo.nombre',
                'type' => 'like'
            ),
            'grupo_id' => array(
                'type' => 'lookup',
                'field' => 'Equipo.grupo_id',
            )
        );

/**
        public function filterGrupo($data, $field = null)
        {
            if (empty($data['grupo_id']))
            {
                return array();
            }
            $grupo = '%' . $data['grupo_id'] . '%';
            return array(
                'OR' => array(
                    $this->alias . '.grupo LIKE' => $grupo,
                )
            );
        }
 * */

}

这是我的视图 Index.ctp

    <div class="equipos index">
    <h2><?php echo __('Equipos'); ?></h2>
        <?php
         //Buscador con pugin CakeDC-Search
            echo $this->Form->create();
            echo $this->Form->input('nombre');
            echo $this->Form->input('grupo_id', array('label' => 'Grupo'));
            echo $this->Form->end(__('Buscar'));
        ?>
    <table cellpadding="0" cellspacing="0">
    <thead>
    <tr>
            <th><?php echo $this->Paginator->sort('id'); ?></th>
            <th><?php echo $this->Paginator->sort('nombre'); ?></th>
            <th><?php echo $this->Paginator->sort('emblema'); ?></th>
            <th><?php echo $this->Paginator->sort('grupo_id'); ?></th>
            <th class="actions"><?php echo __('Actions'); ?></th>

    </tr>
    </thead>
    <tbody>
    <?php foreach ($equipos as $equipo): ?>
    <tr>
        <td><?php echo h($equipo['Equipo']['id']); ?>&nbsp;</td>
        <td><?php echo h($equipo['Equipo']['nombre']); ?>&nbsp;</td>
        <td><?php echo h($equipo['Equipo']['emblema']); ?>&nbsp;</td>
        <td>
            <?php echo $this->Html->link($equipo['Grupo']['nombre_grupo'], array('controller' => 'grupos', 'action' => 'view', $equipo['Grupo']['id'])); ?>
        </td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('action' => 'view', $equipo['Equipo']['id'])); ?>
            <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $equipo['Equipo']['id'])); ?>
            <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $equipo['Equipo']['id']), array('confirm' => __('Are you sure you want to delete # %s?', $equipo['Equipo']['id']))); ?>
        </td>
    </tr>
<?php endforeach; ?>
    </tbody>
    </table>
    <p>
    <?php
    echo $this->Paginator->counter(array(
        'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
    ));
    ?>  </p>
    <div class="paging">
    <?php
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
        echo $this->Paginator->numbers(array('separator' => ''));
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
    ?>
    </div>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>
        <li><?php echo $this->Html->link(__('Nuevo Equipo'), array('action' => 'add')); ?></li>
        <li><?php echo $this->Html->link(__('Listar Grupos'), array('controller' => 'grupos', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('Nuevo Grupo'), array('controller' => 'grupos', 'action' => 'add')); ?> </li>
        <li><?php echo $this->Html->link(__('Listar Jugadores'), array('controller' => 'jugadores', 'action' => 'index')); ?> </li>
        <li><?php echo $this->Html->link(__('Nuevo Jugador'), array('controller' => 'jugadores', 'action' => 'add')); ?> </li>
    </ul>
</div>
4

0 回答 0