开发者

kohana error when model instantiation

开发者 https://www.devze.com 2023-02-20 06:05 出处:网络
Every time I try to instantiate a certain model and use it, like for example: $categories= Model::factory(\'category\')->by_sale($id)->find_all();

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 $thi开发者_C百科s->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!


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.


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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号