Welcome Guest, Not a member yet? Register   Sign In
Insert in database return false without error
#1

I have this table:


Code:
accounts:

    - id : int - primary key
    - email : varchar(255) - unique
    - pass : varchar(255)




I have an entity like this:

PHP Code:
<?php namespace App\Entities;

use 
CodeIgniter\Entity;

class 
Account extends Entity{

    private $id;
    private $email;
    private $pass;

    function __construct($email$pass) {

        parent::__construct();

        $this->email $email;
        $this->pass $pass;

    }

    public function getId(){ return $this->id; }

    public function getEmail(){ return $this->email; }

    public function getPass(){ return $this->pass; }

    public function setPass($newPass){ $this->pass $newPass; }






And model like this:



PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
AccountModel extends Model{

    protected $table 'accounts';
    protected $primaryKey 'id';
    protected $allowedFields = ['pass'];
    protected $returnType 'App\Entities\Account';





Now I try to create a new account and insert it in database.




This is controller:


PHP Code:
<?php namespace App\Controllers;

use 
App\Models\AccountUtil;

class 
Tester extends BaseController{

    public function index(){

        var_dump(AccountUtil::createAccount("[email protected]""mypass"));

    }





This is AccountUtil:


PHP Code:
<?php namespace App\Models;

use 
App\Entities\Account;
use 
CodeIgniter\Model;
use 
Exception;

class 
AccountUtil extends Model{

    public static function createAccount($email$pass){

        try{

            if(empty($email) || empty($pass)){
                throw new Exception("credentials_empty(" $email ")(" $pass ")");
            }

            if(!self::isEmailAvailable($email)){
                throw new Exception("email_not_available(" $email ")");
            }

            $newAccount = new Account($email$pass);

            $accountModel = new AccountModel();

            $result $accountModel->insert($newAccounttrue);

            return $result;

        }catch(Exception $e){
            log_message("error"$e);
            return false;
        }

    }

    public static function isEmailAvailable($email){

        try{

            $result = (new AccountModel())->where("email"$email)->countAllResults();
            if($result == 0){
                return true;
            }else{
                return false;
            }

        }catch(Exception $e){
            log_message("error"$e);
            return false;
        }

    }



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?
Reply
#2

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
Reply
#3

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB