我用兩個發現statment..the第一獲取文章和第二獲取與此文章的相關評論...當我用這樣的代碼有毛病這個發現statment CakePHP的1.3
我看到這個錯誤Content:
Notice (8): Undefined index: Article [APP\views\articles\view.ctp, line 27]
Created
Notice (8): Undefined index: Article [APP\views\articles\view.ctp, line 31]
Notice (8): Undefined index: Article [APP\views\articles\view.ctp, line 73]
articles_controller.php
function view($id=null) {
$article = $this->Article->find('all', array(
'conditions' => array(
'Article.id' => $id,
'Article.status' => 1
)
));
$comment = $this->Article->Comment->find('all',
array('conditions' =>
array('Comment.article_id' => $id, 'Comment.status' => 1)));
$this->set(compact('article','comment'));
if(!empty($article)){
// increment the number of items the dvd was viewed
$this->Article->save(array(
'Article' => array(
'id' => $id,
'views' => ($article['Article']['views'] + 1)
)
));
}
文章/ view.ctp
<!-- to show article title,image,content,date -->
<div class="formats view">
<h2>Title: <?php echo $article['Article']['title']; ?></h2>
<dl>
<dt>Image:</dt>
<dd> <?php
echo $html->image($h,array('height'=>'250px', 'width'=>'280px')); ?>
</dd>
<dt>Content:</dt>
<dd><?php echo strip_tags($article['Article']['content']) ; ?></dd>
<dt>Created</dt>
<dd><?php echo substr($article['Article']['created'], 0, 15); ?></dd>
<!-- to show comments related with articles -->
<?php if(!empty($article['Comment'])): ?>
<div class="related">
<h3>Comments For this Article</h3>
<table>
<thead>
</thead>
<tbody>
<?php foreach($comment as $comments): ?>
<tr>
<tr>
<th>Name,date</th>
<td><?php echo ' '.$comments['Comment']['name'].' '.substr($comments['Comment']['created'], 0, 15); ?> </td>
</tr>
<tr>
<th>title</th>
<td><?php echo $comments ['Comment']['title']; ?></td>
</tr>
<tr>
<th>content</th>
<td><?php echo $comments['Comment']['content']; ?></td>
</tr>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
這個數據庫表
CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`views` int(11) NOT NULL,
`section_id` int(11) NOT NULL,
`status` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=47 ;
如果不改變它..同樣的消息我看到 – user1080247 2011-12-27 15:21:22
@ user108047實在不行 - 什麼是SQL的物品─>找到正在產生? '<?php echo $ this-> element('sql_dump'); (在您的視圖中) – Dave 2011-12-27 15:24:16
@ user108047 - (在我的回答中添加了「編輯」) – Dave 2011-12-27 15:29:29