• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Model refusing to insert data

#1
Hi all.

I am building a web app in CI4 and currently testing my register controller.

I can confirm the data is being read properly by $this->request->getPost() but when I go to save it with my user model, it simply doesn't do anything.

Please help me before I lose my sanity.

PHP Code:
  public function register()
  {
    $userModel = new UserModel();
    $data $this->request->getPost();
    try {
      $userModel->insert($data); // nothing happens ... same thing if I try to use save()
    } catch (\Exception $e) {
      return redirect()->to('/')->with('error'$e->getMessage());
    }

    return redirect()->to('/');
  

This isnt to mention the issues I had trying to use entities... (class not found?????????????)

I really want to enjoy this framework. Help me.
Reply

#2
Check this (http://codeigniter.com/user_guide/models...your-model), and specifically the $allowedFields part.

Also, $data should be formatted in a way that would be accepted (same field names).
Reply

#3
(01-07-2021, 04:35 AM)berendbotje91 Wrote: Check this (http://codeigniter.com/user_guide/models...your-model), and specifically the $allowedFields part.

Also, $data should be formatted in a way that would be accepted (same field names).


This is why I'm confused. Data is accessible through $data using the same field names that $allowedFields expects and yet nothing happens when I try to save it using the provided methods.

Query builder works, as I've already used it to insert data in a seed file. However, I want to avoid using query builder in my controllers considering I've already put in the effort of defining all of my applications models along with validation rules to keep things sane.

edit: figured out a way of doing things where i can still validate user data. now if only i can get entities working properly so that I can use the setter methods I wrote for hashing data instead of having to re-write the code in the controller.
Reply

#4
What is the error you receive?
Plus, check the input - select names in your view and compare them with Modals file and also your database table/columns.
Also, have you ever tried print_r($data) to see your received data?
Reply

#5
(01-08-2021, 08:28 AM)demyr Wrote: What is the error you receive?
Plus, check the input - select names in your view and compare them with Modals file and also your database table/columns.
Also, have you ever tried print_r($data) to see your received data?

Alright a bit of an update on things since I've figured out why I was having issues with entity classes not working (was a filename problem I didnt read the docs closely enough)

Okay, so now I've got data coming in from a POST req, I have already verified that the incoming data structure matches what I'm expecting. I'm able to validate the data, populate my user entity, it uses the setter methods I wrote, all is well here. The snag I'm hitting now is when I go to insert the data to my database.

Using the save/insert method inherited from CodeIgniter\Model returns false, and I can't wrap my head around why. At this point in time, I'm passing the user entity object to that save/insert method, which should automatically convert my data to an array. I tried doing that part myself by calling toArray() on my entity, and have verified that that does not do anything unexpected either and my data remains intact. Still no luck with insert or save.

Nothing explicitly throws an error, if something is failing (which it is) then it's doing so silently.
Reply

#6
Are you sure the data is not rejected by your validation rules? What does $model->errors() returns?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply

#7
I suggest that you check in the Model, protected $allowed Fields = [], if the names are correct with those in the $data array. If the name in the Model is different, wrong, the system does not send an error message because it does not do this verification. I was once told that it would impact performance.
Reply

#8
(01-09-2021, 07:21 AM)includebeer Wrote: Are you sure the data is not rejected by your validation rules? What does $model->errors() returns?




This was one of my first ideas and unfortunately it was not the source of my issue, though it did give me a bit of reassurance that I'm not completely awful at this whole programming thing when I saw that they were working as intended.








For the other response below yours -- I triple checked to make sure I had the exact field names defined in $allowedFields as the keys for the data coming from my POST request. It wouldn't be the first time a typo has killed me, but it's not happening here so we can rule that out too.





After checking out the source code for CodeIgniter\Model, it would seem there's an unimplemented method possibly interrupting code execution, and any error this causes isn't being handled properly and thus fails silently. That's my best guess at least. https://ibb.co/ZTBscWt
Reply

#9
(01-10-2021, 12:02 AM)cantinsertdata Wrote: After checking out the source code for CodeIgniter\Model, it would seem there's an unimplemented method possibly interrupting code execution, and any error this causes isn't being handled properly and thus fails silently. That's my best guess at least. https://ibb.co/ZTBscWt

Which version of CodeIgniter 4 are you using? I use 4.0.4 and the code you are showing in that image is at row 760 in my case, not at 715.
Reply

#10
(01-11-2021, 12:37 AM)berendbotje91 Wrote:
(01-10-2021, 12:02 AM)cantinsertdata Wrote: After checking out the source code for CodeIgniter\Model, it would seem there's an unimplemented method possibly interrupting code execution, and any error this causes isn't being handled properly and thus fails silently. That's my best guess at least. https://ibb.co/ZTBscWt

Which version of CodeIgniter 4 are you using? I use 4.0.4 and the code you are showing in that image is at row 760 in my case, not at 715.

I am also using 4.0.4. I'll run composer update and see if anything changes.

edit: partial output from composer update
Code:
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove

*shrug*
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.