开发者

Fatal error while usin gDoctirne ORM in Codeigniter

开发者 https://www.devze.com 2022-12-26 23:48 出处:网络
I am getting following error message when using Doctrine ORM in Codeigniter. ( ! ) Fatal error: Call to a member function getAttribute() on a non-object in C:\\xampp\\htdocs\\giftshoes\\system\\datab

I am getting following error message when using Doctrine ORM in Codeigniter.

( ! ) 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 -------------------

class ShoesTable extends Doctrine_Table
{
    function getAllShoes($from = 0, $total = 15)
    {
        $q = Doctrine_Query::create()
        ->from('Shoes s')
        ->limit($tot开发者_Go百科al)
        ->offset($from);

        return $q->execute(array(), Doctrine::HYDRATE_ARRAY);
    }

}

-----------------Model Code-----------------

class Shoes extends BaseShoes
{
    function  __construct() {
        $this->table = Doctrine::getTable('shoes');
    }
    public function getAllShoes()
    {
        $this->table->getAllShoes();
    }
}


I suppose that :

  • Shoes extends BaseShoes -- well, we can see that
  • BaseShoes extends Doctrine_Record

Doctrine_Record has a __construct() method, which does a lot of stuff.

If you re-define the __construct() method in one of your classes, it will override the __construct() method that's defined in its parent class.


Here :

  • your Shoes::__construct() method
  • overrides BaseShoes::__construct()
  • which itself doesn't exist, so is the same as Doctrine_Record::__construct()
  • which seems to do some quite important Doctrine-related things ;-)


Not tested, but calling the parent's constructor in your own constructor could help :

class Shoes extends BaseShoes
{
    function  __construct() {
        parent::__construct();
        $this->table = Doctrine::getTable('shoes');
    }
    public function getAllShoes()
    {
        $this->table->getAllShoes();
    }
}


And, as a reference, quoting the Constructors and Destructors page of the PHP manual :

Note: Parent constructors are not called implicitly if the child class defines a constructor.
In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.


Still, as a sidenote, I am not sure that defining your own constructor in a Model class, with Doctrine, is such a good idea -- and I've never seen this done, actually, as far as I remember...

And here's a blog-post on the Doctrine's blog, that seems to indicate I'm right (quoting, emphasis mine) :

[...] someone asked about the constructor of entities in Doctrine 2 and whether or not it could be used.
I think this is something worth writing about since in Doctrine 1 this was not possible. The constructor was hi-jacked from you and used internally by Doctrine.


And here's the section of Doctrine's 1.2 manual that's relevant : Overriding the Constructor :

Doctrine doesn't allow you to override the Doctrine_Record::__construct() method but provides an alternative
[...]

0

精彩评论

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

关注公众号