Monday, March 31, 2014

Re: Stuck on Array issue: $post['Post']['id'] not works now, but $post[0]['id'] is ok

That's strange, findBy<field> is usually the same as doing $this->find('first'), unless you have extended or overridden somewhere?

Try doing the following just to see if it makes a difference? Your issue it seems is that your query is similar to doing $this->find('all') which can have more than one result.

$post = $this->Post->find('first', array('conditions' => array('Post.id' => $id)));


On 31 March 2014 04:42, Sunshine Fang <fang.sunshine@gmail.com> wrote:
Hi, cakePHPers:

I am an architect,  not quite familiar with CakePHP before, recently after an upgrade to CakePHP 2.4.6, $post['Post']['id'] not works now, but $post[0]['id'] is ok.

I am not sure it's caused by CakePHP or other stacks,  because I upgraded CakePHP from 2.1 to 2.4.6 , CentOS from 6.0 to 6.5, and PHP upgraded from 5.2.14 to 5.5.10 too.

Here's the code and screenshot:

Model/Post.php
<?php 
class Post extends AppModel {
}
 

Posts/view.ctp:

<h1><?php echo h($post['Post']['title']); ?></h1> 
<p><small>Created: <?php echo $post['Post']['created']; ?></small></p> 
<p><?php echo $post['Post']['body']; ?></p> 

<h1><?php echo h($post[0]['title']); ?></h1> 
<
p><small>Created: <?php echo $post[0]['created']; ?></small></p> 
<
p><?php echo $post[0]['body']; ?></p>


PostsController.php:
<?php class PostsController extends AppController { 
 
  public $helpers = array('Html', 'Form'); 
   public function view($id = null ) { 
       $post = $this->Post->findById($id); 
 
      print_r($post); 
 
      $this->set('post', $post); 
 
  } 
}


Please let me know what's wrong, the code? the upgrade of CakePHP, or other stack issue. Thanks.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards
 Stephen Speakman

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

1 comment:

Mtoag Techhnologies said...

Somewhere the content of the blog surrounded by little arguments. Yes it is healthy for readers. They can include this kind of language in their writing skill as well as while group discussion in college.
CakePHP Developer