XF 2.2 InvalidArgumentException: Column 'user_id' is read only, can only be set with forceSet

AndBegin

Member
Hello, when trying to add text to the database, an error occurs when saving.

Error:
InvalidArgumentException: Column 'user_id' is read only, can only be set with forceSet in src\XF\Mvc\Entity\Entity.php at line 612

My code:

HTML:
<?php

namespace LKSWDQW\WM\XF\Pub\Controller;

class Account extends XFCP_Account
{
    public function actionAccountDetails()
    {
        $parent = parent::actionAccountDetails();

        if ($this->isPost())
        {
            $wMessage = $this->filter('lkswdqw_wm', 'str');
            $message = $this->em()->create("XF:User");
            $message->user_id = 1;
            $message->lkswdqw_wm = "{$wMessage}";
            $message->save();
        }

        return $parent;
    }
}
 
What are you actually trying to do with this code?

You are creating a new user with a user_id of 1 and trying to save that to the database - which does not seem like what you actually intend to do (and is never going to work).

I think this line is perhaps wrong?

$message = $this->em()->create("XF:User");

I don't think you want to be creating a User entity at this point.
 
Hello, when trying to add text to the database, an error occurs when saving.

Error:
InvalidArgumentException: Column 'user_id' is read only, can only be set with forceSet in src\XF\Mvc\Entity\Entity.php at line 612

My code:

HTML:
<?php

namespace LKSWDQW\WM\XF\Pub\Controller;

class Account extends XFCP_Account
{
    public function actionAccountDetails()
    {
        $parent = parent::actionAccountDetails();

        if ($this->isPost())
        {
            $wMessage = $this->filter('lkswdqw_wm', 'str');
            $message = $this->em()->create("XF:User");
            $message->user_id = 1;
            $message->lkswdqw_wm = "{$wMessage}";
            $message->save();
        }

        return $parent;
    }
}
Actually in the code you've posted you're creating a new user with ID = 1.

Can i ask what you're trying to do ??
 
Actually in the code you've posted you're creating a new user with ID = 1.

Can i ask what you're trying to do ??

This code must be saved when saving the user settings, the transmitted data in the column, and the user database with the value from the input fields.

value "test" I added myself in the database

1651590022814.webp
 
This code must be saved when saving the user settings, the transmitted data in the column, and the user database with the value from the input fields.

value "test" I added myself in the database

View attachment 267838
You need to update the user entity which first you must retrieve it and then update by calling

PHP:
public function updateMember(\XF\Mvc\Entity $user, array $options)
{
    if(!($user instanceof \XF\Entity\User))
    {
        return new \LogicException("Entity {$user} is not an instance of \XF\Entity\User");
    }
    if($user->exists())
    {
        foreach($options as $key => $value)
        {
            if($user->isValidColumn($key))
            {
                $options->set($key,$value);
            }
        }
        $user->save();
    }
}
// So now in controller you should use like this

$user = $this->em()->find("XF:User",$userId);  // you may pass relations in third parameter
if(!$user)
{
    // Handle error if user not found here.
}
$this->updateMember($user, []); // in second parameter u should enter data u wish to save.
Hope i've helped u to understand better.
This is an example how u can do this ...
 
Back
Top Bottom