CodeIgniter Forums
the created model does not work 'User' - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: the created model does not work 'User' (/showthread.php?tid=73541)



the created model does not work 'User' - videoproc - 05-07-2019

Hello, friends!

dump: 
Code:
CREATE TABLE IF NOT EXISTS `user` (
 `id_user` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `login` char(50) DEFAULT NULL,
 `name` char(50) DEFAULT NULL,
 `email` char(50) DEFAULT NULL,
 `password` char(255) DEFAULT NULL,
 `ip` char(15) DEFAULT NULL,
 `created_at` int(10) unsigned NOT NULL DEFAULT '0',
 `updated_at` int(10) unsigned NOT NULL DEFAULT '0',  
 PRIMARY KEY (`id_user`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

INSERT INTO `user` (`login`, `name`, `email`, `password`, `ip`,`created_at`,`updated_at`) VALUES
('test','Ivan','[email protected]','e1r1e1','10.0.0.1', 1457950515,1457950515);

app\Models\UserModel.php:
Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table      = 'user';
       protected $primaryKey = 'id_user';

       protected $returnType = 'array';
       protected $useSoftDeletes = true;

       protected $allowedFields = ['login','name', 'email'];

       protected $useTimestamps = true;
       protected $createdField  = 'created_at';
       protected $updatedField  = 'updated_at';
        protected $dateFormat     = 'int';
        
       protected $validationRules    = [];
       protected $validationMessages = [];
       protected $skipValidation     = false;
}

 
app\Controllers\Home.php :

Code:
<?php namespace App\Controllers;
// *LINK REDACTED*
use CodeIgniter\Controller;
use App\Models\UserModel;

class Home extends BaseController
{
    
    puplic function index()
    {
       
        $userModel = new UserModel();
        $user_id = 1;
        $user = $userModel->find($user_id);
    }
}

Result:

Whoops!

We seem to have hit a snag. Please try again later...

--------------
Help me!


RE: the created model does not work 'User' - InsiteFX - 05-07-2019

Change id_user to just id I believe that is what it is looking for

Just plain id.


RE: the created model does not work 'User' - videoproc - 05-07-2019

Understood! id_user nothing to do with!

\ Models \ UserModel.php:

protected $useSoftDeletes = false;

or
protected $useSoftDeletes = true;

but add to the database table field:
`deleted` tinyint(1) unsigned NOT NULL DEFAULT '0'


RE: the created model does not work 'User' - MGatner - 05-14-2019

Good find! $useSoftDeletes will always presuppose the `deleted` column. For future troubleshooting, if you get "Whoops! We seem to have hit a snag." you can get to the underlying error by checking the log files (might require enabling logging, https://codeigniter4.github.io/CodeIgniter4/general/logging.html) or by setting your environment to "development" which will display the error directly (https://codeigniter4.github.io/CodeIgniter4/general/environments.html).