Ticket Details

Can't extend a base model

BUG Ticket (closed)

I've created a base model for my models like this:

{{{
namespace app\models;

class App extends \lithium\data\Model {
    public static function __init(array $options = array()) {
        parent::__init($options);
    }
}
}}}

And extended it like this:

{{{
namespace app\models;

class User extends App {
    public static function __init(array $options = array()) {
        parent::__init($options);
    }
}
}}}

**I'm using MongoDb**

###The Problem:

You would expect that the model will handle the users collection,
but instead, it selects an **apps** collection. When you debug the
$class variable @ \lithium\data\Model::__init() [line 114], it prints out 'app\models\App'

###Test

I've made a small test... Don't know if it's relevant:

{{{
class A {
	public static function get() {
		var_dump(get_called_class());
		var_dump(__CLASS__);
	}
}

class B extends A { }

class C extends B { }

C::get();
}}}

The result is:

{{{
string 'C' (length=1)

string 'A' (length=1)
}}}

###Hack

After some digging, here is a very hacky way to make it work:

On \lithium\data\model\Query line 91 change this...

{{{
$this->_config['table'] = $model::meta('source');
}}}

... into this...

{{{
$this->_config['table'] = \lithium\util\Inflector::tableize(join('', array_slice(explode("\\", $model), -1)));
}}}
on 03.10.10 reported by: eberfreitas owned by: nate

Updates

on 03.10.10 by eberfreitas
  • description was changed
on 03.10.10 by eberfreitas
  • version was changed to lithium-0.7
  • description was changed
on 03.31.10 by websta
Just a note - if you add this to the top of your ___init, or more correctly your config method as of 0.8, in your base model you'll prevent it overriding the settings resulting in your error. 

{{{
if (($class = get_called_class()) == __CLASS__) {
	return;
}
}}}

This code is lifted straight from the ```Model``` class and prevents it doing precisely the same thing. So maybe not really a bug after all...
(fixed) on 04.19.10 by nate
  • owner was changed to nate
  • status was changed to closed
  • resolution was changed to fixed
Fixed in [5566de901019ee3caa084cfb28beacf7ec0aba4b]. Models can now inherit configuration from any number of base classes. Thanks for the report!