【问题标题】:something wrong with this find statment cakephp 1.3这个查找语句 cakephp 1.3 有问题
【发布时间】:2011-12-27 13:25:23
【问题描述】:

我使用两个 find statment..第一个获取文章,第二个获取与本文相关的 cmets...我在使用这样的代码时看到此错误

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 ;

【问题讨论】:

    标签: cakephp cakephp-1.3


    【解决方案1】:

    尝试从这里更改您的第一个发现:

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

    到这里:

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

    (注意第二个没有“评论”)

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


    NEXT FIX(在对答案的上一个部分进行更改后):

    对 $article 变量执行 print_r。我假设你会看到它应该是:

    $article[0]['Article']
    

    而不是

    $article['Article']
    

    【讨论】:

    • @user108047 - Article->find 生成的 SQL 是什么? &lt;?php echo $this-&gt;element('sql_dump'); ?&gt; 在你看来
    • @user108047 - (在我的答案中添加了“编辑”)
    • 当我将其更改为 $article[0]['Article'] 时,我看到了通知 (8):未定义索引:0 [APP\views\articles\view.ctp,第 18 行]跨度>
    • 使用时 isee 注意(8):未定义索引:title [APP\views\articles\view.ctp, line 18]
    • 当我使用 我看到数组 ( )
    【解决方案2】:

    嘿,而不是以下:

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

    试试

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

    我还不确定为什么会发生这种情况,但它在其他场合对我有用...

    在 cmets 的 foreach 声明中我注意到你有

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

    我认为这不会正确显示您的 cmets。你应该有

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

    为此,在您的控制器中使用

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

    我正在创建一个类似的应用程序,至少我是这样处理所有事情的。

    如果您需要,我会将我的文章控制器、cmets 控制器和相应的视图发回或通过电子邮件发送给您,供您查看。

    谢谢,

    【讨论】:

    • 使用时 isee 注意(8):未定义索引:title [APP\views\articles\view.ctp, line 18]
    • 这个答案有很多问题。 1) 当您执行 find('all') 时,它不会像您试图让他在第一部分中使用那样返回单个项目。 2)他的foreach是正确的,你的是错误的(见他的控制器变量)......等等等等。
    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多