We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24719
    • 194 Posts
    Hi,

    I’ve created a few objects that use inheritance:

    Person -> User -> Advertiser
    -> Member
    -> Admin

    I have to use ’include_once’ in the class files of child objects, or xPDO complains that it can’t find the parent class file. Is there something I’m missing? I notice that the 0.9.7 class files don’t have to do this.

    I’m using a recent version of xPDO - just checked out yesterday.

    Also, I’ve forgotten - will xPDO automatically generate tables for objects the first time an object is saved? I’m getting the following error:

    [2007-05-30 14:27:51] (DEBUG @ /index.php) Returning table name: `member` for class: Member 
    [2007-05-30 14:27:51] (DEBUG @ /index.php) No primary key value found for pk definition: 
    [2007-05-30 14:27:51] (DEBUG @ /index.php) Executing SQL: INSERT INTO `member` (`dob`, `gender`) VALUES (:dob, :gender) with bindings: Array ( [:dob] => Array ( [value] => [type] => 1 ) [:gender] => Array ( [value] => NULL [type] => 0 ) ) 
    
    Fatal error: Call to a member function execute() on a non-object in C:\wamp\www\xpdo\xpdo\om\xpdoobject.class.php on line 945


    My tables don’t exist, but i’m getting the "No primary key value found" error I’ve had before (on a different project). I fixed it last time by inserting my own definition for the id fields into the schema since it wasn’t being inherited properly from the base class for some reason.

    My basic schema is:

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="test" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
        <object class="Advertiser" table="advertiser" extends="User">
    		<field key="company" dbtype="varchar" precision="50" phptype="string" null="true" />
    		<field key="city" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="phone" dbtype="varchar" precision="30" phptype="string" null="true" default="" />
        </object>
        <object class="Member" table="member" extends="User">
    		<field key="dob" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" default="" />
    		<field key="gender" dbtype="enum" precision="'M','F'" phptype="string" null="true" />
        </object>
        <object class="Person" table="person" extends="xPDOObject">
    		<field key="firstname" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
    		<field key="lastname" dbtype="varchar" precision="200" phptype="string" null="false" default="" />
    		<field key="email" dbtype="varchar" precision="60" phptype="string" null="false" default="" />
        </object>
        <object class="User" extends="Person">
        </object>
    </model>
    


    If I do have to create the tables myself first, do I have to create a ’user’ table? And do I need any additional fields to the ones in my schema? How does xPDO keep track of which rows in the different tables make up any particular object of a sub-class?

    Thanks in advance
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: redman at May 30, 2007, 08:51 AM

      I have to use ’include_once’ in the class files of child objects, or xPDO complains that it can’t find the parent class file. Is there something I’m missing? I notice that the 0.9.7 class files don’t have to do this.
      0.9.7 classes that depend on inheritance do require either include/require_once or somewhere in your code, you will need to call $xpdo->loadClass() specifically on the parent class(es) before trying to load a child class. I tried to leave this open so users can decide how to deal with that on their own. Using __autoload() is another possibility for future releases...

      Quote from: redman at May 30, 2007, 08:51 AM

      Also, I’ve forgotten - will xPDO automatically generate tables for objects the first time an object is saved?
      Only if you attempt to retrieve a value before saving one. I need to add logic to save() and some other methods to force the table creation when they don’t exist.

      Quote from: redman at May 30, 2007, 08:51 AM

      I’m getting the following error:

      Fatal error: Call to a member function execute() on a non-object in C:\wamp\www\xpdo\xpdo\om\xpdoobject.class.php on line 945

      My tables don’t exist, but i’m getting the "No primary key value found" error I’ve had before (on a different project). I fixed it last time by inserting my own definition for the id fields into the schema since it wasn’t being inherited properly from the base class for some reason.
      You should create the tables first for now. It’s very easy to do...
      <?php
      $manager= $xpdo->getManager();
      $manager->createObjectContainer('MyClass');
      $manager->createObjectContainer('AnotherClass');
      ?>


      Quote from: redman at May 30, 2007, 08:51 AM

      If I do have to create the tables myself first, do I have to create a ’user’ table? And do I need any additional fields to the ones in my schema? How does xPDO keep track of which rows in the different tables make up any particular object of a sub-class?
      You don’t have to create tables for abstract classes, no, but which model of inheritence are you using? Are you using single-table inheritence or multi-table inheritence? And xPDO just keeps track of each classes fields, period. Handling inheritence is up to you (otherwise I would have to force particular inheritence models and exclude others). The classes define their fields and inheritence will simply aggregate those fields.
        • 24719
        • 194 Posts
        ok, thanks a lot
          • 16511
          • 16 Posts
          Quote from: OpenGeek at May 30, 2007, 10:21 AM

          0.9.7 classes that depend on inheritance do require either include/require_once or somewhere in your code, you will need to call $xpdo->loadClass() specifically on the parent class(es) before trying to load a child class.

          Is this still true in xPDO 2.0?
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: kyank at Aug 17, 2011, 04:52 AM

            Quote from: OpenGeek at May 30, 2007, 10:21 AM

            0.9.7 classes that depend on inheritance do require either include/require_once or somewhere in your code, you will need to call $xpdo->loadClass() specifically on the parent class(es) before trying to load a child class.

            Is this still true in xPDO 2.0?
            Yes.
              • 16511
              • 16 Posts
              OK, I’ve solved this by putting this line at the top of the dependent model class files:
               if (!class_exists('myBaseModel')) require_once 'myBaseModel.class.php';