Paras Narang
Paras Narang

Reputation: 631

How can I get the id of currently logged-in user in function beforeDelete() in cakePHP?

I want to access the id of currently logged in user

My beforeDelete() function in /app/models/course.php:

function beforeDelete() 
{
// Some code 
// code also sets value of $uid2

$uid = $this->Auth->user('id');    //this is line 86 in course.php

    if ($uid2 == $uid) {
            return true;
    } 
    else {
            return false;
    }
}

But during execution, I get the following error:

Notice (8): Undefined property: Course::$Auth [APP/models/course.php, line 86]
Fatal error: Call to a member function user() on a non-object in /var/www/some_path/app/models/course.php on line 86

Please suggest..

Upvotes: 0

Views: 218

Answers (1)

floriank
floriank

Reputation: 25698

Pass the id of the current logged in user with the data to the model from the controller.

$this->Model->data[$this->Model->alias]['user_id'] = $this->Auth->user('id');

In your beforeDelete() callback you can access it

$this->data[$this->alias]['user_id']

and do whatever you want there. I could give you further advice but your question is, to be honest, not very informative. Please be more specific about your goal in the future.

Upvotes: 2

Related Questions