Sunday, July 4, 2010

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

On Sun, Jul 4, 2010 at 8:49 AM, Tomfox Wiranata
<tomfox.wiranata@gmail.com> 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
>
>                        <div class="row">
>                                <span class="label">first name</span>
>                                <span class="profiledata">
>                                <?php
>                                foreach ($users as $user):
>                                        echo $user['User']['firstname'];
>                                endforeach;
>                                ?>
>                                </span>
>                        </div>
>
> 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 sooooo 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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: