Insert in database return false without error |
I have this table:
Code: accounts: I have an entity like this: PHP Code: <?php namespace App\Entities; And model like this: PHP Code: <?php namespace App\Models; Now I try to create a new account and insert it in database. This is controller: PHP Code: <?php namespace App\Controllers; This is AccountUtil: PHP Code: <?php namespace App\Models; I go in browser at : `localhost/myproj/tester` and it's printed on page `bool(false)`. If I look on logs, I have just this: `INFO - 2020-03-23 12:35:53 --> Controller "App\Controllers\Tester" loaded.` Why I get `false`, but no error?
Could you drop the catch block and figure out exactly where the FALSE is coming from? If it is indeed from the model try checking $accountModel->errors() for anything helpful. You might also review your log settings while you're in there. https://codeigniter4.github.io/userguide...gging.html
(03-23-2020, 09:42 AM)MGatner Wrote: Could you drop the catch block and figure out exactly where the FALSE is coming from? If it is indeed from the model try checking $accountModel->errors() for anything helpful. You might also review your log settings while you're in there. https://codeigniter4.github.io/userguide...gging.html Log is setted right. I removed try-catch and same result and $accountModel->errors() is null. After some research, I found that in `$accountModel->insert(...)`call `classToArray`. That `classToArray` check `if(method_exists($data, 'toRawArray'))`, which will call that method to get my object as an raw array. Super class of `Account` is `Entity`(why? I don't know, I just did copy-paste). `Entity` has `toRawArray`, to `toRawArray` return an empty raw array(no attributes). The way is to override `toRawArray` in `Account`, but I have no reason to keep `extends Entity`. I removed `extends Entity`, but that still didn't solved the problem. Attributes of `Account` are private, and the `classToArray` collect only public and protected attributes, so I made attributes protected and now everything works fine. |
Welcome Guest, Not a member yet? Register Sign In |