Thanks for all of the ideas. Here's what I ended up doing, which works
great and does not break MVC :)

In the AppController:


function beforeFilter() {
    // put the latest published schedule into the session
    if (!$this->Session->check('Schedule')) {
        $schedule = ClassRegistry::init('Schedule')->find('first',
            array('order' => 'id desc')
        );
        $this->Session->write('Schedule', $schedule['Schedule']);
    }
    $this->{$this->modelClass}->schedule_id = $this->Session-
>read('Schedule.id');
}



And then in the AppModel:


var $schedule_id;

function contain() {
    $args = func_get_args();
    foreach($args as &$arg) {
        $arg .= '.schedule_id = ' . $this->schedule_id;
    }
    parent::contain($args);
}

function find($type, $params = array()) {
    if ($this->name != 'Schedule') {
        if (array_key_exists('contain', $params)) {
            foreach($params['contain'] as &$contain) {
                $contain .= '.schedule_id = ' . $this->schedule_id;
            }
        }
        $params = array_merge_recursive(
            $params,
            array(
                'conditions' => array(
                    "{$this->name}.schedule_id" => $this->schedule_id
               )
            )
        );
        if (isset($this->id)) {
            $params['conditions']["{$this->name}.id"] = $this->id;
        }
    }
    return parent::find($type, $params);
}


Now I can use the ideal shorter code from my first message on this
thread!
Next is to adapt it for saves and deletes as well.

Could this be a good solution for people who want to use composite
primary keys?

-Jason


On May 22, 9:57 am, Jamie <[email protected]> wrote:
> On May 22, 7:07 am, shantamg <[email protected]> wrote:
>
> > @Andrel: I'd love to be able to set the conditions in the model, but
> > the model can't know what's in the session!
>
> It can, you just need to load the Session object. Nothing wrong with
> that (in my opinion)...
>
> App::import('Core', 'CakeSession');
> $Session = new CakeSession();
> $myValue = $Sesssion->read('myValue');
>
> etc.
>
> Or just put it in your AppModel::__construct() function:
>
> function __construct($id = false, $table = null, $ds = null) {
>     parent::__construct($id, $table, $ds);
>     App::import('Core', 'CakeSession');
>     $this->_session = new CakeSession();
>
> }
>
> Then you've got a $this->_session variable in all of your models.
> Someone uptight about a rigid MVC structure might have an objection,
> but it does the job.
>
> Then, if you go that route, you can add in schedule_id conditions in,
> say, your AppModel::beforeFind() function:
>
> function beforeFind($queryData) {
>    // Check $queryData['conditions'] array for schedule_id, add it if
> it's not already there.
>    // Loop through $queryData['contain'] and add the condition to each
> contained model
>    return $queryData;
>
> }
>
> That's just off the cuff, but it might be what you're looking for.
>
> - Jamie
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with 
> their CakePHP related questions.
>
> 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 
> athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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