xPDOForeignKeyConstraint
Last updated Oct 17th, 2026 | 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
$194 per month—let's make that $500!
Learn moreWhat Does the Rule Do?¶
This rule tests the validity of a foreign key for a defined relationship. It resolves the relationship from the <aggregate> or <composite> declared on the object, so the rule only needs the alias of that relationship; it does not read foreign, local or class attributes of its own.
Using the Rule¶
Here we want to make sure no category ID is assigned to our object that doesn’t have a category that exists.
First, our model:
<model package="Test" baseClass="xPDO\Om\xPDOObject" platform="mysql"
defaultEngine="InnoDB" version="3.0">
<object class="myTest" table="test" extends="xPDO\Om\xPDOSimpleObject">
<field key="category" dbtype="int" precision="10" attributes="unsigned"
phptype="integer" default="0" null="false" index="index" />
<validation>
<rule field="category"
name="preventBlank"
type="xPDOValidationRule"
rule="xPDOForeignKeyConstraint"
alias="Category"
message="The category specified does not exist."
/>
</validation>
<aggregate alias="Category" class="MODX\Revolution\modCategory"
local="category" foreign="id"
cardinality="one" owner="foreign" />
</object>
</model>
From there, go ahead and generate the model from the XML schema. And now in a Snippet we’ll call Test:
$output = '';
$modx->addPackage('Test', '/path/to/components/test/model/');
$obj = $modx->newObject('Test\myTest');
$obj->set('category', 123);
$validator = $obj->getValidator();
if (!$validator->validate()) {
foreach ($validator->getMessages() as $errorMsg) {
$output .= $errorMsg['message'];
}
}
return $output;
This will display, assuming that a category doesn’t exist with ID ‘123’:
The category specified does not exist.
See Also¶
- xPDOForeignKeyConstraint
- xPDOMaxLengthValidationRule
- xPDOMaxValueValidationRule
- xPDOMinLengthValidationRule
- xPDOMinValueValidationRule
- xPDOObjectExistsValidationRule
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
$194 per month—let's make that $500!
Learn more










