Writing custom findByYear function in post model - chapter 7, page 106

7 messages Options
Embed this post
Permalink
MatthewJenkins

Writing custom findByYear function in post model - chapter 7, page 106

Reply Threaded More More options
Print post
Permalink
I'm getting no output from the debug function when I visit this URL:
http://domain.com/blog/posts/read/2008

Can someone tell me if I'm doing this right?
I've created a view file named "read.ctp" inside the views/posts folder:

/views/posts/read.ctp

It contains nothing but this code: <?php debug($posts);?>

Am I doing this right?
The little query-shower at the bottom of the page says shows me:
        SELECT `Post`.`id`, `Post`.`name`, `Post`.`date`, `Post`.`content`, `Post`.`user_id`, `User`.`id`, `User`.`name`, `User`.`email`, `User`.`firstname`, `User`.`lastname` FROM `posts` AS `Post` LEFT JOIN `users` AS `User` ON (`Post`.`user_id` = `User`.`id`) WHERE DATE(`Post`.`date`) = '<2008-12-31 23:59:59'

so I assume it's running the query correctly.

Any help would be appreciated,

Matthew
backwardselvis

Re: Writing custom findByYear function in post model - chapter 7, page 106

Reply Threaded More More options
Print post
Permalink

User: Emmanuel Becerra posted this earlier in the forum

One of the samples in the book is wrong Page 107:

 function findByYear($year=null) {
      $date = $year.'-01-01 00:00:00';
      $end_date = $year.'-12-31 23:59:59';
      return $this->find('all',array('conditions'=>array('DATE(Post.date)'=>'>'.$date,'DATE(Post.date)'=>'<'.$end_date)));
 }

The correct function is:

function findByYear($year=null) {
                $date = $year.'-01-01 00:00:00';
                $end_date = $year.'-12-31 23:59:59';
                return $this->find('all',array('conditions'=>array('DATE(Post.date) >' => $date,'DATE(Post.date) <' => $end_date)));
        }

I hope this helps.

Thanks!
backwardselvis

Re: Writing custom findByYear function in post model - chapter 7, page 106

Reply Threaded More More options
Print post
Permalink
In reply to this post by MatthewJenkins
AND

The "Query-Show-er" just shows you the query being run. You should be getting the debug output in a YELLOW box
backwardselvis

Re: Writing custom findByYear function in post model - chapter 7, page 106

Reply Threaded More More options
Print post
Permalink
In reply to this post by MatthewJenkins
Something else I just thought of. If you are entering the following path

../posts/read/2008

be aware that you must have records in the database that were inserted with this DATE.

You probably should be looking at

../posts/read/2009

-later
MatthewJenkins

Re: Writing custom findByYear function in post model - chapter 7, page 106

Reply Threaded More More options
Print post
Permalink
In reply to this post by backwardselvis
Thanks backwardselvis!

I will give that code a try!
MatthewJenkins

Re: Writing custom findByYear function in post model - chapter 7, page 106

Reply Threaded More More options
Print post
Permalink

That worked, thanks! I was just about ready to give up on that part; I appreciate your help.

APress should really work these fixes into the downloadable code from their site. Errata crx are not useful unless they're made available to readers.
MatthewJenkins

Re: Writing custom findByYear function in post model - chapter 7, page 106

Reply Threaded More More options
Print post
Permalink
In reply to this post by backwardselvis

Good call, I had that same thought.

For me, 2008 brings up plenty of posts. I started this book in November!
I'm a PHP beginner so It's slow-going for me.