Creating Objects
Last updated Apr 12th, 2019 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn moreWhat is an Object?¶
An "Object" in xPDO is simply an abstract, class-based representation of a row in a table in a database. In other words, a row in the table 'cars' would have an xPDO model definition of the 'cars' table, and then you would grab Collections of Objects of each car.
xPDO defines these Objects using the xPDOObject
class.
Creating an Object¶
Creating objects in xPDO utilizes the "newObject" xPDO method.
Let's say we have an object defined in our model of class "Box". We want to create a new object of it:
$myBox = $xpdo->newObject('Box');
It's that simple. We can also create the Box object with some pre-filled field values:
$myBox = $xpdo->newObject('Box',array(
'width' => 5,
'height' => 12,
'color' => 'red',
));
You cannot set primary key values when using the second parameter of newObject()
. Set the primary key values using fromArray()
after creating the instance with the newObject()
and make sure you set the parameter setPrimaryKeys equal to true.
This will give us an xPDOObject-based Box object that can be manipulated and saved. Note that this Object is not yet persistent until you save it using xPDOObject.save.
In versions prior to xPDO 2.1.1-pl, if your SQL table does not exist for the object you've created, and the object class has a defined table for that class, xPDO will automatically create the table in the database for you. In 2.1.1-pl and later versions, you must set xPDO::OPT_AUTO_CREATE_TABLES
to true to have tables created automatically. It is recommended that you create the tables for your model explicitly in a setup script rather than depending on the auto table creation features that were not optional in earlier releases of xPDO. See xPDOManager.createObjectContainer for information on explicitly creating tables from the model.
See Also¶
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn more