0

我正在使用带有 croggo 的 cakephp 框架,我有 3 个模型 Service 、 Association 和 Member 以及关系 hasmany hasbelongs

协会.php

public $hasMany = array(
        'Service' => array(
            'className' => 'Service',
            'foreignKey' => 'association_id',
            'dependent' => false,
        ),
    );

服务.php

public $belongsTo = array(
        'Association' => array(
            'className' => 'Association',
            'foreignKey' => 'association_id',
        ),
    );

public $hasMany = array(
        'Member' => array(
            'className' => 'Member',
            'foreignKey' => 'service_id',
            'dependent' => false,
        ),
    );

会员.php

public $belongsTo = array(
        'Service' => array(
            'className' => 'Service',
            'foreignKey' => 'service_id',
        ),
    );

我想在索引/成员中进行搜索与关联

我在服务和工作方面做得很好,但协会不起作用

编码 :

$conditions = array();
        //Transform POST into GET
        if(($this->request->is('post') || $this->request->is('put')) && isset($this->data['Filter'])){
            $filter_url['controller'] = $this->request->params['controller'];
            $filter_url['action'] = $this->request->params['action'];
            // We need to overwrite the page every time we change the parameters
            $filter_url['page'] = 1;

            // for each filter we will add a GET parameter for the generated url
            foreach($this->data['Filter'] as $name => $value){
                if($value){
                    // You might want to sanitize the $value here
                    // or even do a urlencode to be sure
                    $filter_url[$name] = urlencode($value);
                }
            }   
            // now that we have generated an url with GET parameters, 
            // we'll redirect to that page
            return $this->redirect($filter_url);
        } else {
            // Inspect all the named parameters to apply the filters
            foreach($this->params['named'] as $param_name => $value){
                // Don't apply the default named parameters used for pagination
                if(!in_array($param_name, array('page','sort','direction','limit'))){
                    // You may use a switch here to make special filters
                    // like "between dates", "greater than", etc
                    if($param_name == "search"){
                        $conditions= array(
                            array('Member.nom LIKE' => '%' . $value . '%'),
                            //array('Member.prenom LIKE' => '%' . $value . '%')
                        );
                    } 
                     else {
                        $conditions['Member.'.$param_name] = $value;
                    }                   
                    $this->request->data['Filter'][$param_name] = $value;
                }
            }
        }
        $this->Member->recursive = 0;
        $this->paginate = array(
            'limit' => 10,
            'conditions' => $conditions
        );
        $this->set('members', $this->paginate());

        // get the possible values for the filters and 
        // pass them to the view
        $services = $this->Member->Service->find('list');
        $associations = $this->Member->Service->Association->find('list');
        $this->set(compact('services','associations'));

        // Pass the search parameter to highlight the text
        $this->set('search', isset($this->params['named']['search']) ? $this->params['named']['search'] : "");

索引/成员是:

$base_url = array('controller' => 'members', 'action' => 'index');
        echo $this->Form->create("Filter",array('url' => $base_url, 'class' => 'filter'));
        // add a select input for each filter. It's a good idea to add a empty value and set
        // the default option to that.
        echo $this->Form->input("service_id", array('label' => 'Service', 'options' => $services, 'empty' => '-- All servicess --', 'default' => ''));
        echo $this->Form->input("Service.associaton_id", array('label' => 'Association', 'options' => $associations, 'empty' => '-- All associations --', 'default' => ''));
        // Add a basic search 
        echo $this->Form->input("search", array('label' => 'Search', 'placeholder' => "Search nom..."));

        echo $this->Form->submit("Valider");

        // To reset all the filters we only need to redirect to the base_url
        echo "<div class='submit actions'>";
        echo $this->Html->link("Reset",$base_url);
        echo "</div>";
        echo $this->Form->end();

搜索正常,服务过滤器正常,关联不正常

我有一个要过滤关联的问题

4

0 回答 0