我有一个非常基本的设置,我试图在CodeIgniter中测试PHP ActiveRecord。这是我的设置:
文件名是product.php:
class Product extends ActiveRecord\Model{
static $has_many = array(
array('skus')
);
}
文件名是sku.php:
class Sku extends ActiveRecord\Model{
}
我的控制器有以下代码:
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);
}
}
当我调用测试控制器的索引操作时,我得到以下输出:
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
有谁知道这里的问题是什么?我不明白为什么活跃的记录试图加载" Skus" (复数)类,而不是" Sku"类。根据PHP ActiveRecord文档,您应该使用$ has_many声明的复数名称。
另外,为了澄清一点,CodeIgniter能够正确地找到Sku模型,如果我不是在" has_many"的上下文中使用它。另一种模式的关系。例如,如果我加载的模型$this->load->model('sku')
正常工作,那么我不认为从CodeIgniter角度看Sku类有什么问题。
答案 0 :(得分:0)
请参阅此网址以获取更多帮助 http://ellislab.com/codeigniter/user-guide/database/active_record.html
Class Product extends CI_Model{ function getbyid($id){ $this->db->select ( '*' ); $this->db->from ( 'user_table' ); $this->db->where ( 'user_id', $userID ); $query = $this->db->get (); return $query->result (); } }
这些是codeigniter的基本活动记录