doujiong3146 2011-03-29 07:52
浏览 34
已采纳

模型实例化时kohana错误

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!

  • 写回答

3条回答 默认 最新

  • doulingzou1712 2011-03-29 08:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀