Skip to content

Instantly share code, notes, and snippets.

@timaschew
Created September 28, 2012 14:16
Show Gist options
  • Save timaschew/3800132 to your computer and use it in GitHub Desktop.
Save timaschew/3800132 to your computer and use it in GitHub Desktop.
patch file for using foreign keys to non pk reference (only for ManyToOne)
diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
index d73983c..9f8a253 100644
--- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
+++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
@@ -598,10 +598,18 @@ class BasicEntityPersister
$quotedColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->_class, $this->_platform);
$this->quotedColumns[$sourceColumn] = $quotedColumn;
-
- if ($newVal === null) {
- $result[$owningTable][$sourceColumn] = null;
- } else if ($targetClass->containsForeignIdentifier) {
+ if ($newVal === null) {
+ $result[$owningTable][$sourceColumn] = null;
+ $this->_columnTypes[$sourceColumn] = $targetClass->getTypeOfColumn($targetColumn);
+ return $result;
+ }
+ // $newVal is not null
+ // NEW FEATURE: undefined index (foreign keys to non pk columns)
+ // add the real field name and value, which is used for the assoc.
+ // otherwise this only works if $targetColumn is the primary key and nothing else
+ $fieldName = $targetClass->getFieldName($targetColumn);
+ $newValId[$fieldName] = $targetClass->getFieldValue($newVal, $fieldName);
+ if ($targetClass->containsForeignIdentifier) {
$result[$owningTable][$sourceColumn] = $newValId[$targetClass->getFieldForColumn($targetColumn)];
} else {
$result[$owningTable][$sourceColumn] = $newValId[$targetClass->fieldNames[$targetColumn]];
@rifeman2007
Copy link

Is there a patch for one is one mapping?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment