We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50227
    • 18 Posts
    MODX Revolution 2.3.3-pl (traditional)
    Hi
    I'm having problems deleting records from the child table (FEELPUPILSCHARTS) when a record is deleted from the parent table (PUPIL). This is my schema:
    <!--PUPIL-->
    <object class="Pupil" table="pupil" extends="xPDOObject">
    	<field key="Id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
    	<field key="username" dbtype="char" precision="100" phptype="string" null="false" default="" />
    	<field key="password" dbtype="char" precision="20" phptype="string" null="false" default="pupil1" />
    	<field key="fname" dbtype="varchar" precision="20" phptype="string" null="true" />
    	<field key="sname" dbtype="varchar" precision="30" phptype="string" null="true" />
    	<field key="Level" dbtype="char" precision="50" phptype="string" null="false" default="pupil" />
    	<field key="active" dbtype="int" precision="11" phptype="integer" null="false" default="1" />
    	<field key="showgraphs" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    	<field key="user_id" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    
    	<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
    		<column key="Id" length="" collation="A" null="false" />
    	</index>
    	<aggregate alias="Myusers" class="Myusers" local="Id" foreign="Id" cardinality="one" owner="foreign" />
    	<composite alias="FeelPupilsCharts" class="FeelPupilsCharts" local="Id" foreign="pupcht_pupil_id" cardinality="many" owner="local" />
    </object>
    
    <!--FEELPUPILSCHARTS-->
    <object class="FeelPupilsCharts" table="feel_pupils_charts" extends="xPDOObject">
    	<field key="pupcht_Id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
    	<field key="pupcht_title" dbtype="varchar" precision="255" phptype="string" null="true" default="Chart Title" />
    	<field key="pupcht_act_id" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    	<field key="pupcht_pupil_id" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    	<field key="pupcht_chart_id" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    	<field key="pupcht_user_id" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    	<field key="pupcht_showgraphs" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    	<field key="pupcht_showcomments" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    	<field key="pupcht_img_left" dbtype="varchar" precision="100" phptype="string" null="true" default="0" />
    	<field key="pupcht_img_mid" dbtype="varchar" precision="100" phptype="string" null="false" default="0" />
    	<field key="pupcht_img_right" dbtype="varchar" precision="100" phptype="string" null="false" default="0" />
    
    	<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
    		<column key="pupcht_Id" length="" collation="A" null="false" />
    	</index>
    	<aggregate alias="Pupil" class="Pupil" local="pupcht_pupil_id" foreign="Id" cardinality="one" owner="foreign" />
    	<aggregate alias="UsersActivities" class="UsersActivities" local="pupcht_act_id" foreign="ua_act_id" cardinality="one" owner="foreign" />
    	<aggregate alias="UsersActivities" class="UsersActivities" local="pupcht_user_id" foreign="ua_user_id" cardinality="one" owner="foreign" />
    	<composite alias="Feeling" class="Feeling" local="pupcht_Id" foreign="feel_chart_id" cardinality="many" owner="local" />
    </object>
    


    The pupil record is deleted but not the corresponding records in FeelPupilsCharts.
    The error log is showing:
    [2015-04-06 15:07:26] (ERROR @ /index.php) Error removing dependent object: Array
    (
    [pupcht_Id] => 16
    [pupcht_title] => wibble pupil 1 chart 1
    [pupcht_act_id] => 2
    [pupcht_pupil_id] => 12
    [pupcht_chart_id] => 0
    [pupcht_user_id] => 23
    [pupcht_showgraphs] => 0
    [pupcht_showcomments] => 0
    [pupcht_img_left] => 0
    [pupcht_img_mid] => 0
    [pupcht_img_right] => 0
    )

    I read a post about a similar issue saying that a PK was needed in the tables which I have so I'm stumped as to what the problem is????
    Any help would be muchly appreciated.

    Thanks in advance smiley

    This question has been answered by goldsky. See the first response.

      • 3749
      • 24,544 Posts
      I don't see the Feeling object schema.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 50227
        • 18 Posts
        Quote from: BobRay at Apr 06, 2015, 12:03 PM
        I don't see the Feeling object schema.
        Hi BobRay - Thanks for the reply

        I've only included the scheema code for the 2 relevant tables - I can include it if needed. Querying the tables and saving works fine - its just the delete that doesn't want to behave :-(
          • 3749
          • 24,544 Posts
          Sorry, I misread what wasn't being removed.

          Have you maybe changed the schema without regenerating the class and map files? xPDO ignores the schema itself.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            What does this return if you run it from a snippet (make sure your package is loaded)?

            var_dump($modx->getPK('FeelPupilsChart'));


            Not really related to your question, but there's no real need prefix every column in a table with the same prefix (pupcht). When you're grabbing data you can instead do
            $feelPupilsChart->toArray('myprefix.')
            to return unique placeholders. Maybe it's a matter of preference, but I like my database tables clean laugh
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • 50227
              • 18 Posts
              I've rebuilt my custom tables and created the foriegn keys with cascade update and delete, rewritten my scheema and generated the model. I've run a test snippet on two tables (PUPIL -> PUPILCHARTS) and the delete child record works. But it also deletes the related child record in PUPILCHARTS child table FEELING (PUPIL -> PUPILCHARTS -> FEELING) for which I haven't written the compsosite/aggregate declarations in my scheema??? I guess that is because of the cascade delete on the foriegn keys? Basic question - before I rip it all apart again can you tell me please if I need to define the foriegn keys in the DB tables and if so should the action be set to update/delete or to no action?

              :-)
                • 3749
                • 24,544 Posts
                If you're redoing things, I'd recommend removing the Id field from the schema and extending xPDOSimpleObject. That will give you an auto-increment id field automatically.

                I'd also suggest doing things the way MODX does, with the main object having just the ID field and the child objects having a separate field (not the pk) called internalKey which holds the ID of the parent object. (See the MODX schema, e.g., modUser and modUserProfile).
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 50227
                  • 18 Posts
                  Thanks BobRay - that makes a lot of sense but I think I need to go away and do some serious head scratching as I don't really have a clue - hence doing it the simple way I've been trying. I'll be back...........................
                    • 50227
                    • 18 Posts
                    I've looked into BobRay's advice but haven't tried it yet, I want to make sure I understand whats happening with my current setup before moving forward in case there are other underlying problems so I'd appreciate it if you could have a look at what I've now done to try and resolve this.
                    I've atatched detailed info but basically what I've done is to drop my existing tables and scheema/maps and created a series of 5 simple tables (I think I read somewhere about not using plural names for tables???) with no joins or FK:
                    Tutor -> Pupil -> Chart -> Feeling -> Period

                    If I delete a Tutor - linked Pupil's are NOT deleted
                    If I delete a Pupil - linked Charts's are NOT deleted
                    If I delete a Chart - linked Feeling's AND Period's ARE deleted.

                    I've included scheema, test data, reuslts and error log info - I'd REALLY appreciate it if you guys could have a look and see if there are any obvious issues with what I've done.

                    I've run BobRays' site checker and the only issue was "MySQL and PHP date and time do not match"
                    When I bought the hosting it came with softaculous and an install for MODX which is what I used, I've since read that this can cause problems later on???????
                    MODX Revolution 2.3.3-pl (traditional)
                    Apache Version 2.2.29
                    PHP Version 5.4.38
                    MySQL Version 5.5.42-cll
                    Architecture x86_64
                    Operating System linux

                    Thanks VERY much in advance smiley
                      • 22303 MODX Staff
                      • 10,725 Posts
                      I don't see anything obviously wrong here. This may be entirely related to the use of a mixed cased field name for "Id", but I would have to test the theory to be sure.