Welcome Guest, Not a member yet? Register   Sign In
You must use the "set" method to update an entry. (Again)
#1

Hello everyone,

I'm currently migrating my CI3 project to CI4, but unfortunately I'm struggling with I guess one of the easier things Sad . I already searched the forums and tried some things, but couldn't find a fitting solution. I think my main problem is that the error doesn't really tell me what's wrong and I have no clue what could bring me a step closer to the solution at this point.

In general I only want to create a user in my user model.

This is my Controller Test Case
PHP Code:
public function CreateUserTest()
    {
        
$userModel = new UserModel();
        
$returnValue $userModel->create_user('MrXXX''12345678''Max''Muster''[email protected]');
    } 

This is my UserModel declaration and definitions
PHP Code:
protected $DBGroup 'users';
protected 
$table 'user';
protected 
$allowedFields = ['role_id, first_name, last_name, username, password, '
    
'email_address, mail_me, ack_round, language, referee, xp, uuid, '
    
'activation_token, activation, dsgvo, notified']; 

This is my insert statement inside the createuser method
PHP Code:
$this->set('uuid''UUID()'false);
    
//$this->set('referee', $referee);   
    
$data = ['username' => $username,
            
'first_name' => $first_name,
            
'last_name' => $last_name,
            
'email_address' => $email_address,
            
'password' => $hash,            
            
'activation_token' => $token];
    
var_dump($data);
    
$insert $this->insert($data); 

The result from var_dump looks like this:
Code:
array(6) { ["username"]=> string(5) "MrXXX" ["first_name"]=> string(3) "Max" ["last_name"]=> string(6) "Muster" ["email_address"]=> string(15) "[email protected]" ["password"]=> string(60) "CleandUpCleandUp..." ["activation_token"]=> string(254) "CleandUpCleandUp..." }

Thx for your help!
Reply
#2

(This post was last modified: 10-13-2020, 05:05 AM by InsiteFX.)

You need to use QueryBuilder.

PHP Code:
$data = [
    'username'         => $username,
    'first_name'       => $first_name,
    'last_name'        => $last_name,
    'email_address'    => $email_address,
    'password'         => $hash,            
    
'activation_token' => $token
];
    
$insert 
$this->builder->insert($data);

// or

$db      db_connect();
$builder $db->table('users');

$data = [
    'username'         => $username,
    'first_name'       => $first_name,
    'last_name'        => $last_name,
    'email_address'    => $email_address,
    'password'         => $hash,            
    
'activation_token' => $token
];
    
$insert 
$builder->insert($data); 

Take your pick both should work for you.

Set is only used on update.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Hello, thx for the answer,

I tried the following:

PHP Code:
$data = ['username' => $username,
            
'first_name' => $first_name,
            
'last_name' => $last_name,
            
'email_address' => $email_address,
            
'password' => $hash,            
            
'activation_token' => $token];    
    
$insert $this->builder->insert($data); 

Resulting in the error: Call to a member function set() on null.

Maybe I have to add the point that the create_user method is part of the UserModel, therefore I thought that with my $this->insert() the method described in the documentation is used: https://codeigniter.com/user_guide/models/model.html

Quote:insert()

An associative array of data is passed into this method as the only parameter to create a new row of data in the database. The array’s keys must match the name of the columns in a $table, while the array’s values are the values to save for that key:
Reply
#4

The set statement is only used to set the fields or data array for an update not insert.

You must have something else wrong someplace.

Check your data fields against the table columns and make sure there not miss spelled.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 10-13-2020, 02:28 PM by Petrusilius.)

Hello,
thx again for the fast answer, what I did right now was, I've made an SQL export of the database and double checked it again. Did that also yesterday, but to be sure. Sorry for comments in german in the dump. What I did also was testing another method in the model to ensure the connection to the database in general works, at least that I could ensure. I also tried to reduce the amount of parameters to find the problematic one, but unfortunately that also didn't help.

I've also added here now my Code from CI3 that has worked with the same database, when also done in a different way. (I tried this already, but same result Sad )


SQL Dump
Code:
-- phpMyAdmin SQL Dump
-- version 4.9.2
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Erstellungszeit: 13. Okt 2020 um 22:41
-- Server-Version: 10.3.21-MariaDB
-- PHP-Version: 7.2.29

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Datenbank: `users`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `user`
--

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `role_id` int(11) NOT NULL DEFAULT 1,
  `first_name` varchar(25) NOT NULL,
  `last_name` varchar(25) NOT NULL,
  `username` varchar(25) NOT NULL,
  `password` blob DEFAULT NULL,
  `email_address` blob DEFAULT NULL,
  `mail_me` tinyint(1) NOT NULL DEFAULT 0,
  `ack_round` tinyint(1) NOT NULL DEFAULT 0,
  `language` enum('de-de','en-us') NOT NULL DEFAULT 'de-de',
  `referee` int(11) DEFAULT NULL,
  `xp` int(11) NOT NULL DEFAULT 0,
  `uuid` char(36) NOT NULL,
  `activation_token` varchar(255) NOT NULL,
  `activation` tinyint(1) NOT NULL DEFAULT 0,
  `dsgvo` tinyint(1) NOT NULL DEFAULT 0,
  `notified` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `user`
--
ALTER TABLE `user`
  ADD PRIMARY KEY (`id`,`username`),
  ADD UNIQUE KEY `username` (`username`),
  ADD KEY `role_id` (`role_id`),
  ADD KEY `referee` (`referee`);

--
-- AUTO_INCREMENT für exportierte Tabellen
--

--
-- AUTO_INCREMENT für Tabelle `user`
--
ALTER TABLE `user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=19;

--
-- Constraints der exportierten Tabellen
--

--
-- Constraints der Tabelle `user`
--
ALTER TABLE `user`
  ADD CONSTRAINT `user_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`),
  ADD CONSTRAINT `user_ibfk_2` FOREIGN KEY (`referee`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE SET NULL;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Old Code from CI3
PHP Code:
    $users $this->load->database('users'TRUE);
    $insert $users->set('first_name'$this->input->post('first_name'))
            ->set('last_name'$this->input->post('last_name'))
            ->set('email_address'$this->input->post('email_address'))
            ->set('username'$this->input->post('username'))
            ->set('password'$hash)
            ->set('uuid''UUID()'false)
            //TODO: ->set('referee', $this->session->userdata('user_id'))
            ->set('activation_token'$token)
            ->insert('user'); 

Additionally the Call Stack from the log
Code:
CRITICAL - 2020-10-13 16:24:38 --> You must use the "set" method to update an entry.
#0 /volume1/web/Commercia/system/Database/BaseBuilder.php(2234): CodeIgniter\Database\BaseBuilder->validateInsert()
#1 /volume1/web/Commercia/system/Model.php(755): CodeIgniter\Database\BaseBuilder->insert()
#2 /volume1/web/Commercia/app/Models/UserModel.php(97): CodeIgniter\Model->insert(Array)
#3 /volume1/web/Commercia/app/Controllers/Test/UserModelTest.php(11): App\Models\UserModel->create_user('MrXXX', '12345678', 'Max', 'Muster', '[email protected]')
#4 /volume1/web/Commercia/system/CodeIgniter.php(918): App\Controllers\Test\UserModelTest->CreateUserTest()
#5 /volume1/web/Commercia/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Test\UserModelTest))
#6 /volume1/web/Commercia/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#7 /volume1/web/Commercia/public/index.php(45): CodeIgniter\CodeIgniter->run()
#8 {main}
Reply
#6

If you getting the field values form a form you can do it like this.

PHP Code:
$db      db_connect();
$builder $db->table('user');

$data = [
    'first_name'       => $this->request->getPost('first_name'),
    'last_name'        => $this->request->getPost('last_name'),
    'email_address'    => $this->request->getPost('email_address'),
    'username'         => $this->request->getPost('username'),
    'password'         => $hash,
    'uuid'             => 'UUID()',
    //TODO: 'referee'  => $this->session->userdata('user_id'),
    'activation_token' => $token,
];

$insert $builder->insert($data); 

When doing an update it would be like above except you would use set on the array and then do an update.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

Hehehe I think that would have been an alternative way but I could found the reason why it hasn't worked. I was able to backtrace loosing my data inside the doProtectFields method. I vardumped the information from the allowedFields and then there it is right in front of me the whole time also posted here, the error Big Grin

PHP Code:
protected $allowedFields = ['role_id, first_name, last_name, username, password, '
    
'email_address, mail_me, ack_round, language, referee, xp, uuid, '
    
'activation_token, activation, dsgvo, notified']; 

Maybe you can see him also Wink Thx for your help Smile
Reply
#8

You missed placed single quotes and continuation.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

(This post was last modified: 10-15-2020, 06:54 AM by T.O.M..)

Shouldn't $allowedFields be an array?
PHP Code:
protected $allowedFields = ['role_id''first_name''last_name''username''password'
    
'email_address''mail_me''ack_round''language''referee''xp''uuid'
    'activation_token''activation''dsgvo''notified']; 
And in your case, it is an array of one string element.
Reply
#10

Totally correct, that has fixed it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB