I'm having some difficulty figuring out how to set a variable that I'd like
to retrieve from the DB and to make available application-wide.

The site is an online journal and I need to have the latest edition ID at
hand for pretty much every page, controller, model, etc. I thought that I
might be able to do it from bootstrap.php but that's not looking promising.
I've been fiddling around with app_controller with some success but have run
into a further roadblock.

Here's what I have so far:

class AppController extends Controller
{
    var $uses = array('Issue');
    var $latest_issue_id;
    ...

    function beforeFilter()
    {
        if (isset($this->Issue))
        {
            /* this first var is for other controllers
             */
            $latest_issue_id = $this->Issue->getLatestId();

            /* now set for the views
             */
            $this->set('latest_issue_id', $latest_issue_id);
        }
    }
...
}

class Issue extends AppModel
{
   ...
    function getLatestId()
    {
        $tmp = Set::extract($this->query('SELECT MAX(id) FROM issues WHERE
published = true'), '{n}.0.MAX(id)');

        return $tmp[0];
    }

BTW, if anyone can point out a more elegant way of doing that, I'd
appreciate it. I can't figure out how to make Set give me anything aside
from an array. I also tried using the field() method without any luck. It
seemed to me that this should work:

$this->field('id', array('id' => '-!MAX(id)', 'published' => '1'));

but that doesn't return anything.


class IssuesController extends AppController
{
    var $name = 'Issues';
    ...
    function view($id = null)
    {
        if (!$id)
        {
            /* no sense looking this up as it's already available
             */
            $id = $this->latest_issue_id;
        }

        $this->set('issue', $this->Issue->find('first',
            array(
                'conditions' => array('Issue.id' => $id),
                'contain' => array(
                    'Section',
                    'Contribution' => array(
                        'fields' => array('id', 'slug', 'title',
'section_id'),
                        'order' => 'Contribution.section_id ASC',
                        'Contributor' => array(
                            'fields' => array('first_name', 'last_name',
'slug')
                        )
                    )
                )
            )
        ));
    }



Now, the above works just fine. The 'view'  view for Issue includes a call
to the toc element to display the table of contents. The element toc.php
expects that an $issue var will be set.

However, the client wants the main site page to display the same as the toc
for the latest issue. So, I created the following route:

Router::connect('/', array('controller' => 'issues', 'action' => 'view'));

The fact that the $id is not set will mean that IssuesController will use
$latest_issue_id. But this is always NULL when using the above route. What
am I missing? How could it now be empty?

And, is there a better way to do all of this? For the record, I do plan to
keep this var in the session, once I can figure out how to set it properly.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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