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($newAccount, true);
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?