Post by Joe1234 » Fri Aug 30, 2024 10:26 pm

I'm getting the following php error:

Code: Select all

PHP Fatal error:  Uncaught Error: Call to a member function model() on null in.............
Library file is located: system\library\library_file.php
The call is: $this->load->model('extension/common/model_file');

I have the library file calling a model function, I verified that the path to the model file is correct.
The model is located: catalog\model\extension\common\model_file.php

And finally I have the registration setup in the catalog/controller/startup with: $this->registry->set('Guardian_SRH', new Guardian_SRH($this->registry));


Full stack trace:

Code: Select all

[30-Aug-2024 00:12:01 America/New_York] PHP Fatal error:  Uncaught Error: Call to a member function model() on null in ............../system/library/library_file.php:48
Stack trace:
#0 ............../catalog/controller/account/login.php(259): Guardian_SRH->tracking('', '', 'Account Login')
#1 ............../catalog/controller/account/login.php(108): ControllerAccountLogin->validate()
#2 ............../system/engine/action.php(79): ControllerAccountLogin->index()
#3 ............../catalog/controller/startup/router.php(25): Action->execute(Object(Registry))
#4 ............../system/engine/action.php(79): ControllerStartupRouter->index()
#5 ............../system/engine/router.php(77): Action->execute(Object(Registry))
#6 ............../system/engine/router.php(66): Router->execute(Object(Action))
#7 ............../system/framework.php(169): Router->dispatch(Object(Action), Object(Action))
#8 ............../system/startup.php(104): require_once('/home/user/...')
#9 ............../index.php(19): start('catalog')
#10 {main}
  thrown in ............../system/library/guardian_srh.php on line 48
Why am I getting this error? Thanks.
Last edited by Joe1234 on Wed Sep 11, 2024 10:30 pm, edited 1 time in total.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by by mona » Fri Aug 30, 2024 11:10 pm


DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by JNeuhoff » Sat Aug 31, 2024 12:03 am

This won't work because your library class can't call $this->load->model(...), it doesn't know about the registry from where it would get the loader.
Your library_file.php would need a constructor and get and set methods like this:

Code: Select all

public class LibraryFile {
    protected $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function __get($key) {
        return $this->registry->get($key);
    }

    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
    ....
}

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Joe1234 » Sat Aug 31, 2024 1:35 am

I'm barely understanding (just learning about libraries). I already have a constructor in my library...but I guess it is clear that I am missing something (I don't see a model file to put in the get parameter):

Code: Select all

		public function __construct($registry) {
			$this->config = $registry->get('config');
			$this->db = $registry->get('db');
			$this->request = $registry->get('request');
			$this->session = $registry->get('session');
			$this->log = $registry->get('log');
		}
I added what you posted, but still nothing.

Also note, I have:

Code: Select all

class LibraryFile {
at the top.
When I change the format to what you posted:

Code: Select all

public class LibraryFile {
I start getting a 500 errror unexpected token public if that matters.


EDIT: I think I fixed it. I had "protected $registry" instead of "private $registry" at the top. Have to do more testing, will report back.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by JNeuhoff » Sat Aug 31, 2024 3:26 pm

Sorry, yes it should start with

Code: Select all

class LibraryFile {

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Joe1234 » Thu Sep 05, 2024 3:08 am

OK the previous issue is resolved. Now the next issue, I'm trying to get customer info, but what I'm trying doesn't seem to be working.

Code: Select all

		private $customer;

		public function __construct($registry) {

			$this->registry = $registry;

			========I tried one of the following==============
			$this->customer = $registry->get('customer');
			$this->customer = $registry->get('cart/customer');
			$this->customer = $registry->get('/cart/customer');

		}

		public function __get($name) {

		   return $this->registry->get($name);

		}
		
		public function something() {
		
			$customer_id = $this->customer->getId();
		
		}
Thanks

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by JNeuhoff » Thu Sep 05, 2024 5:54 pm

It won't be available until after the catalog/controller/startup/startup.php has been executed:

Code: Select all

		// Customer
		$customer = new Cart\Customer($this->registry);
		$this->registry->set('customer', $customer);

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Joe1234 » Fri Sep 06, 2024 11:34 pm

Do you mean the customer registry has to be executed before my library is executed in that startup file? If that's the case, that is what is happening. My library is actually executed at the very end of the file.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by JNeuhoff » Fri Sep 06, 2024 11:40 pm

Strange, it should have worked with:

Code: Select all

$this->customer = $registry->get('customer');
provided it's executed after the startup/startup.php.

Maybe you are not using a proper OpenCart, something bad as the Journal3 framework, or other incompatible extensions. Hard to say without seeing your website.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Joe1234 » Wed Sep 11, 2024 10:29 pm

I reordered the "registry->get" list and so far I don't see any errors, hopefully that fixed it. Thanks.

v3.0.3.9 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am
Who is online

Users browsing this forum: Gpli, Majestic-12 [Bot] and 15 guests