cakephp视图从数据库打印出相同的值
创建了一个查看功能,每当我点击链接查看cakephp视图从数据库打印出相同的值,cakephp,Cakephp,创建了一个查看功能,每当我点击链接查看模板时,页面顶部的url是正确的,但它会在数据库中显示相同的字段列表 田野是 accounts - id, company name, abn template - id, name, description, account_id field - id, name, field type, template_id function view(){ $accounts=$this->User->AccountsUser->fin
模板时,页面顶部的url是正确的,但它会在数据库中显示相同的字段列表
田野是
accounts - id, company name, abn
template - id, name, description, account_id
field - id, name, field type, template_id
function view(){
$accounts=$this->User->AccountsUser->find('list',
array('fields'=>array('id', 'account_id'),
'conditions' =>array('user_id' =>
$this->Auth->user('id'))));
$templates=$this->Template->find('first',
array('conditions' => array(
'Template.account_id' => $accounts)));
$fields=$this->Field->find('all',
array('conditions' => array(
'Field.template_id' => Set::extract('/Template/id', $templates))));
$this->set('template', $templates);
$this->set('account', $accounts);
$this->set('field', $fields);
}
这里是风景
<div class = "conlinks">
</br></br></br></br></br><h2>Here is your template fields</h2></br>
<?php foreach($field as $fields): ?>
<tr>
<td align='center'><?php echo $fields['Field']['name']; ?>
</tr></br>
<?php endforeach; ?>
</div>
这是您的模板字段
因此,问题在于它获取了完全相同的字段列表,而不是正确的模板id
,当它打印出字段时您应该能够自己调试它。只要一步一步地缩小缺陷
首先,在视图函数中,对以下变量进行打印,并确保每个变量都包含逻辑结果:
- 美元账户
- $templates
- $fields
如果您在那里发现了意外的结果,我将查看您传递到每个发现中的参数,并确保它们正常。您正在将$accounts作为数组传递给find条件-确保它与cake期望的格式匹配。对Set::extract('/Template/id',$templates)执行相同的操作
还要看看Cake生成的SQL
如果您还没有使用它,我强烈建议您安装Cake的调试工具包工具栏,因为它使调试变量和SQL变得更加容易
如果您执行了上述步骤,但无法解决问题,那么您至少应该能够将问题缩小到一两行代码。更新您的答案以显示是哪一行或哪两行导致了问题,并包括您正在处理的一些变量的打印。这将有助于StackOverflow上的其他人给你一个具体的答案
希望有帮助 问题是我在单击链接时没有获取参数
function view($name){
$fields = $this->Template->Field->find('list',array(
'fields'=> array('name'),
'conditions' => array(
'template_id'=> $name)));
$this->set('field', $fields);
}
那景色呢
<div class = "conlinks">
</br><h2>Here is your template fields</h2>
<?php foreach($field as $name): ?>
<tr>
<td align='center'>
<?php echo $name; ?>
</tr></br>
<?php endforeach; ?>
</br>
<?php
echo $this->Html->link('Back', '/templates/view', array('class' => 'button'));?>
</div>
这是您的模板字段
foreach($fieldas$fields):)?我不确定我需要更改什么?@als:我认为在这种情况下这实际上是正确的(虽然在语义上没有任何意义),因为他传递了包含字段的$field..^^@user14。。。你真的应该在$variable标签上下功夫。这是一个巨大的混乱。看看你的模板。它应该是单数的,因为你确实发现了(第一个)等等。包括你自己在内的任何人都无法理解这一点而不犯错误。一直改变它们也没用。账户也一样。最后但并非最不重要的一点是,如果未选中“空”,则不应使用以前的搜索结果。