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!
-
MODX Staff
- 10,725 Posts
Sounds like you do not have a primary key defined properly on your table.
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.
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
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.
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
-
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.