Bingo! Thanks so much for responding, Andy. It was fortuitous that I came across Polymorphic yesterday because I was questioning my db schema for this project. The site originally used a "user_type" table to differentiate but it's turned into a nightmare, with more changes on the horizon. In moving the site over to Cake, I'd decided to go with separate models but then ran into this issue. Doing the heavy lifting entirely in a behavior is absolutely the way to go here.
I'll have to spend a bit of time to compare these two to understand what's happening exactly but, thanks again for the hand up on this. It's truly a big load off. The Cake group can expect a donation from me just as soon as this site's changed over. On Thu, Dec 18, 2008 at 4:51 PM, AD7six <[email protected]> wrote: > > > > On Dec 18, 8:10 pm, brian <[email protected]> wrote: >> Andy Dawson's PolymorphicBehavior seems like the perfect answer to a >> problem I have but I'm having trouble implementing it. My situation is >> that I have a User model, then several different models which a user >> can "be" ie. 'Organization', 'Student', etc. So, I've tried doing >> this: >> >> users table: >> id >> created >> modified >> model >> foreign_key >> -- etc. >> >> class User extends AppModel >> { >> var $name = 'User'; >> var $actsAs = array('Polymorphic'); >> >> } >> >> class Admin extends AppModel >> { >> var $name = 'Admin'; >> >> var $hasOne = array( >> 'User' => array( >> 'className' => 'User', >> 'foreignKey' => 'foreign_key', >> 'conditions' => array('User.model' => 'Admin'), >> 'dependent' => true >> ) >> ); >> >> } >> >> UsersController: >> >> $user = $this->User->findByEmail($this->data['User']['email']); >> debug($user); exit; >> >> All I see is an array for 'User'. >> >> I've verified that the DB tables have the correct information. >> >> Am I looking at this the wrong way? In Andy's article[1] he uses the >> example of a Note class which can belongTo several others. My >> situation is a bit different, though it seems much closer to how I >> understood polymorphic classes to work. >> >> [1]http://bakery.cakephp.org/articles/view/polymorphic-behavior > > It's for polymorphic associations rather than polymorphic objects. > Although you can use it for what you're describing, it was written for > things like notes. > > Try the active-dev version: > http://trac.assembla.com/mi/browser/branches/base/models/behaviors/polymorphic.php#L105 > > you'd see a $data['User']['Admin'][ full admin data ] for an admin > user. > > hth > > AD > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
