Hi

I had tried something similar to this before, using "hasOne (ugroup)"
in the users model, and "belongsTo (users)" in the ugroup model.

The code I had tried, and the one you suggested, both came up with a
SQL error.  Ive ended up solving this using Tuftys code, with the
addition of this from Avairet

 <?
class Ugroup extends AppModel
{
   public $primaryKey = 'ugroup_id';
}

?>

Thanks both for your help :)

On Dec 12, 10:16 pm, MrTufty <[EMAIL PROTECTED]> wrote:
> The error message you're getting tells you what the problem is... on
> line 34, you're referring to $this->Ugroup->findByUgroupid().
>
> This would be fine, except that you haven't told cake to give you
> access to Ugroup in this way. I'm assuming you have a model for
> Ugroup, and that it's associated with User in the correct way (which
> would appear to be User belongsTo Ugroup, Ugroup hasMany User). This
> would allow you to use... $this->User->Ugroup->findByUgroupid() to
> achieve what you want.
>
> Except by the magic of associations, you don't need to make the second
> query, as Cake's magic will bring in the information you're looking
> for with the first query, using a join.
>
> This should give you an array in your $someone, something like this:
>
> $someone = array(
>    'User' => array( ... user details ...),
>    'Ugroup' => array( ... usergroup details ...)
> );
>
> So you'll be able to refer to the Ugroup like this - $someone['Ugroup']
> ['perm_admin'].
>
> Hope that helps!
>
> On Dec 12, 7:37 am, Sanfly <[EMAIL PROTECTED]> wrote:
>
> > No good im afraid, same error, except ugroup_id is now ugroupid
>
> > --------------------------------------------------------------------
> > Notice: Undefined property: UsersController::$Ugroup in C:\server\www
> > \cake\basic_site\plugins\users\controllers\users_controller.php on
> > line 34
>
> > Fatal error: Call to a member function findByUgroupid() on a non-
> > object in C:\server\www\cake\basic_site\plugins\users\controllers
> > \users_controller.php on line 34
> > --------------------------------------------------------------------
>
> > On Dec 12, 7:31 pm, chad <[EMAIL PROTECTED]> wrote:
>
> > > try findByUgroupId
>
> > > On Dec 11, 10:08 pm, Sanfly <[EMAIL PROTECTED]> wrote:
>
> > > > Hi
>
> > > > Ive only just started mucking around with CakePHP, and am having a
> > > > little trouble getting my head around the controllers and models
> > > > thingees.  YES - I have tried looking in the manual!!
>
> > > > Im trying to make a cake website that will more or less mirror some
> > > > normal-non-framework php sites I have done.
>
> > > > There is a table called users where all normal user data is stored
> > > > (see below for table structure).  The ugroup table contains a
> > > > ugroup_name and a number of "permissions" that dictate what the user
> > > > will be able to access on the site.  The ugroup_id field in users
> > > > indicates which ugroup the user belongs to.
>
> > > > When the person logs in, I first want it to check that the user_name
> > > > and user_pass are valid, then access the ugroups table to set the
> > > > ugroup data (permissions) as sessions, which can then be called when I
> > > > like throughout the rest of the site.
>
> > > > users_controller.php
> > > > -----------------------------------------
> > > > <?php
> > > > class UsersController extends AppController
> > > > {
>
> > > >     function login()
> > > >     {
> > > >         $this->set('error', false);
>
> > > >         if (!empty($this->data))
> > > >         {
> > > >             $someone = $this->User->findByUser_name($this->data['User']
> > > > ['user_name']);
>
> > > >             if(!empty($someone['User']['user_pass']) &&
> > > > $someone['User']['user_pass'] == md5($this->data['User']
> > > > ['user_pass']))
> > > >             {
> > > >                 $this->Session->write('User', $someone['User']);
>
> > > >                                 // Here is the code that is giving me 
> > > > problems
> > > >                                 $perms = 
> > > > $this->Ugroup->findByUgroup_id($someone['User']
> > > > ['user_group']);
>
> > > >                                 if($perms['Ugroup']['perm_admin'] == 
> > > > "1"){
> > > >                                         
> > > > $this->redirect('/pages/admin/');
> > > >                                 }
> > > >                                 else{
> > > >                                         $this->redirect('/');
> > > >                                 }
> > > >                                 // End Problem code
> > > >             }
> > > >             else
> > > >             {
> > > >                $this->set('error', true);
> > > >             }
> > > >         }
> > > >     }
>
> > > >     function logout()
> > > >     {
> > > >        $this->Session->delete('User');
>
> > > >         $this->redirect('/');
> > > >     }}
>
> > > > ?>
>
> > > > ----------------------------------------------------------
> > > > However, this code gives me an error:
>
> > > > ----------------------------------------------------------
> > > > Notice: Undefined property: UsersController::$Ugroup in C:\server\www
> > > > \cake\basic_site\plugins\users\controllers\users_controller.php on
> > > > line 34
>
> > > > Fatal error: Call to a member function findByUgroup_id() on a non-
> > > > object in C:\server\www\cake\basic_site\plugins\users\controllers
> > > > \users_controller.php on line 34
> > > > ----------------------------------------------------------
>
> > > > Can anyone point me in the right direction to solve this?  Sorry, but
> > > > my cake knowledge is very limited and Im having trouble finding "cake
> > > > for dummies" tutorials :s
>
> > > > Thanks for any help :)
>
> > > > ------------------------------------------------------
> > > > My tables:
>
> > > > CREATE TABLE `users` (
> > > >   `user_id` int(25) NOT NULL auto_increment,
> > > >   `user_name` varchar(70) NOT NULL default '',
> > > >   `user_pass` varchar(70) NOT NULL default '',
> > > >   `ugroup_id` int(25) NOT NULL,
> > > >   `user_email` varchar(50) NOT NULL default '',
> > > >   `user_active` enum('0','1') NOT NULL default '0',
> > > >   `user_logged` datetime NOT NULL default '0000-00-00 00:00:00',
> > > >   `user_display` varchar(30) NOT NULL default '',
> > > >   `user_joined` datetime NOT NULL default '0000-00-00 00:00:00',
> > > >   `user_access` enum('0','1') NOT NULL default '0',
> > > >   `user_bio` text NOT NULL,
> > > >   `user_web` varchar(200) NOT NULL,
> > > >   PRIMARY KEY  (`user_id`),
> > > >   KEY `ugroup_id` (`ugroup_id`)
> > > > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
>
> > > > ALTER TABLE `users`
> > > >   ADD CONSTRAINT `users_ibfk_1` FOREIGN KEY (`ugroup_id`) REFERENCES
> > > > `ugroups` (`ugroup_id`);
>
> > > > CREATE TABLE `ugroups` (
> > > >   `ugroup_id` int(25) NOT NULL auto_increment,
> > > >   `ugroup_name` varchar(70) NOT NULL default '',
> > > >   `ugroup_rank` int(10) NOT NULL,
> > > >   `perm_admin` enum('0','1') NOT NULL default '0',
> > > >   `perm_configweb` enum('0','1') NOT NULL default '0',
> > > >   `perm_setperm` enum('0','1') NOT NULL default '0',
> > > >   `perm_adduser` enum('0','1') NOT NULL default '0',
> > > >   `perm_edituser` enum('0','1') NOT NULL default '0',
> > > >   `perm_viewuserhidden` enum('0','1') NOT NULL default '0',
> > > >   `perm_viewusers` enum('0','1') NOT NULL default '0',
> > > >   `perm_addgroup` enum('0','1') NOT NULL default '0',
> > > >   `perm_editgroup` enum('0','1') NOT NULL default '0',
> > > >   `perm_viewgroup` enum('0','1') NOT NULL default '0',
> > > >   PRIMARY KEY  (`ugroup_id`)
> > > > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--~--~---------~--~----~------------~-------~--~----~
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