无法使用$ this-> set('data',从find()接收到的数组)从控制器传递数组以在Cakephp 3中进行查看

时间:2019-05-14 06:43:41

标签: cakephp-3.0

无法通过数组变量形式控制器来查看以下代码 如果在控制器上打印,它显示“未定义的变量:数据”

public function report(){
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
print_r($data); die();}

2 个答案:

答案 0 :(得分:0)

尝试像这样使用compact

public function report(){ $data=$this->Nodals->find()->toArray(); $this->set(compact('data')); };

现在在您的视图中执行dd,以检查data变量是否发送到该视图。我假设您的视图文件为report.ctp,所以在report.ctp文件中写此行

<?php
  dd($data);
?>

我希望它会有所帮助。

答案 1 :(得分:0)

如果要从以下位置打印查询结果,则在代码中未定义

$ data: $ this-> Nodals-> find()-> toArray();那么您需要将其存储在$ data变量或任何其他变量中

class Products(models.Model):
    name=models.Charfield()
    user=models.ForeignKey(User)

$results=$this->Nodals->find()->toArray();
$this->set('data',$results);

//data will be sent to the template which wrap the $result and in template you can access $reseult by using $data
print_r($results); 
die();