Welcome Guest, Not a member yet? Register   Sign In
Model not using DBGroup
#1

I searched the forums but it does not appear anyone else is having this issue.

I am using the myth-auth module to provide authentication to my system.  To keep things logically separated, I have a separate database for the authentication module and a separate database for my data.  I have configure the myth-auth module to use the 'default' database group, and I'm trying to configure CodeIgniter 4 to use another  database for the data.  Here is what I have.

.env
Code:
database.default.hostname = localhost
database.default.database = users
database.default.username = mysql
database.default.password = password
database.default.DBDriver = MySQLi

database.publications.hostname = localhost
database.publications.database = publications
database.publications.username = mysql
database.publications.password = password
database.publications.DBDriver = MySQLi

/app/Config/Database.php
PHP Code:
public $default = [
 
'DSN'      => '',
 
'hostname' => 'localhost',
 
'username' => 'mysql',
 
'password' => 'password',
 
'database' => 'users',
 
'DBDriver' => 'MySQLi',
 
'DBPrefix' => '',
 
'pConnect' => false,
 
'DBDebug'  => (ENVIRONMENT !== 'production'),
 
'charset'  => 'utf8',
 
'DBCollat' => 'utf8_general_ci',
 
'swapPre'  => '',
 
'encrypt'  => false,
 
'compress' => false,
 
'strictOn' => false,
 
'failover' => [],
 
'port'    => 3306,
 ];
 public 
$publications = [
 
'DSN'      => '',
 
'hostname' => 'localhost',
 
'username' => 'mysql',
 
'password' => 'password',
 
'database' => 'publications',
 
'DBDriver' => 'MySQLi',
 
'DBPrefix' => '',
 
'pConnect' => false,
 
'DBDebug'  => (ENVIRONMENT !== 'production'),
 
'charset'  => 'utf8',
 
'DBCollat' => 'utf8_general_ci',
 
'swapPre'  => '',
 
'encrypt'  => false,
 
'compress' => false,
 
'strictOn' => false,
 
'failover' => [],
 
'port'    => 3306,
 ]; 

/app/Models/FiscalYearModel.php
PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
FiscalYearModel extends Model {
  protected $DBGroup  'publications';
  protected $table "FiscalYears";
  protected $primaryKey "FiscalYearID";
  protected $allowedFields = ["FiscalYear"];

  public function getFiscalYear($fiscalYearID) {
    return $this->find($fiscalYearID);
  }

  public function getDBGroup() {
    return $this->DBGroup;
  }

Error Message
mysqli_sql_exception #1146

Table 'users.FiscalYears' doesn't exist
Reply
#2

I did test on my code, it work perfectly with DBGroup,
better check your db connection, seem like the connection fallback to default. maybe failed to connect to db publications.
Reply
#3

As far as I can tell the database connection is working correctly.  For instance in my controller I can use the following code and get data returned.
PHP Code:
public function index() {

    $db = \Config\Database::connect('publications');
    $builder $db->table('FiscalYears');
    $builder->select("*");
    $fy $builder->get()->getResult();

    // Populate the data going to the view
    $data = [
      'fiscalYears' => $fy,
      'title' => 'Fiscal Years',
    ];

    // Generate the view
    echo view('Myth\Auth\Views\layout');
    echo view('FiscalYears/index.php'$data);
  
Reply
#4

(This post was last modified: 07-30-2021, 09:01 PM by InsiteFX.)

make sure the name is spelled correct and that the case is correct.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

I have triple checked the names, and they are identical.  I even went so far as to copy paste database names.  I also created a brand new CI project that does not use the myth-auth module just in case this was causing a conflict somehow.  But I still encounter the same error.

Where does the group name for $DBGroup in the model correspond to?  Does it correspond to the App\Config\Database.php or the .env file?
Reply
#6

(This post was last modified: 07-31-2021, 09:26 AM by nfaiz. Edit Reason: Add and edit example )

Do you have any other folder in app/Models?

how do you call the model in controller?

PHP Code:
$model = new App\Models\FiscalYears(); 

or

PHP Code:
$model model(FiscalYears::class); 

I don't know is it a bug or not, but we can't use a sub-directory/namespace using model() factories with a same class name.

---- Edit (Add example)---

E.g User Model
PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
User extends Model
{
  protected $DBGroup 'default';
  protected $table 'user';
  protected $primaryKey 'id';


Another User Model
PHP Code:
<?php

namespace App\Models\Admin;

use 
CodeIgniter\Model;

class 
User extends Model
{
  protected $DBGroup 'project';
  protected $table 'user';
  protected $primaryKey 'id';


User Controller
PHP Code:
<?php

namespace App\Controllers;

use 
App\Controllers\BaseController;

class 
User extends BaseController
{
  public function index()
  {
    // No issue
    $user model('User')->findAll();

    // No issue
    $user model('App\Models\User')->findAll();

    // No issue
    $user model(App\Models\User::class)->findAll();

    // This will use app/Models/User.php and DBGroup default is used instead of project.
    $user model('App\Models\Admin\User')->findAll();

    // This will use app/Models/User.php and DBGroup default is used instead of project.
    $user model(App\Models\Admin\User::class)->findAll();
  }

Reply
#7

Can you share the full stack trace?
Reply
#8

Try renaming your .env file to env and see if it will pull thedatabase config settings.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB