-
workoverflow Newbie

-
Posts: 5
Threads: 1
Joined: Oct 2019
Reputation:
0
10-06-2019, 12:03 PM
(This post was last modified: 10-06-2019, 02:50 PM by workoverflow.)
Hi! I have faced with problem with Model()->find() and Model()->findAll(). If i call ones of this methods i get null. It's doesn't work after update to 4.0.0-rc.2.1 with composer.
For example, i have model UserModel which based on BaseModel which extends Model.
PHP Code: class UserModel extends BaseModel {
private $user_uploads_dir;
private $user_avatar_uri;
protected $table = 'users';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $useTimestamps = true;
protected $dateFormat = 'int';
protected $createdField = 'created';
protected $updatedField = null;
protected $deletedField = 'deleted';
protected $useSoftDeletes = true;
}
Table users has 1 row with id = 1
When i try to get row from DB with find() or findAll() i get Null result. I don't known why it's happening.
PHP Code: $items = (new UserModel())->find(1); // will be null
PHP Code: $items = (new UserModel())->where('id', 1)->get()->getRowArray(); // will be array with user data
Tell me, please, what i doing wrong? I created issue on github.com, but jimm-parry redirect me to this forum.
-
dave friend Posting Freak
    
-
Posts: 1,015
Threads: 15
Joined: Jun 2015
Reputation:
50
10-06-2019, 01:22 PM
(This post was last modified: 10-06-2019, 01:25 PM by dave friend.)
Tell us about BaseModel. Does it extend CodeIgniter\Model? What are your namespace statements?
(10-06-2019, 01:01 PM)ciadmin Wrote: Model already has $table and $primaryKey properties. Shouldn't your model have a constructor, where it sets those?
Code: public function __construct() {
parent::__construct();
$this->table = 'users';
}
You don't initialize the $DBGroup property, which might affect your outcome.
The docs show assignments to those and other Model properties in the class definition. Since the values are not typically used in a dynamic way there is no reason not to do it that way.
-
workoverflow Newbie

-
Posts: 5
Threads: 1
Joined: Oct 2019
Reputation:
0
10-06-2019, 02:42 PM
(This post was last modified: 10-07-2019, 11:55 PM by workoverflow.)
(10-06-2019, 01:22 PM)dave friend Wrote: Tell us about BaseModel. Does it extend CodeIgniter\Model? What are your namespace statements?
(10-06-2019, 01:01 PM)ciadmin Wrote: Model already has $table and $primaryKey properties. Shouldn't your model have a constructor, where it sets those?
Code: public function __construct() {
parent::__construct();
$this->table = 'users';
}
You don't initialize the $DBGroup property, which might affect your outcome.
The docs show assignments to those and other Model properties in the class definition. Since the values are not typically used in a dynamic way there is no reason not to do it that way.
I have app/Models/BaseModel which extends CodeIgniter\Model and contains some methods specific for my project.
PHP Code: namespace App\Models;
use App\Models\Main\UserModel; use Carbon\Carbon; use CodeIgniter\Model; use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Validation\ValidationInterface; use Config\App; use Config\Services;
class BaseModel extends Model {
/** * @var \CodeIgniter\Session\Session */ public $session;
/** * @var $locale */ public $locale;
/** * @var string|string[] */ public $lang;
/** * Meta params for AJAX requests * @var array */ public $meta = [];
public function __construct(ConnectionInterface &$db = null, ValidationInterface $validation = null) { parent::__construct($db, $validation); $this->session = \Config\Services::session(); $this->locale = $this->getUserLang(); $this->lang = Services::language($this->locale)->getLine('app.lang', []); } }
All my model extends BaseModel, and before update to last version I don't call __construct() in sub classes.
For example, i put code of one class which building menu. I rewrited it with MenuModel()->get()->getResutArray(), because findAll() don't work after all.
PHP Code: <?php
namespace App\Models\Main;
use App\Models\BaseModel;
class MenuModel extends BaseModel {
protected $table = 'menu';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $deletedField = 'deleted';
/** * Get list of all category and subcategory * @return array|bool */ public function getMenu() { if($data = $items = $this->select("id, url, module, icon, title_{$this->locale} title, parent") ->where('deleted', 0) ->orderBy('order', 'ASC') ->findAll() ) { $result = []; $user_access = (new AccessModel())->getUserAccess((new UserModel())->id());
array_map(function($item) use(&$result, &$user_access) { if (boolval($item['parent'])) { if (array_key_exists($item['parent'], $result)) { $result[$item['parent']]['child'][$item['id']] = $item; } else { foreach ($result as &$value) { if(isset($value['child']) && array_key_exists($item['parent'], $value['child'])) { $value['child'][$item['parent']]['child'][] = $item; } } } }else{ if ($user_access[$item['module']]){ $result[$item['id']] = $item; } } }, $data); return $result; } return false; }
}
-
workoverflow Newbie

-
Posts: 5
Threads: 1
Joined: Oct 2019
Reputation:
0
10-06-2019, 02:49 PM
(This post was last modified: 10-06-2019, 02:57 PM by workoverflow.)
(10-06-2019, 01:04 PM)ciadmin Wrote: More thoughts:
1) I presume UserModel is namespaced, and that you have a something like "use App\Models\UserModel;"
2) The primary key is supposed to be a string, and you are passing an int. Should there be some casting?
1) All my models are in namespace app/Models/Main
2) I use protected $primaryKey = 'id'; it's not a int
I known, you guess that i'm dummy, but i'm sure that something happened with Model. I have used it without any problems yesterday.
-
workoverflow Newbie

-
Posts: 5
Threads: 1
Joined: Oct 2019
Reputation:
0
(10-06-2019, 04:04 PM)ciadmin Wrote: The last update to Model was on Sept 29th.
Did you update your CI4 framework yesterday? How have you installed it? (manual, composer framework, composer appstarter)
I use composer for update project dependencies.
-
workoverflow Newbie

-
Posts: 5
Threads: 1
Joined: Oct 2019
Reputation:
0
(10-08-2019, 06:17 AM)MGatner Wrote: @workoverflow Composer will update against various sources depending on where you installed it from. Open composer.json and verify your CodeIgniter dependency looks something like this:
"codeigniter4/codeigniter4": "dev-develop"
This is my current config of Composer.
Code: "require": {
"php": ">=7.2",
"codeigniter4/framework": "v4.0.0-rc.2.1",
"codeigniter4/translations": "v4.0.0-rc.2",
"claviska/simpleimage": "~3.3",
"guzzlehttp/guzzle": "~6.3",
"smarty/smarty": "~3.1",
"nesbot/carbon": "^2",
"phpoffice/phpspreadsheet": "^1.9"
},
"require-dev": {
"mikey179/vfsstream": "1.6.*",
"phpunit/phpunit": "^7.0"
},
"config": {
"optimize-autoloader": true
}
|