On Sun, Nov 14, 2010 at 7:42 AM, O.J. Tibi <[email protected]> wrote:
> Hi all,
>
> Cutting to the chase:
>
> 1.) I'm writing code in a controller now.
> 2.) I need "raw" data (text from a cookie/s) managed by a custom
> component (that could use the built-in Cookie component).
> 3.) The cookie's value will be used to retrieve associated data from a
> particular model.
> 4.) The resulting record's primary key (id) will be set into $this-
>>data as an association (e.g., $this->data['User']['foreign_id'] =
> $resultRecord['SampleModel']['id'])
> 5.) Save $this->data into the final model.
>
> I'm thinking of doing an App::import('Model') inside my custom
> component, OR doing an App::import('Component') in my lookup model,
> since "fat models and skinny controllers" is an often preached
> discipline. These approaches feel dirty because accessing a model
> inside a component, and accessing components inside models are not
> generally recommended methods of writing manageable code.
>
> I'm also thinking of writing the coupling code inside the controller,
> although it would make the controller a little fatter ("keep it short,
> stupid" is another discipline I'm striving to practice diligently).
> Loose-coupling in the long run will make it easier to maintain and add
> new features, but I'm not sure if writing the bulk of the code in the
> controller is worth it.
>
> Can you guys suggest how you approach this dilemma, or at least point
> me where to look for techniques?
Take advantage of the fact that components' callbacks are handed a ref
to the controller. You've got several choices:
public function initialize(&$Controller, $settings = array()) {}
public function startup(&$Controller) {}
public function beforeRender(&$Controller) {}
Given that, it should be a cinch to get at $Controller's data. You
just need to figure out which requests are the ones you want to act
upon, then if $Controller->data is empty or not. Additionally, you can
use the controller's model to get at its associated model.
$this->Controller->Model->OtherModel->find(...)
Perhaps even better, put the find() in one of the models. If doing
that, one might as well put it in the first instead of dealing with
the chain inside the component:
$fk = $this->Controller->Model->someMethod($cookie_value);
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