Having trouble creating working read.ctp file for chapter 7

3 messages Options
Embed this post
Permalink
fightinggeek

Having trouble creating working read.ctp file for chapter 7

Reply Threaded More More options
Print post
Permalink
I tried modifying the code from the view file but I get Undefined Index errors. Can someone help?

Here's my code:

<?=$html->link('<h1>'.$posts['Post']['name'].'</h1>','/posts/view/'.$posts['Post']['id'],null,null,false);?>
<p>Author: <?=$posts['User']['name'];?>
<p>Date Published: <?=$time->nice($posts['Post']['date']);?></p>
<hr/>
davidgolding

Re: Having trouble creating working read.ctp file for chapter 7

Reply Threaded More More options
Print post
Permalink
You might want to try using the (now encouraged) method for specifying the route:

<?=$html->link('<h1>'.$posts['Post']['name'].'</h1>', array('controller'=>'posts','action'=>'view',$posts['Post']['id']), null, null, false);?>

The undefined index error is likely to missing the Time helper in the $helpers declaration in the controller class.

var $helpers = array('Time','Html','Ajax','Form','Javascript'); //anything else you may need
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

[ get your copy at: http://www.amazon.com/Beginning-CakePHP-Novice-Professional/dp/1430209771/ ]
fightinggeek

Re: Having trouble creating working read.ctp file for chapter 7

Reply Threaded More More options
Print post
Permalink
No, that didn't fix it and the time helper is declared. Maybe my debug code will help. I've been at it for two days now.

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [name] => How to Skateboard
                    [date] => 2009-06-28 23:01:00
                    [content] => A simple detailed how to tutorial for skateboarding.
                    [user_id] => 1
                )

            [User] => Array
                (
                    [id] => 1
                    [name] => Bartman
                    [email] => bsimpson@fox.com
                    [firstname] => Bart
                    [lastname] => Simpson
                )

            [comment] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Great Article
                            [content] => I learned alot
                            [post_id] => 1
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [name] => Good for begginers
                            [content] => OK for beginners, but lacking for more advanced people
                            [post_id] => 1
                        )

                )

        )

    [1] => Array
        (
            [Post] => Array
                (
                    [id] => 2
                    [name] => How to make beer
                    [date] => 2009-06-28 23:31:00
                    [content] => This is a detailed article on how to make beer
                    [user_id] => 2
                )

            [User] => Array
                (
                    [id] => 2
                    [name] => Duffman
                    [email] => hsimpson@fox.com
                    [firstname] => Homer
                    [lastname] => Simpson
                )

            [comment] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => Glughhhhh Beer
                            [content] => No beer and no TV make me something, something
                            [post_id] => 2
                        )

                )

        )

)

Here's the error:

Notice (8): Undefined index:  Post [APP\views\posts\read.ctp, line 2]
Notice (8): Undefined index:  User [APP\views\posts\read.ctp, line 3]
Notice (8): Undefined index:  Post [APP\views\posts\read.ctp, line 4]