[英]kohana error when model instantiation
每当我尝试实例化某个模型并使用它时,例如:
$categories = Model::factory('category')->by_sale($id)->find_all();
我收到一个奇怪的错误。 如果我在kohana :: init中设置了'errors'=> TRUE,那么错误是: Could not execute Model_Category::__construct()
否则我只会收到Warning: array_keys() expects parameter 1 to be array, null given in /application/classes/model.php on line 42
在这里的意思是:
private function _get_real_property_name($property)
{
if (isset($this->_belongs_to[$property]) OR
isset($this->_has_one[$property]) OR
isset($this->_has_many[$property]))
return $property;
$column_prefix = $this->_table_name . '_';
$property_prefix = substr($property, 0, strlen($column_prefix));
if ($property_prefix != $column_prefix)
{
$prefixed_property = $column_prefix . $property;
if (in_array($prefixed_property, array_keys($this->table_columns())))
{
return $prefixed_property;
}
}
return $property;
}
类别模型如下所示:
class Model_Category extends Model {
/**
* @see ORM::_table_name
*
* @var array
*/
protected $_table_name = 'category';
/**
* @see ORM::_primary_key
*
* @var array
*/
protected $_primary_key = 'category_id';
/**
* @see ORM::_belongs_to
*
* @var array
*/
protected $_belongs_to = array(
'parent' => array('model' => 'category', 'foreign_key' => 'category_category'),
'sale' => array('foreign_key' => 'category_sale')
);
/**
* @see ORM::_has_many
*
* @var array
*/
protected $_has_many = array(
'products' => array('model' => 'product', 'foreign_key' => 'product_category')
);
/**
* Adds the 'top_level' condition to the query
*
* @return Model_Sale
*/
public function top_level()
{
return $this->where('category_category', '=', 0);
}
/**
* Adds the 'by_sale' condition to the query
*
* @return Model_Sale
*/
public function by_sale($sale_id)
{
return $this->where('category_sale', '=', $sale_id);
}
public function __get($property)
{
if ($property == 'siblings')
{
return $this->where('category_sale', '=', $this->sale->id)
->where('category_category', '=', $this->category_category);
}
if ($property == 'children')
{
return $this->where('category_category', '=', $this->pk());
}
return parent::__get($property);
}
} // End Model_Category
谢谢!
我不认识$this->table_columns()
。 当查看http://kohanaframework.org/3.0/guide/api/Model时,我也看不到它。
您是否已将此方法自己添加到Model类中? 我猜它没有返回正确的类型。
我对Kohana不熟悉,但是与Codeigniter非常相似。 您可以在扩展模型类的模型中尝试以下操作:
函数__construct(){parent :: Model(); }(最终传递您拥有的参数)
尝试更换$this->table_columns()
由$this->table_columns
(在_get_real_property_name()
因为table_columns
似乎是一个变量,而不是一个方法:3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.