Welcome Guest, Not a member yet? Register   Sign In
models in CI4 - OK to put in app/Models? What about naming/namespacing/autoloading?
#4

(This post was last modified: 12-12-2020, 01:22 AM by tgix.)

My main gripe with CI4's models is that they don't work the way I expect - to represent a single record. 
Maybe I am wrong about how a model should or should not work, but in my other main framework (Ext JS), the Model represents the database record. CI4 models sort of represents the database table. This means that you cannot (at least I haven't found a reasonable way) work on the actual record data in the model-code. 
My models are tied with a corresponding Entity that is returned on a Model::find() and that works fine when accessing and transforming the data of the record. However saving an Entity requires you to use the Model again with a Model::update($id, $data). I have been working on adding CRUD functions directly to the Entity but scrapped that put code in the Controller instead, for example:
PHP Code:
public function delete($id)
    {
        $credits = new ExamTypeModel();
        $data $credits->find($id);
        if (is_null($data)) {
            return $this->failNotFound();
        }

        // Check to see so there are balance on this credit type
        $db db_connect();
        $builder $db->table(Table::credits);
        $company_balance $builder->selectSum('balance''sum')
            ->where('typeLink'$id)
            ->get()
            ->getRowObject(0)->sum;

        $builder $db->table(Table::user_credits);
        $user_balance $builder->selectSum('balance''sum')
            ->where('typeLink'$id)
            ->get()
            ->getRowObject(0)->sum;

        if (((int)$user_balance 0) || ((int)$company_balance 0)) {
            return $this->failValidationError();
        }

        $credits->delete($id);
        return $this->respondNoContent();
    

This snipped I would really like to reduce to:

PHP Code:
    public function delete($id)
    {
        $record = new ExamTypeModel($id);
        if (is_null($record)) {
            return $this->failNotFound();
        }

        if (($record->userBalance() > 0) || ($record->companyBalance() > 0)) {
            return $this->failValidationError();
        }
        
        $record
->delete();
        
        
return $this->respondNoContent();
    

Where userBalance() and companyBalance() would be functions in the Model (or Entity).

Namespacing:
I wouldn't worry too much about this. I use composer to install CI4 and the dependencies I need and so far there have been zero problems (other than me mistyping something). Your code should live in App namespace which by default points to your app/ directory. As long as you follow the PSR-4 specifications and name things properly in the app/ directory you can create anything your like (some restrictions with reserved keywords applies of course). As long as you make sure that the autoloader can find it you will be able to use it.
Folders should be named as the namespace, so a class App\Models\pickers\BrandPickerModel should be defined in a file located in app/Models/pickers/BrandPickerModel.php. Upper/lower case is important, especially if you are on macOS or Windows because the filesystems are case-insensitive which is something that will bite you if your production environment is Linux.
In some places in CI4 the docs tells you type the name of the class as a string, for example when configuring a $returnType of an Entity (https://codeigniter4.github.io/userguide...-the-model). This is error prone and makes refactoring hard. My guess is this is because of not having to autoload everything which would be the case if you were to use ::class instead. I am using ::class in many places where the docs shows a string and haven't seen any issues.
I use PHPstorm IDE and it helps a lot to keep things organized.

https://codeigniter4.github.io/userguide...oader.html
https://www.php-fig.org/psr/psr-4/

My project directory:
   
Reply


Messages In This Thread
RE: models in CI4 - OK to put in app/Models? What about naming/namespacing/autoloading? - by tgix - 12-12-2020, 01:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB