Welcome Guest, Not a member yet? Register   Sign In
Problem with Model()->find() and Model()->findAll()
#5

(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 nullValidationInterface $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;
    }


Reply


Messages In This Thread
RE: Problem with Model()->find() and Model()->findAll() - by workoverflow - 10-06-2019, 02:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB