[英]retrieve data from a related table in a loop that is recovering other data from another table
I'm new with cakephp .. sorry, if my question seems basic.. I have models: user, post, comment, all related and need to show comments on the post view .... but also want to show the username and I can't access this field of the user model. 我是cakephp的新手..对不起,如果我的问题似乎很基本。.我有以下模型:用户,帖子,评论,所有相关内容,需要在帖子视图上显示评论....但也想显示用户名和我无法访问用户模型的此字段。 I have user_id and post_id in comment.php model and user_id in post.php model, but inside the loop how can i access the username field of the user model??
我在comment.php模型中有user_id和post_id,在post.php模型中有user_id,但是在循环内部如何访问用户模型的用户名字段?
I have this on ... posts / view.ctp 我在... posts / view.ctp上有这个
<?php foreach ($post['Comment'] as $comment): ?>
<tr<?php echo $class;?>>
// this does not work
// echo $comment['user_id']['username'];
<td><?php echo $comment['user_id']; ?>
</td>
</tr>
<tr>
<td><?php echo $comment['created'];?></td>
<td></td>
</tr>
<tr>
<td><?php echo $comment['content'];?></td>
</tr>
<?php endforeach; ?>
</table>
then how could I fix it?? 那我该怎么解决呢? thanks...
谢谢...
solved the problem .... the most common way of trial and error .. how to fix it was .. 解决了问题..最常见的尝试和错误方式..如何解决。
$this->Post->recursive = 2; //in posts_controller.php
and then ... 接着 ...
<?php foreach ($post['Comment'] as $comment): ?>
<tr<?php echo $class;?>>
<!-- this is the way to access -->
<td><?php echo $comment['User']['username']; ?></td>
.......
Did you set auth in your any of you controller.If not,add it in your controllers and try using 您是否在您的任何控制器中设置了auth。否则,请将其添加到控制器中并尝试使用
$this->Auth->user('username');
which will give you current user username any where in you controller.You can use this in your view like this 它将在您的控制器中的任何位置为您提供当前用户的用户名。您可以在视图中像这样使用它
$this->Session->read('Auth.User.username')
$ this-> Session-> read('Auth.User.username')
//returns particular field value by this you can get everything from you User table in any acontroller Find out more about Aut component here //由此返回特定的字段值,您可以从任何控制器中获得用户表中的所有内容在此处了解有关Aut组件的更多信息
I can make my answer better if you can post how you coded your controller....and pass exact errors that your peroject interrupts.. 如果您可以发布控制器的编码方式,并传递peroject中断的确切错误,我可以使我的答案更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.