Ccortina
Ccortina

Reputation: 587

passing data to view CakePHP

This might be easy, but I have tried a few ideas but no solution at all, I'm no expert with cakePhp but I'm trying to modify a existing project with limited time.

The problem is that I have a entity called 'centro' but I'm using its model on a different controller, 'rw', but I can't get it to pass that data to the view at all; it seems it's because the variable gets erased when I use a redirect to go back to the 'index'.

I need that data to generate a google map, the method $this->set(.....) is not working.

Here's the code of the 'rw' controller.

<?php
class RwController extends AppController {
var $name = 'Rw';
// var $paginate = array(
    // 'Tip' => array(
        // 'limit' => 1,
        // 'order' => array(
            // 'tip.created' => 'desc'
        // ),
    // ),
    // 'Evento' => array(
        // 'limit' => 1,
        // 'order' => array(
            // 'evento.fecha' => 'desc'
        // ),
    // )
// );
function map() {
    $this->helpers[]='GoogleMapV3';
}
function pageForPagination($model) {
    $page = 1;
    // $chars = preg_split('/model:/', $this->params['url']['url'], -1, PREG_SPLIT_OFFSET_CAPTURE);
    // #print_r($chars);
    // if(sizeof($chars) > 1 && sizeof($chars) < 3) {
        // #echo "Belongs to ".$model.": \n";
        // #echo var_dump($chars);
    // }
    // $params = Dispatcher::parseParams(Dispatcher::uri());
    // echo "<p>".var_dump($params)."</p><br />";
    #echo $this->params['named']['model'].$model;
    #echo $this->params['named']['page'];
    $sameModel = isset($this->params['named']['model']) && $this->params['named']['model'] == $model;
    $pageInUrl = isset($this->params['named']['page']);
    if ($sameModel && $pageInUrl) {
        $page = $this->params['named']['page'];
    } else {
        #echo var_dump($this->passedArgs);
    }
    $this->passedArgs['page'] = $page;
    return $page;
}

function index() {

    $this->loadModel('User');
    $this->loadModel('Evento');
    $this->loadModel('Tip');

    $dataEvento = $this->Evento->find('all');
    $dataTip = $this->Tip->find('all');

    $page = $this->pageForPagination('Evento');
    $this->paginate['Evento'] = array(
        'contain' => false,
        'order' => array('Evento.fecha' => 'desc'),
        'limit' => 1,
        'page' => $page
    );
    $dataEvento = $this->paginate('Evento');

    $page = $this->pageForPagination('Tip');
    $this->paginate['Tip'] = array(
        'contain' => false,
        'order' => array('Tip.created' => 'desc'),
        'limit' => 1,
        'page' => $page
    );
    $dataTip = $this->paginate('Tip');

    $this->set('users', $this->User->find('all'));
    $this->set('eventos', $dataEvento);
    $this->set('tips', $dataTip);
    $this->set('rw');

    if(isset($this->params['named']['model'])) {
        if (strcmp($this->params['named']['model'], 'Evento') == 0) {
            if($this->RequestHandler->isAjax()) {
                $this->render('/elements/ajax_rw_evento_paginate');
                return;
            }
        } elseif (strcmp($this->params['named']['model'], 'Tip') == 0) {
            if($this->RequestHandler->isAjax()) {
                $this->render('/elements/ajax_rw_tip_paginate');
                return;
            }
        }
    }
}

function about($id = null) {
    $this->Rw->recursive = 0;
    $this->set('rw', $this->paginate());
}

function beforeFilter() {
        parent::beforeFilter(); 
        $this->Auth->allow(array('*'));
}

function getCentros($id = null ){
    $this->loadModel('Centro');
    $this->log('getcentros','debug');
    $this->log('el id'.$id,'debug');
    if( sizeof($id) > 1){
        $this->set('centros', $this->Centro->query("SELECT centros.id, name, latitud ,longitud 
                                                        FROM `centros`,`centrosmateriales` 
                                                            WHERE centros.id = centro_id 
                                                                AND material_id ='".$id[0]."' 
                                                                    OR material_id='".$id[1]."'"));
        $this->log('size id > 1 ','debug');                                                         
    }elseif( sizeof($id) >0) {
        if($id == 0){
            $this->set('centros', $this->Centro->find('all'));
        }else{
            $this->set('centros', $this->Centro->query("SELECT centros.id, name, latitud ,longitud 
                                                            FROM `centros`,`centrosmateriales` 
                                                                WHERE centros.id = centro_id 
                                                                    AND material_id ='".$id[0]."'"));
            }
        }
        $this->Session->write('saludos', 'Saludando');
        $this->redirect(array('action' => 'index'));
}


}   
?>

I have been thinking about ajax but I'm not sure.

Anyways, thanks in advance.

Upvotes: 0

Views: 2293

Answers (1)

pixelistik
pixelistik

Reputation: 7830

Using redirect() sends an actual HTTP redirect to the browser. This causes the browser to send another request to the server, to the URL it has been redirected to.

The data that you pass to set() is only available within that single CakePHP request cycle. All PHP data is wiped from memory afterwards. The only way to pass data to the next request is in the URL or in the session.

In your case you should rethink your design and set() all data you need in the index() action. You could possibly move the logic from getCentros() into your Centro model. Then, in your index() function, you could just do

$this->set(
    'centro',
    $this->Centro->getCentros();
);

Upvotes: 2

Related Questions