0

I have been trying to use Mongodb with CodeIgniter 4 but facing issues.

puppyModel.php -

<?php

namespace App\Models;
use CodeIgniter\Model;
use App\Libraries\Mongo;

class puppyModel{
    /**
     * @var Mongo
     */
    protected $m;

    /**
     *
     */
    public function __construct()
    {
        $this->m = new Mongo();
    }

    public function getList(string $collection, array $where = [], array $options = [], array $select = [])
    {
        return $this->m->options($options)->select($select)->where($where)->find($collection)->toArray();
    }
}

?>

Home.php (Controller) -

<?php

namespace App\Controllers;
use CodeIgniter\Controller;
use app\Models\puppyModel;

class Home extends BaseController{
    public function index(){
        $puppyModel = new puppyModel();
        $data1 = $puppyModel->getList();
        print_r($data1);
    }
}

I am getting this error -

CRITICAL - 2022-02-02 23:58:05 --> Class "app\Models\puppyModel" not found
#0 C:\xampp\htdocs\cimongo\system\CodeIgniter.php(825): App\Controllers\Home->index()
#1 C:\xampp\htdocs\cimongo\system\CodeIgniter.php(412): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Home))
#2 C:\xampp\htdocs\cimongo\system\CodeIgniter.php(320): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#3 C:\xampp\htdocs\cimongo\index.php(37): CodeIgniter\CodeIgniter->run()
#4 {main}

I am using this repo as a reference - https://github.com/bertugfahriozer/ci4mongodblibrary I have already placed the config and library files as in the repo and changed namespaces.

1
  • Your use app in your controller should be use App - see if that fixes it. Commented Feb 7, 2022 at 9:11

3 Answers 3

1

use app\Models\puppyModel" not found

try this one

use App\Models\puppyModel 
Sign up to request clarification or add additional context in comments.

Comments

0

have you tried below:

$puppyModel = model(puppyModel::class);

$result = $puppyModel->getList([your parameters here]);

Comments

-1

I just saw your question. I'm sorry for the late reply.

I think it will work if you edit your file like this:

<?php

namespace App\Models;
use ci4mongodblibrary\Libraries\Mongo;

class puppyModel{

i developing a cms/erp system with this repository. you can check from this link :

https://github.com/ci4-cms-erp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.