这个发现的错误cakephp 1.3 [英] something wrong with this find statment cakephp 1.3

查看:230
本文介绍了这个发现的错误cakephp 1.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用两个查找statment ...第一次获取文章和第二个获取与这篇文章相关的评论...我看到这个错误时使用我的代码

i use two find statment..the first to fetch articles and the second to fetch comments that related with this articles...i see this error when use my code like this

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

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)
                    )
                ));
              }

articles / view.ctp

articles/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 ;


推荐答案

尝试从这里更改第一个查找:

Try changing your first find from this:

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

至此:

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

第二个)

此外 - 真的没有理由你需要编写两个单独的查询 - 这是CakePHP可以轻松处理你的可控行为

Also - there's really no reason you should need to write 2 separate queries - that's something CakePHP can handle easily for you with the Containable Behavior

NEXT FIX (根据回答的上一部分进行更改后):

NEXT FIX (after changes were made from prev part of answer):

在$文章变量上执行print_r您会看到它应该是:

Do a print_r on your $article variable. I assume you'll see that it should be:

$article[0]['Article']

而不是

$article['Article']

这篇关于这个发现的错误cakephp 1.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆