问题
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
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
<!-- 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; ?>
this database table
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 ;
回答1:
Try changing your first find from this:
$article = $this->Article->Comment->find(
To this:
$article = $this->Article->find(...
(Notice the lack of "Comment" in the second one)
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 (after changes were made from prev part of answer):
Do a print_r on your $article variable. I assume you'll see that it should be:
$article[0]['Article']
instead of
$article['Article']
回答2:
Hey instead of the following:
<?php echo $article['Article']['title']; ?>
try
<?php echo $article['title']; ?>
I am not sure yet why that happens, but it has worked for me in other occasions...
Also on the foreach statement for comments I noticed you have
<?php foreach($comment as $comments){ ?>
Which I dont think will display your comments correctly. You should have
<?php foreach($comments as $comment){ ?>
And in your controller for this, use
$comments = $this->Article->Comment->find('all', ...
I am creating a similar application and at least that is how I handled everything.
If you need to, I will post back or email you my articles controller, comments controller and corresponding views for your review.
Thanks,
来源:https://stackoverflow.com/questions/8645141/something-wrong-with-this-find-statment-cakephp-1-3