在cakePHP中传递控制器内的数组

时间:2011-06-11 17:21:48

标签: cakephp

在我的控制器中我有2个动作,例如

action1() {

//code

SomeArray=();

//code

}

如何将所有SomeArray数据传递给action2?

  1. 我试图在我的班级中创建一个公共数组变量并传递它,但没有运气。

  2. 我试图将其作为参数传递给action2 ......

  3. 例如在action1,$ this-> action2(SomeArray)和action2($ param)中再没有运气。

    function doExam($id = null) {
    
    if (!$id) {
        $this->Session->setFlash(__('Invalid exam', true));
        $this->redirect(array('action' => 'index'));
    }
    
    $this->Exam->recursive=1;
    
    
    $conditions_question = array('Question.exam_id' => $id);
    $questions = $this->Exam->Question->find('all',array('conditions' => $conditions_question));
    
    foreach ($questions as $question) {
        **$this->questionsByExam[]** = $question['Question']['qst'];
    }
    //OK PASSED
    echo debug($this->questionsByExam);
    
    //OK $exam_id  
    $this->exam_id = $id;
    }
    

    我有另一个动作validate_answer,我想在这里传递questionsByExam

    任何帮助?

    提前致谢

1 个答案:

答案 0 :(得分:0)

  

我试图在我的班级中创建一个公共数组变量并传递它,但没有运气。

你能为此展示代码吗?它应该可以作为类变量工作......

E.g:

class FooController extends AppController {

    var $someArray = array();

    function doExam() {

        // Populate the array here
        $this->someArray = array(1,2,3); 

    }

    function bar() {

        // Use it here, no need to pass it as an argument
        print_r($this->someArray);

    }

}