0

I am trying to send the latest user's id from UsersController to AdminController whose add_employee() action creates a new employee. My users and employees table are separate and what I want to do is when Admin creates a new user its entry go into users table. Then he opens create employee form and the latest user id will be assigned to the new employee the admin is creating. So when admin will open create new employee form the latest user id will be shown in the form. My UsersController has this code for sending latest user it to AdminsController:

public function get_latest_user_id()
    {
        $content = $this->User->query("SELECT id FROM users ORDER BY id DESC LIMIT 0,1");
        $this->set('latest_user', $content);
    }

AdminsController page's add_employee contains this code:

public function add_employee()
    {
        $this->loadModel('Employee');
        $this->set('latest_user', $this->requestAction('/Users/get_latest_user_id'));
        if ($this->request->is('post'))
        {
            $this->Employee->create();
            if ($this->Employee->save($this->request->data))
            {
                $this->Session->setFlash(__('The employee profile has been saved.'));
                return $this->redirect(array('action' => 'list_of_employees'));
            }
            else
            {
                $this->Session->setFlash(__('The employee profile could not be saved. Please, try again.'));
            }
        }
    }

So UserController's get_latest_user_id function sends latest user id to add_employee function of AdminController. There latest_user is set to latest user id so that when add_employee view is called it is there. But it is not showing. So I want to know that am i doing it right? Please help and thanks.

In add_employee.ctp I am displaying it like this:

echo $latest_user['User']['id'];
user3224207
  • 457
  • 4
  • 14
  • 1
    http://stackoverflow.com/questions/979709/what-is-the-equivalent-to-getlastinsertid-in-cakephp/5592197#5592197 – Anubhav Jan 23 '14 at 08:12

2 Answers2

0

Move get_latest_user_id to the User model

public function get_latest_user_id()
{
    $user = $this->query("SELECT id FROM users ORDER BY id DESC LIMIT 1");
    if (empty($user)) {
        return 0;
    }
    // return only the Id
    return $user[0]['users']['id'];
}

In the controller:

public function add_employee()
{
    $this->loadModel('Employee');

    $this->loadModel('User');
    $this->set('latest_user', $this->User->get_latest_user_id());
    if ($this->request->is('post'))
    {
       // ....
    }
}
cornelb
  • 6,046
  • 3
  • 19
  • 30
0

cornelb is right that you should move the method to your User model. Although a more Cake-ish approach would be to use a find('first'), rather than doing a direct query():

// app/Model/User.php
public function getLatest() {
    // Get the latest user
    $user = $this->find('first', array(
        'fields' => array('id'), // Only interested in id? Use this.
        'order' => array('User.id' => 'DESC')
    ));

    if (!empty($user)) {
        return $user['User']['id'];
    } else {
        // Nothing was returned, this is very awkward
        throw new NotFoundException(__('No users found!'));
    }
}

And in your controller:

// app/Controller/AdminsController.php
public function add_employee() {
    $this->loadModel('User');
    $this->set('latestUser', $this->User->getLatest());
    // ...
}
Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • Ok thnx for this suggestion but the problem was find was not working properly. – user3224207 Jan 23 '14 at 10:15
  • @user3224207 If finds are not working properly, there is something seriously wrong with your models and I would investigate that further. It's one of Cake's deepest core features that should really work in a case like this. – Oldskool Jan 23 '14 at 10:17