On Dec 23, 2007 8:53 AM, Christophe Cholot <[EMAIL PROTECTED]>
wrote:

>
> Hello,
>
> Assuming you have a newsletter Controller and a newsletter Model :
> in NewsletterController (newsletter_controller.php) :
>
> class NewsletterController extends AppController {
>    var name = 'Newsletter';
>
>   function archive_list(){
>       $this->set('archives', $this->Newsletter->find('all',
> array("Newsletter.publish_date" => "< NOW()"))
>   }
> }
> In your archive list view (archive_list.ctp), you'll be able to
> iterable over your archives through $archives.
>
> or .. in your controller above, (assuming its not the
> NewsletterController) just do :
>
>  var uses = array('Newsletter');
>
> function archive_list(){
>        $this->set('archives', $this->Newsletter->getArchives());
> }
>
> in the Newsletter model, create a method for your archive list :
>
> function getArchives(){
>        $sql = 'SELECT id, to_char(publish_date, \'Mon, YYYY\') AS
> month FROM newsletter '  . 'WHERE publish_date < NOW() ORDER BY
> publish_date DESC';
>        return $this->query($sql);
> }
>

Thanks for your reply. I'd actually gotten it working but couldn't reply
because new posters are moderated.

Here's what i did:

// newsletters_controller.php
function archive_list()
{
    $sql = 'SELECT id, to_char(publish_date, \'YYYY-MM\') AS month FROM
newsletters '
        . 'WHERE publish_date < NOW() ORDER BY publish_date DESC';

    return $this->Newsletter->query($sql);
}

// view.ctp

$this->renderElement('newsletter_archive_list')

// views/elements/newsletter_archive_list.ctp

$archive_list = $this->requestAction('newsletters/archive_list');

while (list ($id, $issue) = each($archive_list))
{
...


This works but it seems to me that the query should be in the model. Using
your example above, i changed things to:

// newsletters_controller.php

function get_archive_list()
{
    $this->set('archive_list', $this->Newsletter->getArchives());
}


// views/elements/newsletter_archive_list.ctp

$this->requestAction('newsletters/get_archive_list');

which results in:
Variable passed to each() is not an array or object

because $archive_list is null. If i change it to $this->archive_list i get
an error stating that the view does not contain this variable. Since it was
set in the controller, it seems i have to refer to that in the element.
However, i'm at a loss here--how do i refer to the controller from the
element (which is a part of the view)?

It still seems a bit odd to me that the $archive_list var is being set in
the controller, rather than the model, though the DB query is in the model.
So i tried doing this in the controller:

$this->Newsletter->set('archive_list', $this->Newsletter->getArchives());

and this in the element:

while (list ($id, $issue) = each($this->Newsletter->archive_list))

So, unless i can figure out how to set the var somewhere (and how to
retrieve it) it seems that my only other optioin is to go back to just
returning it from the method.

My head is spinning!


>
> .. or simply correct the typo $this->Newsletter instead of $this-
> >newsletter.
>
>

Yes, that was only a typo in the mail.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to