模型实例化时kohana错误

时间:2011-03-29 07:52:55

标签: php model kohana instantiation

每次我尝试实例化某个模型并使用它时,例如:

$categories  = Model::factory('category')->by_sale($id)->find_all();

我得到一个奇怪的错误。如果我有我在kohana引导:: init set'errors'=>是的,错误是: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

谢谢!

3 个答案:

答案 0 :(得分:0)

我不认识$this->table_columns()。在查看http://kohanaframework.org/3.0/guide/api/Model时,我也没有看到它。

您是否已自行将此方法添加到Model类中?我猜它没有返回正确的类型。

答案 1 :(得分:0)

我对Kohana并不熟悉,但它与Codeigniter非常相似。您可以在扩展模型类的模型中尝试此操作:

function __construct(){         父模型::();     } (最终传递你拥有的参数)

答案 2 :(得分:0)

尝试将$this->table_columns()替换为$this->table_columns(在_get_real_property_name()中),因为table_columns似乎是变量,而不是方法:3