⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Got Error Set custom Model



nungadi28

nungadi28
  • profile picture
  • Member

Posted 07 May 2018 - 04:38 AM


A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Cust_proj::__construct(), called in C:\xampp\htdocs\newcargo\system\core\Loader.php on line 353 and defined

Filename: models/Cust_Proj.php

Line Number: 10

Backtrace:

File: C:\xampp\htdocs\newcargo\application\models\Cust_Proj.php
Line: 10
Function: _error_handler

File: C:\xampp\htdocs\newcargo\application\controllers\TransProject.php
Line: 13
Function: model

File: C:\xampp\htdocs\newcargo\index.php
Line: 315
Function: require_once
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: databaseConfig

Filename: models/Cust_Proj.php

Line Number: 12

Backtrace:

File: C:\xampp\htdocs\newcargo\application\models\Cust_Proj.php
Line: 12
Function: _error_handler

File: C:\xampp\htdocs\newcargo\application\controllers\TransProject.php
Line: 13
Function: model

File: C:\xampp\htdocs\newcargo\index.php
Line: 315
Function: require_once

My Model

   
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    use GroceryCrud\Core\Model;
    use GroceryCrud\Core\Model\ModelFieldType;
    class  Cust_proj extends Model {
     
   protected $ci;
   protected $db;

    function __construct($databaseConfig) {
        
        $this->setDatabaseConnection($databaseConfig);

        $this->ci = & get_instance();
        $this->db = $this->ci->db;
    }

    public function getFieldTypes($tableName)
    {
        $fieldTypes = parent::getFieldTypes($tableName);

        $fullNameFieldType = new ModelFieldType();
        $fullNameFieldType->dataType = 'varchar';

        $countOrdersFieldType = new ModelFieldType();
        $countOrdersFieldType->dataType = 'varchar';

        $fieldTypes['barang'] = $fullNameFieldType;
        $fieldTypes['qty'] = $countOrdersFieldType;

        return $fieldTypes;
    }

    public function getOne($id)
    {
        $customer = parent::getOne($id);

        $this->db->select('COUNT(*) as qty');
        $this->db->where('id_project', $id);
        $customer['qty'] = $this->db->get('tbl_item_project')->row()->count_orders;

        return $customer;
    }

    public function getList() {

        $order_by = $this->orderBy;
        $sorting = $this->sorting;

        // All the custom stuff here
        $this->db->select('tbl_item_project.id_customer, barang, harga, qty, COUNT(*) as qty', false);
        $this->db->join('tbl_item_project', 'tbl_data_project.id_project = tbl_item_project.id_project', 'left');
        $this->db->group_by('tbl_item_project.id_project');

        if ($order_by !== null) {
            $this->db->order_by($order_by. " " . $sorting);
        }

        if (!empty($this->_filters)) {
            foreach ($this->_filters as $filter_name => $filter_value) {
                $this->db->like($filter_name, $filter_value);
            }
        }

        if (!empty($this->_filters_or)) {
            foreach ($this->_filters_or as $filter_name => $filter_value) {
                $this->db->or_like($filter_name, $filter_value);
            }
        }

        $this->db->limit($this->limit, ($this->limit * ($this->page - 1)));
        $results = $this->db->get($this->tableName)->result_array();

        return $results;

    }

    public function getTotalItems()
    {
        return parent::getTotalItems();
    }
               }
      
    ?>

MY Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/libraries/BaseController.php';
include(APPPATH . 'libraries/GroceryCrudEnterprise/autoload.php');
use GroceryCrud\Core\GroceryCrud;

class TransProject extends BaseController {

	public function __construct()
	{
		parent::__construct();
		$this->isLoggedIn(); 
		$this->load->database();
		$this->load->model('cust_proj');
		$this->load->helper('url');
	}

	public function project_out($output = null)
	{
		if (isset($output->isJSONResponse) && $output->isJSONResponse) {
            header('Content-Type: application/json; charset=utf-8');
            echo $output->output;
            exit;
		}
		$this->global['pageTitle'] = 'Aplikai Cargo : Project Data';
        $this->loadViews("sethal/project_data", $this->global,(array)$output);
	}

	public function index()
	{
		$this->loadThis();
		$this->project_out((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
	}



	public function setProject()
	{
		include(APPPATH . 'models/Cust_proj.php');
         	 $this->load->database();
         	 $this->load->model('cust_proj');
             $model = new \Cust_proj($db);
             $crud->setModel($model);
             $crud->setTable('tbl_data_project');
	}
}

dlaynes

dlaynes
  • profile picture
  • Member

Posted 08 May 2018 - 01:00 AM

I believe you are required to pass the database configuration when creating a new custom model. So, you shouldn't load it as a Codeigniter model, since it is an external class or library.

dbarros

dbarros
  • profile picture
  • Member

Posted 15 May 2018 - 12:30 PM

I believe you are required to pass the database configuration when creating a new custom model. So, you shouldn't load it as a Codeigniter model, since it is an external class or library.

Agreed with @dlaynes  statement. 

 

Look at the code below.  This additional function has to be added on your controller. 

 private function _getDbData(){

    return [
        'adapter' => [
            'driver' => 'Pdo_Mysql',
            'database' => 'db_name',
            'username' => 'db_username',
            'password' => 'db_password',
            'charset' => 'utf8'
        ]
    ];

}


public function setProject()
{
    include(APPPATH . 'models/Cust_proj.php');
// $this->load->database();
// $this->load->model('cust_proj');
    $model = new \Cust_proj($this->_getDbData);
    $crud->setModel($model);
    $crud->setTable('tbl_data_project');
}

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 19 May 2018 - 07:14 AM

Hello @dbarros,

 

Did the suggestion of @dlaynes fixed your issue? If yes, can you please share the whole controller as a file so others can also see the solution to your problem?

Regards

Johnny