Set fields in database table without posting the form

I want to set a bio id and a bio name when the user goes to the admin dashboard but the admin dashboard doesn’t use post so it would stay on the same page when the bio fields are created. So in order to save the bio data, I can’t use:
if ($this->request->is('post'))
I don’t want to use a form. I want to set the bio id to the user id in the session and the bio name with:
$this->set('authUser', $this->Auth->user());

Or should I set a bio id and name in the registration method instead of the admin dashboard? I don’t know how I would get the newly created user id while registering. The bio id has to be set to the user id but there is no user id until the form has been submitted.

Saving data doesn’t need to be present inside a if ($this->request->is('post'))

you can always do

$entity = $this->MyModel->get(123);
$entity = $this->MyModel->patchEntity( $entity, $data );

If this is actually needed/usefull depends on what you want to achieve/your application design.

Basically what I understand is, that you have some sort of registration form which redirects you after a successfull submission to the admin dashboard (already logged in without mail confirmation?)

I tried this in the controller but it didn’t work:

$session = $this->request->getSession();

        $user_id = $session->read('');
	    try {
          $bio = $this->Bios->get($user_id, [
            'contain' => ['Users'],
       } catch (RecordNotFoundException $ex) {
     	       $bio->id = $user_id;
		$bio->contributor = $session->read('Auth.User.contributor');
		$bio->body = "This cook hasn't added a bio yet.";
   I got the error:
   Record not found in table "bios"

I wasn’t using patchEntity or save so of course it couldn’t work.

I tried this way instead but it threw the same error

    try {
          $bio = $this->Bios->get($user_id, [
            'contain' => ['Users'],
       } catch (RecordNotFoundException $ex) {
        $this->Flash->error(__('You must add a bio before you can use the admin dashboard.'));
        return $this->redirect(['controller' => 'bios', 'action' => 'add']);

You’re trying to read a Bio with the same ID as the User. There’s always a chance that will work, but it would be coincidental. More likely, you’ll get the wrong Bio, or nothing at all (which will get you the error you’re seeing).

Your subject line is also misleading, it seems you don’t want to set (i.e. update) fields in the database at all. You want to set a session variable based on the logged-in user? There’s at least three ways I can think of to do that:

  1. Use get on the Users table, containing the Bio.
  2. Use find on the Bios table, with a condition like ['Bios.user_id' => $user_id].
  3. Use a custom finder in the authentication setup, so that what is found in $session->read('Auth.User') will always automatically include the Bio record.

In the bios add method, I set the bios id equal to the user id so it should be able to find a bio with the same id as the user id but the bio id isn’t set for the test user that I logged in as. I wanted to set the bios id equal to the user id in my first attempt, but that didn’t work so I tried re-directing to the bios add method instead. According to what Zuluru wrote, it sounds like I should use something like this:

$bio_id = $this->Bios->find('list')->select(['id'])->where(['' => $user_id])->first();

I added the following and tested a user that didn’t have a bio yet but no redirect occurred. It was supposed to redirect the user to the add bio page but it stayed at the admin dashboard and didn’t throw an error:

try {
        $bios_id =  $this->Bios->find('list')->select(['id'])->where(['' => $user_id])->first();
       } catch (RecordNotFoundException $ex) {
        $this->Flash->error(__('You must add a bio before you can use the admin dashboard.'));
        return $this->redirect(['controller' => 'bios', 'action' => 'add', $user_id]);

first doesn’t throw an exception if there are no records, it returns null. Either check for that, or use firstOrFail.

Thanks, Zuluru. I tried firstOrFail() instead and it returned: Record not found in table “bios”

This is what I used:
$bios_id = $this->Bios->find('list')->select(['id'])->where(['' => $user_id])->firstOrFail();

I tried to change my subject line but the edit didn’t work.

I just realized I wasn’t using:
use Cake\Datasource\Exception\RecordNotFoundException;

Thanks, everyone. It’s working now.