We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38117
    • 11 Posts
    ModX Revolution 2.2-advanced
    PHP 5.2/5.3 (tested using both)
    MySQL 5.x
    LAMP
    Windows XP/Windows 7 Pro x64

    The code:

    $myEntity=$modx->getObject('myObj',$id);
    $myEntity->remove();

    The xPDO model for myObj is so simple that no quote is needed. No relationship, just one table with some fields.

    After the execution of the code i get my MySQL table completely truncated. Not only the entity with id=$id is removed but every record in the table.

    The $id is correct. The /core/cache/logs/error.log contains nothing related to the issue. Tried to find a way to log or trace the SQL command created by object->remove() but could not find a way to do it.

    Need help. Thanks in advance!
      • 22303 MODX Staff
      • 10,725 Posts
      Sounds like you do not have a primary key defined properly on your table.
        • 38117
        • 11 Posts
        My table was created manually but it has a primary key "id" as other ModX tables.

        Found another problem:

        $myEntity=$modx->newObject('myObj',$data);
        $myEntity->save();

        In the case that no SQL table is created for storing myObj'ects this portion of code cannot create this table. After this operation the /core/cache/logs/error.log contains the SQL error "table _tablename does not exist". But as far as I understand the xPDO concepts the "$myEntity->save();" should create the table.

        We could suppose that there's a problem with the model and build script. Don't think this is the case. The save() function works as it is intended: it creates new records and updates the existing records in the table.
          • 3548
          • 102 Posts
          First post: Do some debugging, check what $myEntity contains before remove...

          Second post: $myEntity->save(); xpdo does not create table if it does not exist (it did in earlier versions). You can still set it up to work that way by setting xPDO::OPT_AUTO_CREATE_TABLES
            Antonio Zdilar
            linearvector.com
            • 38117
            • 11 Posts
            Drilled down into xpdo.class.php . Code from line 1551 and below:

            $delete= $this->xpdo->newQuery($this->_class);
            $delete->command('DELETE');
            $delete->where($pk);

            Added some code to prevent the query from execution and to display some debug info. The $pk ("primary key" for sure) has correct value but the resulting SQL statement is like "DELETE from _tablename". No "where" part! Of course this SQL command deletes all records from the table.

            Another portion of code from xpdo.class.php near the deletion part of code:

            print_r($stmt->errorInfo(), true));

            This returns the following:

            Array
            (
            [0] =>
            )

            Think it's no error at all.
              • 3548
              • 102 Posts
              Is your pk/id autoincrement value? Check your objects classes that they have all the necessary fields. And do post your model.
                Antonio Zdilar
                linearvector.com
                • 38117
                • 11 Posts
                OK, here's my model.

                <?xml version="1.0" encoding="UTF-8"?>
                <model package="shopLinks" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
                	<object class="shopLinks" table="shopLinks" extends="xPDOSimpleObject">
                		<field key="id" dbtype="int" precision="10" phptype="int" null="false" index="index"/>
                		<field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
                		<field key="url" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                		<field key="resources" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>
                		<field key="level" dbtype="int" precision="2" phptype="int" null="false" default="1" />
                		<field key="description" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                		<field key="tags" dbtype="text" precision="65535" phptype="string" null="false" default="" />
                		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE">
                			<column key="id" length="10" collation="A" null="false" />
                		</index>
                	</object>
                </model>
                

                Now I'm almost sure there's a problem with indexes in shopLinks table and/or its PDO representation. Tried some different XML models (with 'index="index'' or 'index="pk"' , 'generated="native"' and so on) but the result is always the same: no WHERE clause in the resulting SQL query. [ed. note: tiareteg last edited this post 14 years, 7 months ago.]
                  • 22303 MODX Staff
                  • 10,725 Posts
                  You extended xPDOSimpleObject which includes the id definition and primary key index definition. Take those out of your model as they are inherited.
                    • 38117
                    • 11 Posts
                    Quote from: opengeek at Feb 23, 2012, 02:40 PM
                    You extended xPDOSimpleObject which includes the id definition and primary key index definition. Take those out of your model as they are inherited.

                    OpenGeek, You're a magician. It's working now. Can't believe my eyes. Was struck on this problem during a whole working day. Thanks a lot!

                    P.S. It's however strange that redefining id and index for xPDOSimpleObject leads to such unexpected problems. Not a bug but, i think, an inconvenience.