Thanks bob, I see exactly what should be happening now & as far as I can tell everything I have is correct with the exception of possibly the schema, here's what I have, the comments tell the story, but basically addOne will work, while addMany will not!
public function createNewContract($scriptProperties, & $hook){
$formfields = $hook->getValues();
echo '<pre>';print_r($formfields);echo '</pre>';
$newContract = $this->modx->newObject('Contract');
foreach($formfields as $key => $value){
$newContract->set($key,$value);
}
$contractBudgets = array();
foreach($formfields['budget_type_id'] as $value){
$newBudget = $this->modx->newObject('ContractBudget');
$newBudget->set('budget_type_id',$value);
$newBudget->set('budget_f1',$value);
$newBudget->set('budget_f2',$value);
// I don't have to set this, it get automatically picked up from the newContract object
$newBudget->set('contract_id',999);
$contractBudgets[] = $newBudget;
}
// this works, shows correct count of array of objects
echo '<br>'.count($contractBudgets);
// works, I can extract the correct data from each object
echo '<br> budget_f2 0 = '.$contractBudgets[0]->get('budget_f2');
echo '<br> budget_f2 1 = '.$contractBudgets[1]->get('budget_f2');
echo '<br> budget_f2 2 = '.$contractBudgets[2]->get('budget_f2');
// does not work, no records are added to the contrac_budget table.
$newContract->addMany($contractBudgets);
// this will add a contract budget but gives the error
// (ERROR @ /index.php) ContractBudget: Attempt to set NOT NULL field contract_id to NULL
$newContract->addOne($newBudget);
// a new contract is saved however!?
if(!$newContract->save()){
$mess = $this->modx->lexicon('modflowthrough.add_Contract_failed');
$this->modx->setPlaceholder('message_error',$mess);
return false;
}else{
$mess = $this->modx->lexicon('modflowthrough.add_Contract_success');
$this->modx->setPlaceholder('message_success',$mess);
}
exit();
return true;
} // end createNewContract
And the appropriate part of the schema:
<object class="ContractBudget" table="contract_budget" extends="xPDOSimpleObject">
<field key="created" dbtype="timestamp" phptype="timestamp" null="true" default="CURRENT_TIMESTAMP" extra="on update current_timestamp" />
<field key="modified" dbtype="timestamp" phptype="timestamp" null="true" />
<field key="budget_type_id" dbtype="int" precision="8" phptype="integer" null="false" index="index" />
<field key="budget_f1" dbtype="decimal" precision="12,2" phptype="float" null="false" />
<field key="budget_f2" dbtype="decimal" precision="12,2" phptype="float" null="false" />
<field key="comment" dbtype="longtext" phptype="string" null="true" />
<!-- a budget may only belong to one contract -->
<field key="contract_id" dbtype="int" precision="8" phptype="integer" null="false" index="index" />
<aggregate alias="Contract" class="Contract" local="contract_id" foreign="id" cardinality="many" owner="foreign" />
</object>
<object class="Contract" table="contract" extends="xPDOSimpleObject">
<field key="created" dbtype="timestamp" phptype="timestamp" null="false" default="CURRENT_TIMESTAMP" extra="on update current_timestamp" />
<field key="modified" dbtype="timestamp" phptype="timestamp" null="false" default="0000-00-00 00:00:00" />
<field key="status_id" dbtype="int" precision="8" phptype="integer" null="true" index="index" />
<field key="location_id" dbtype="int" precision="8" phptype="integer" null="false" />
<field key="client_id" dbtype="int" precision="8" phptype="integer" null="false" />
<field key="employer_id" dbtype="int" precision="8" phptype="integer" null="false" />
<field key="program_id" dbtype="int" precision="8" phptype="integer" null="false" />
<field key="requester_uid" dbtype="int" precision="8" phptype="integer" null="false" />
<field key="approver_uid" dbtype="int" precision="8" phptype="integer" null="false" />
<field key="start_date" dbtype="date" phptype="date" null="false" />
<field key="end_date" dbtype="date" phptype="date" null="false" />
<field key="start_balance_f1" dbtype="decimal" precision="12,2" phptype="float" null="false" />
<field key="start_balance_f2" dbtype="decimal" precision="12,2" phptype="float" null="false" />
<field key="end_balance" dbtype="decimal" precision="12,2" phptype="float" null="true" />
<field key="comment" dbtype="longtext" phptype="string" null="true" />
<aggregate alias="StatusFlag" class="StatusFlag" local="status_id" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Location" class="Location" local="location_id" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Client" class="Client" local="client_id" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Employer" class="Employer" local="employer_id" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Program" class="Program" local="program_id" foreign="id" cardinality="one" owner="foreign" />
<!-- contracts may have many budgets -->
<composite alias="ContractBudget" class="ContractBudget" local="id" foreign="contract_id" cardinality="one" owner="local" />
</object>
so after all that, my error *HAS* to be in the schema? Correct? everything works correctly and checks out except when it comes time to addMany... ?