在Codeigniter中使用Doctrine ORM时,我收到以下错误消息。
( ! ) Fatal error: Call to a member function getAttribute() on a non-object in C:\xampp\htdocs\giftshoes\system\database\doctrine\Doctrine\Record.php on line 1424
Call Stack
# Time Memory Function Location
1 0.0011 327560 {main}( ) ..\index.php:0
2 0.0363 3210720 require_once( 'C:\xampp\htdocs\giftshoes\system\codeigniter\CodeIgniter.php' ) ..\index.php:116
3 0.0492 3922368 Welcome->Welcome( ) ..\CodeIgniter.php:201
4 0.0817 6234096 CI_Loader->model( ) ..\welcome.php:14
5 0.0824 6248376 Shoes->__construct( ) ..\Loader.php:184
6 0.0824 6248424 Doctrine_Core::getTable( ) ..\Shoes.php:5
7 0.0824 6248424 Doctrine_Connection->getTable( ) ..\Core.php:1080
8 0.0824 6254304 Doctrine_Table->__construct( ) ..\Connection.php:1123
9 0.0841 6396128 Doctrine_Table->initDefinition( ) ..\Table.php:249
10 0.0841 6397472 Shoes->__construct( ) ..\Table.php:301
11 0.0841 6397680 Doctrine_Access->__set( ) ..\Access.php:0
12 0.0841 6397680 Doctrine_Record->set( ) ..\Access.php:60
------------------ Doctrine Table Definition -------------
abstract class BaseShoes extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('shoes');
$this->hasColumn('sku', 'integer', 11, array('primary' => true, 'autoincrement' => false));
$this->hasColumn('name', 'string', 255);
$this->hasColumn('keywords', 'string', 255);
$this->hasColumn('description', 'string');
$this->hasColumn('manufacturer', 'string', 20);
$this->hasColumn('sale_price', 'double');
$this->hasColumn('price', 'double');
$this->hasColumn('url', 'string');
$this->hasColumn('image', 'string');
$this->hasColumn('category', 'string', 50);
}
public function setUp() {
}
}
------------------------ Doctrine Table Code ------------------- < / p>
class ShoesTable extends Doctrine_Table
{
function getAllShoes($from = 0, $total = 15)
{
$q = Doctrine_Query::create()
->from('Shoes s')
->limit($total)
->offset($from);
return $q->execute(array(), Doctrine::HYDRATE_ARRAY);
}
}
-----------------型号代码-----------------
class Shoes extends BaseShoes
{
function __construct() {
$this->table = Doctrine::getTable('shoes');
}
public function getAllShoes()
{
$this->table->getAllShoes();
}
}
答案 0 :(得分:3)
我想:
Shoes extends BaseShoes
- 好吧,我们可以看到BaseShoes extends Doctrine_Record
Doctrine_Record
有一个__construct()
方法,可以做很多事情。
如果在其中一个类中重新定义__construct()
方法,它将覆盖在其父类中定义的__construct()
方法。
在这里:
Shoes::__construct()
方法BaseShoes::__construct()
Doctrine_Record::__construct()
未经测试,但在您自己的构造函数中调用父的构造函数可能有所帮助:
class Shoes extends BaseShoes
{
function __construct() {
parent::__construct();
$this->table = Doctrine::getTable('shoes');
}
public function getAllShoes()
{
$this->table->getAllShoes();
}
}
并且,作为参考,引用PHP手册的Constructors and Destructors页面:
注意:如果子类,则不会隐式调用父构造函数 定义构造函数。
为了 运行父构造函数,调用parent::__construct()
内的Doctrine_Record::__construct()
子构造函数是必需的。
不过,作为一个旁注,我不确定在一个Model类中使用Doctrine定义自己的构造函数是一个好主意 - 而且我从未见过这样做,实际上,据我记得...... / p>
这是一个blog-post on the Doctrine's blog,似乎表明我是对的(引用,强调我的):
[...] [...]有人问起这个问题 Doctrine 2中实体的构造函数 以及是否可以使用它。
我认为这是值得的 自从Doctrine 1中的开始写作 这是不可能的。构造函数 从你那里得到了高兴的使用 内部由Doctrine 。
以下是Doctrine 1.2手册的相关部分:Overriding the Constructor:
Doctrine不允许你覆盖 {{1}} 方法但提供了另一种选择
[...]