模型实例化时出现 kohana 错误

发布于 2024-10-28 00:58:36 字数 2717 浏览 5 评论 0原文

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

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

我收到一个奇怪的错误。如果我在 kohana::init 中引导,设置 'errors' => TRUE,错误是:无法执行 Model_Category::__construct() 否则我只收到警告警告:array_keys() 期望参数 1 为数组,在 /application/classes 中给出 null /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

谢谢!

Every time I try to instantiate a certain model and use it, like for example:

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

I get a weird error. If i have i bootstrap in kohana::init set 'errors' => TRUE,the error is: Could not execute Model_Category::__construct() else i get only a warning Warning: array_keys() expects parameter 1 to be array, null given in /application/classes/model.php on line 42

meaning here:

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;
}

The category model looks like this:

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

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海少女心 2024-11-04 00:58:36

我不认识 $this->table_columns()。当查看 http://kohanaframework.org/3.0/guide/api/Model 我那里也看不到它。

您自己将此方法添加到 Model 类中了吗?我猜它没有返回正确的类型。

I don't recognize the $this->table_columns(). When looking at http://kohanaframework.org/3.0/guide/api/Model I don't see it there either.

Have you added this method to the Model class yourself? I'm guessing it's not returning the proper type.

琉璃繁缕 2024-11-04 00:58:36

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

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

I am not familiar with Kohana, but it is quite resembling with Codeigniter. You can try this in your model that extends model class:

function __construct(){
parent::Model();
}
(eventually pass the parameters that you have)

任性一次 2024-11-04 00:58:36

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

Try replacing $this->table_columns() by $this->table_columns (in _get_real_property_name()) because table_columns seems to be a variable, not a method :3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文