[TYPO3-mvc] [Howto] Inject own model in existing extension by using fake singleTableInheritence

Franz Koch typo3.RemoveForMessage at elements-net.de
Thu Sep 2 22:00:34 CEST 2010


Hey there,

I just needed to find a way to extend the model of a foreign extension 
with custom properties and tell the other extension to use my model 
instead of the default base model. After digging around the source and 
examine all the mapping functions I didn't find a easy way/patch to add 
this feature - so I searched for a little hack and found 
singleTableInheritence to be perfect for this. You now might ask how, 
and that's what this posting is about.


1: Create your custom model extending the base model that you like to 
replace

2: Configure TS mapping for your model
config.tx_extbase.persistence.classes {
	Tx_YourExt_Domain_Model_CustomModel {
		mapping {
			tableName = tx_otherext_domain_model_basemodel
		}
	}
}

3: Prepare for singleTableInheritence
And here comes the trick. Map both classes to the same record type and 
you're ready to go. If your basemodel doesn't have a record type, it's 
not a problem at all, see step 4. But first the TS extended with 
singleTableInheritence:

config.tx_extbase.persistence.classes {
	Tx_YourExt_Domain_Model_CustomModel {
		mapping {
			tableName = tx_otherext_domain_model_basemodel
			recordType = 0
		}
	}
	Tx_OtherExt_Domain_Model_BaseModel {
		mapping {
			recordType = 0
		}
		subclasses {
			0 = Tx_YourExt_Domain_Model_CustomModel
		}
	}
}

4: if not available, add fake type field to the TCA
In my case, the other extension didn't have a type field or something 
that I could have used for it. At least that's what I thought at first - 
but then I thought - why shouldn't I simply use the "deleted" field for 
that as those don't get rendered anyway, and the deleted value is always 
0 then. So after adding the following line in my ext_tables.php I 
finally seamlessly injected my custom model to a foreign extension:

$GLOBALS['TCA']['tx_otherext_domain_model_basemodel']['ctrl']['type'] = 
'deleted';



I know that this is a reeeeeeealy ugly hack and we should aim for a 
better solution with a pimped objectManager using DI or whatever - but 
until this one is ready the hack might come handy for some of you.

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list