2011-12-27 92 views
0

我用两个发现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 '&nbsp;'.$comments['Comment']['name'].' &nbsp;&nbsp;'.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 ; 

回答

0

尝试从此改变你的第一个发现:

$article = $this->Article->Comment->find(

要这样:

$article = $this->Article->find(... 

(注意缺乏第二个 “注释”)

另外 - 没有理由需要编写2个单独的查询 - 这是CakePHP可以轻松处理的Containable Behavior


NEXT FIX变化(从答案的分组部分作出后):

做你的$文章变量的print_r。我想你会看到,它应该是:

$article[0]['Article'] 

,而不是

$article['Article'] 
+0

如果不改变它..同样的消息我看到 – user1080247 2011-12-27 15:21:22

+0

@ user108047实在不行 - 什么是SQL的物品─>找到正在产生? '<?php echo $ this-> element('sql_dump'); (在您的视图中) – Dave 2011-12-27 15:24:16

+0

@ user108047 - (在我的回答中添加了“编辑”) – Dave 2011-12-27 15:29:29

-1

的嘿嘿不要使用以下:

<?php echo $article['Article']['title']; ?> 

尝试

<?php echo $article['title']; ?> 

我不知道为什么会发生,但它有w orked我在其他场合...

同样在征求意见foreach语句我注意到你有

<?php foreach($comment as $comments){ ?> 

这一点我不认为会正确显示您的意见。你应该有

<?php foreach($comments as $comment){ ?> 

而在你的控制这一点,使用

$comments = $this->Article->Comment->find('all', ... 

我创建一个类似的应用程序,至少这是我如何处理一切。

如果您需要,我会回复或通过电子邮件发送给我的文章控制器,评论控制器和相应的视图以供您审阅。

感谢,

+0

当使用<?php echo $ article ['title']; ?> isee通知(8):未定义的索引:标题[APP \ views \ articles \ view.ctp,第18行] – user1080247 2011-12-27 17:08:15

+0

此答案有多个错误。 1)当你做一个find('all')时,它不会返回一个单独的项目,就像你试图让他在第一部分中使用一样。 2)他的foreach是正确的,你的错(见他的控制器变量)等等等等 – Dave 2011-12-27 17:15:09