Welcome Guest, Not a member yet? Register   Sign In
PHP ActiveRecord and CodeIgniter Basic Setup
#1

(This post was last modified: 04-18-2023, 12:17 AM by VeroniTasingolba.)

I have a pretty basic setup that I am trying to use to test PHP ActiveRecord within online2yu CodeIgniter. Here's my setup:

Filename is product.php:

class Product extends ActiveRecord\Model{

        static $has_many = array(
                array('skus')
        );

}
Filename is sku.php:

class Sku extends ActiveRecord\Model{

}
My controller has the following code:

class Test extends CI_Controller {

        public function __construct(){
                parent::__construct();
                $this->load->model('sku');
                $this->load->model('product');
        }

        public function index(){
                $product = Product::first();
                print_r($product);
        }
}
When I call the index action of the test controller, I get the following output:

PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class Skus does not exist' in /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Reflections.php:33
Stack trace:
#0 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Reflections.php(33): ReflectionClass->__construct('Skus')
#1 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Relationship.php(286): ActiveRecord\Reflections->add('Skus')
#2 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Relationship.php(281): ActiveRecord\AbstractRelationship->set_class_name('Skus')
#3 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Relationship.php(464): ActiveRecord\AbstractRelationship->set_inferred_class_name()
#4 /var/www/domain.com/i.domain.com/sparks/php-a in /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Reflections.php on line 33

Fatal error: Uncaught exception 'ReflectionException' with message 'Class Skus does not exist' in /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Reflections.php:33
Stack trace:
#0 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Reflections.php(33): ReflectionClass->__construct('Skus')
#1 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Relationship.php(286): ActiveRecord\Reflections->add('Skus')
#2 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Relationship.php(281): ActiveRecord\AbstractRelationship->set_class_name('Skus')
#3 /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Relationship.php(464): ActiveRecord\AbstractRelationship->set_inferred_class_name()
#4 /var/www/domain.com/i.domain.com/sparks/php-a in /var/www/domain.com/i.domain.com/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Reflections.php on line 33
Does anyone know what the issue could be here? I don't understand why active record is trying to load the "Skus" (plural) class, rather than the "Sku" class. According to the PHP ActiveRecord docs, you should be using the plural name for the $has_many declaration.

Also, just to clarify, CodeIgniter is able to correctly find the Sku model if I use it not in the context of a "has_many" relationship from another model. For example, if I load the model with $this->load->model('sku') that works correctly, so I don't think there is anything wrong with the Sku class from a CodeIgniter omeglz perspective.
Reply


Messages In This Thread
PHP ActiveRecord and CodeIgniter Basic Setup - by VeroniTasingolba - 02-01-2023, 09:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB