CakePHP Foreach不起作用

时间:2016-02-22 10:50:10

标签: php arrays cakephp foreach controller

我是CakePHP的新手,我现在正试图获取表格中的所有行"流派"和"主题"填写一些复选框,但是当我尝试foreach时,我总是得到以下错误:

  

通知(8):未定义的变量:类型[APP / View / Pages / register.ctp,第88行]

     

警告(2):为foreach()提供的参数无效[APP / View / Pages / register.ctp,第88行]

     

注意(8):未定义的变量:主题[APP / View / Pages / register.ctp,第98行]

     

警告(2):为foreach()提供的参数无效[APP / View / Pages / register.ctp,第98行]

然而,我设置了#genies'和'主题'在我的GenresController和ThemesController但我不断收到此错误。这是我的代码:

register.ctp

<div id='stream'>
            <?php
            foreach ($genres as $genre):
                echo $this->Form->checkbox('Genre.0.libelle', array(
                    'value' => $genre['Genre']['id'],
                    'name' => $genre['Genre']['libelle']
                ));
            endforeach;
            ?>
        </div>
        <div id='webserie'>
            <?php
            foreach ($themes as $theme):
                echo $this->Form->checkbox('Theme.0.libelle', array(
                    'value' => $theme['Theme']['id'],
                    'name' => $theme['Theme']['libelle']
                ));
            endforeach;
            ?>
        </div>

GenresController:

App::uses('AppController', 'Controller');

/**
 * CakePHP GenresController
 * @author Sen
 */
class GenresController extends AppController {

    public function index() {

        $this->set('genres', $this->Genre->find('all'));
    }

}

ThemesController:

App::uses('AppController', 'Controller');

/**
 * CakePHP ThemesController
 * @author Sen
 */
class ThemesController extends AppController {

    public function index($id) {
        $this->set('themes', $this->Theme->find('all'));
    }

}

我试图将数组设置为$ genres,但它表示我的GenresController索引没有定义:

$genres = array();
$genres[0]['libelle'] = "Lamb";
$genres[1]['libelle'] = "Ham";

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

App::uses('AppController', 'Controller');

class PagesController extends AppController {
     public $uses = Array('Genre');

     public function register() {
         $this->set('genres', $this->Genre->find('all'));
     }
}

添加以上代码以在页面控制器中注册操作,不要忘记使用模态

答案 1 :(得分:0)

class GenresController extends AppController {
    public $uses = array('Genre','Theme');

    public function index() {
         $this->render('register');
         $this->set('genres', $this->Genre->find('all'));
         $this->set('themes', $this->Theme->find('all'));
    }

}