CakePHP foreach没有显示任何内容

时间:2014-02-28 14:28:19

标签: php cakephp-2.0

当运行我的查询时,foreach循环甚至导致HTML不显示或在屏幕上打印出来,我无法找到原因......

这是我的控制器:

public function signup_complete() {

    $weight=array(
            "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44", 
            "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",
            "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",
            "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",
            "44",  "44",  "44",  "44",  "44",  "44",  "44",  "44",  "48",  "48",
            "48",  "48",  "52",  "52",  "52",  "52",  "56",  "56",  "56",  "56",
            "60",  "60",  "60",  "60",  "60",  "60",  "60",  "67",  "67",  "67",
            "67",  "67",  "67",  "67",  "67",  "75",  "75",  "75",  "75",  "75",
            "75",  "75",  "82",  "82",  "82",  "82",  "82",  "82",  "82",  "82",
            "90",  "90",  "90",  "90",  "90",  "90",  "90",  "90",  "90",  "90",
            "100", "100", "100", "100", "100", "100", "100", "100", "100", "100",
            "110", "110", "110", "110", "110", "110", "110", "110", "110", "110",
            "110", "110", "110", "110", "110", "125", "125", "125", "125", "125",
            "125", "125", "125", "125", "125", "125", "125", "125", "125", "125",
            "125", "125", "125", "125", "125", "145", "145", "145", "145", "145",
            "145", "145", "145", "145", "145", "145", "145", "145"
    );

    $this->layout='setup';

    $userweight=$this->Auth->user('weight');

    $this->User->bindModel(array('hasOne'=> array('Strengthtable'=> array(
                'className'=>'Strengthtable',
                'foreignKey'=>false
            ))),false);

    $this->set('benchs',$this->User->find('all',array('conditions'=> array(
            'User.id'=>$this->Auth->user('id'),
            'Strengthtable.gender = User.gender',
            'Strengthtable.bench = User.benchmax',
            'Strengthtable.weight'=>$weight[$userweight]
        ))));
}

这是我的观点:

<table cellpadding="0" cellspacing="0">
    <tr>
        <th>id</th>
        <th>name</th>
        <th>username</th>
        <th>email</th>
        <th>Plan</th>
        <th>Outcome</th>
    </tr>
    <tr>
        <td><?php foreach ($benchs as $bench): echo $bench['Strengthtable']['level']; ?>&nbsp;<?php endforeach; ?></td>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
    </tr>
</table>

我知道这可能是非常丑陋和低效的,但这是我第一次使用Cake而且我完全被难倒了!

如果需要任何其他代码,请大声说出来,我会发布

1 个答案:

答案 0 :(得分:0)

如果您的查询错误,可以使用

进行调试
echo $this->element('sql_dump');
在您的视图中

查看蛋糕的确切查询。

然后,您可以将预期查询与蛋糕查询进行比较,并在控制器中修改$ this-&gt; User-&gt; find(...)语法。