Re: Setting a "where"-statement in the "find"-function

2010-07-04 Thread cricket
On Sun, Jul 4, 2010 at 8:49 AM, Tomfox Wiranata
 wrote:
> Hi,
>
> this one is a short question :)
>
> i want to retrieve user data from my DB to show them in the user
> profile:
> this is my controller:
>
>    function profile()
>    {
>        $this->set('users', $this->User->find('all'));
>    }
>
>
>
> my view profile.ctp
>
>                        
>                                first name
>                                
>                                                                foreach ($users as $user):
>                                        echo $user['User']['firstname'];
>                                endforeach;
>                                ?>
>                                
>                        
>
> i dont need the loop, do I!!
>
> the SELECT statement would now be without further restrictions in my
> WHERE clause. it finds all entries . but i just wanna have the user
> that is logged in. how do i get data from the logged in user and put
> it in this find-function?
>
> this is my login-function
>
>    function login()
>    {
>        //wurden alle login-felder ausgefüllt?
>        if(empty($this->data) == false)
>        {
>                if(($user = $this->User->validateLogin($this->data['User']))
> == true)
>            {
>                //ist der account freigeschaltet?
>                $active = $this->User->findByusername($this->data['User']
> ['username']);
>                if ($active['User']['active']==1)
>                {
>                        #$user = $this->data['User']['username'];
>                        $this->Session->write('User', $active['User']);
>
>                        $this->Session->setFlash('Du bist eingeloggt.');
>                        $this->redirect(array('controller' => 'users',
> 'action' => 'index'));
>                        exit();
>
>                        /*if (!empty($this->data['User']['remember_me']))
>                        {
>                                $cookie = array();
>                                $cookie['username'] = 
> $this->data['User']['username'];
>                                $cookie['pass'] = $this->data['User']['pw'];
>                                $this->Cookie->write('User', $cookie, true,
> '+3600*24');
>                        }
>                        unset($this->data['User']['remember_me']);*/
>                }
>                else
>                {
>                        $this->Session->setFlash('Der Account ist nicht
> freigeschaltet.');
>                        $this->redirect(array('controller' => 'users', 'action'
> => 'login'));
>                        exit();
>                }
>
>            }
>            else
>            {
>                $this->Session->setFlash('Falscher Benutzername/
> Passwort.');
>                $this->redirect(array('controller' => 'users',
> 'action' => 'login'));
>                exit();
>            }
>         }
>    }
>
>
> i guess i have to use information from the created session
>
> thanks so much
>


What is in your validateLogin() method? Could that return the User
data? In that case, you could have:

if ($user = $this->User->validateLogin($this->data['User'])

If you were to use AuthController it would already have fetched the
User data and you could do something like:

if ($user = $this->Auth->user())

Also, there would be no need to save any other cookies. Auth is quite
simple to use, once you've figured it out. It'll save you a lot of
coding.

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: Setting a "where"-statement in the "find"-function

2010-07-04 Thread Tomfox Wiranata
danke für die antwort nini

ich bekomme die fehlermeldung

Notice (8): Undefined index: User [APP\views\users\profile.ctp, line
47]

   echo $user['User']['firstname'];
endforeach;
?>

hast du eine idee? danke :)

On 4 Jul., 15:53, nini  wrote:
> In deinem Fall könnte man folgendes machen:
>
> function profile() {
>   $this->set('users', $this->User->findByUsername($this->Session-
>
> >read('User.username'));
> }
>
> In wie weit so eine Lösung sicher ist, ist fraglich.
> Warum nimmt man aber nicht einfach Auth-Komponente, dort gibt es doch
> schon alles was man braucht?
> Doku dazu gibt es auch.
>
> On Jul 4, 2:49 pm, Tomfox Wiranata  wrote:
>
> > Hi,
>
> > this one is a short question :)
>
> > i want to retrieve user data from my DB to show them in the user
> > profile:
> > this is my controller:
>
> >     function profile()
> >     {
> >         $this->set('users', $this->User->find('all'));
> >     }
>
> > my view profile.ctp
>
> >                         
> >                                 first name
> >                                 
> >                                  >                                 foreach ($users as $user):
> >                                         echo $user['User']['firstname'];
> >                                 endforeach;
> >                                 ?>
> >                                 
> >                         
>
> > i dont need the loop, do I!!
>
> > the SELECT statement would now be without further restrictions in my
> > WHERE clause. it finds all entries . but i just wanna have the user
> > that is logged in. how do i get data from the logged in user and put
> > it in this find-function?
>
> > this is my login-function
>
> >     function login()
> >     {
> >         //wurden alle login-felder ausgefüllt?
> >         if(empty($this->data) == false)
> >         {
> >                 if(($user = $this->User->validateLogin($this->data['User']))
> > == true)
> >             {
> >                 //ist der account freigeschaltet?
> >                 $active = $this->User->findByusername($this->data['User']
> > ['username']);
> >                 if ($active['User']['active']==1)
> >                 {
> >                         #$user = $this->data['User']['username'];
> >                         $this->Session->write('User', $active['User']);
>
> >                         $this->Session->setFlash('Du bist eingeloggt.');
> >                         $this->redirect(array('controller' => 'users',
> > 'action' => 'index'));
> >                         exit();
>
> >                         /*if (!empty($this->data['User']['remember_me']))
> >                         {
> >                                 $cookie = array();
> >                                 $cookie['username'] = 
> > $this->data['User']['username'];
> >                                 $cookie['pass'] = $this->data['User']['pw'];
> >                                 $this->Cookie->write('User', $cookie, true,
> > '+3600*24');
> >                         }
> >                         unset($this->data['User']['remember_me']);*/
> >                 }
> >                 else
> >                 {
> >                         $this->Session->setFlash('Der Account ist nicht
> > freigeschaltet.');
> >                         $this->redirect(array('controller' => 'users', 
> > 'action'
> > => 'login'));
> >                         exit();
> >                 }
>
> >             }
> >             else
> >             {
> >                 $this->Session->setFlash('Falscher Benutzername/
> > Passwort.');
> >                 $this->redirect(array('controller' => 'users',
> > 'action' => 'login'));
> >                 exit();
> >             }
> >          }
> >     }
>
> > i guess i have to use information from the created session
>
> > thanks so much

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: Setting a "where"-statement in the "find"-function

2010-07-04 Thread nini
In deinem Fall könnte man folgendes machen:

function profile() {
  $this->set('users', $this->User->findByUsername($this->Session-
>read('User.username'));
}

In wie weit so eine Lösung sicher ist, ist fraglich.
Warum nimmt man aber nicht einfach Auth-Komponente, dort gibt es doch
schon alles was man braucht?
Doku dazu gibt es auch.

On Jul 4, 2:49 pm, Tomfox Wiranata  wrote:
> Hi,
>
> this one is a short question :)
>
> i want to retrieve user data from my DB to show them in the user
> profile:
> this is my controller:
>
>     function profile()
>     {
>         $this->set('users', $this->User->find('all'));
>     }
>
> my view profile.ctp
>
>                         
>                                 first name
>                                 
>                                                                  foreach ($users as $user):
>                                         echo $user['User']['firstname'];
>                                 endforeach;
>                                 ?>
>                                 
>                         
>
> i dont need the loop, do I!!
>
> the SELECT statement would now be without further restrictions in my
> WHERE clause. it finds all entries . but i just wanna have the user
> that is logged in. how do i get data from the logged in user and put
> it in this find-function?
>
> this is my login-function
>
>     function login()
>     {
>         //wurden alle login-felder ausgefüllt?
>         if(empty($this->data) == false)
>         {
>                 if(($user = $this->User->validateLogin($this->data['User']))
> == true)
>             {
>                 //ist der account freigeschaltet?
>                 $active = $this->User->findByusername($this->data['User']
> ['username']);
>                 if ($active['User']['active']==1)
>                 {
>                         #$user = $this->data['User']['username'];
>                         $this->Session->write('User', $active['User']);
>
>                         $this->Session->setFlash('Du bist eingeloggt.');
>                         $this->redirect(array('controller' => 'users',
> 'action' => 'index'));
>                         exit();
>
>                         /*if (!empty($this->data['User']['remember_me']))
>                         {
>                                 $cookie = array();
>                                 $cookie['username'] = 
> $this->data['User']['username'];
>                                 $cookie['pass'] = $this->data['User']['pw'];
>                                 $this->Cookie->write('User', $cookie, true,
> '+3600*24');
>                         }
>                         unset($this->data['User']['remember_me']);*/
>                 }
>                 else
>                 {
>                         $this->Session->setFlash('Der Account ist nicht
> freigeschaltet.');
>                         $this->redirect(array('controller' => 'users', 
> 'action'
> => 'login'));
>                         exit();
>                 }
>
>             }
>             else
>             {
>                 $this->Session->setFlash('Falscher Benutzername/
> Passwort.');
>                 $this->redirect(array('controller' => 'users',
> 'action' => 'login'));
>                 exit();
>             }
>          }
>     }
>
> i guess i have to use information from the created session
>
> thanks so much

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en