[TYPO3-mvc] sub-models within the model table

Jochen Rau jochen.rau at typoplanet.de
Mon Apr 19 10:20:55 CEST 2010


Hi Masi.

On 18.04.10 22:27, Martin Kutschker wrote:
> Let's assume we have a customer model. It has two addresses one for shipping and one for billing. We
> further assume that we don't need tha address on it's own (quite likely in a shop application) and
> so we don't want to create a separate address table.
>
> The table looks like this:
>
> id
> name
> shipping_address
> shipping_postcode
> shipping_city
> billing_address
> billing_postcode
> billing_city
>
> My idea is now to model those two addresses as models with the customer model rather then a bunch of
> unrelated properties of the customer. If the address sub-model is general enough it could be reused
> in any other model (and table).
>
> Do you think this kind of setup makses sense?

Yep. This fits perfect in the concept of Single Table Inheritance. You 
might want to reuse the table of tt_address for storing your data. You 
have to extend this table with a "type" field. I suggest to use 
"tx_extbase_type", but you are free to choose here.

CREATE TABLE tt_address (
	tx_extbase_type varchar(255)
);

Create three Domain Models:

Tx_MyExt_Domain_Model_Address (not really necessary but helpful)

Tx_MyExt_Domain_Model_ShippingAddress extends Tx_MyExt_Domain_Model_Address

Tx_MyExt_Domain_Model_BillingAddress extends Tx_MyExt_Domain_Model_Address


Add the selection to tt_address:

$tempColumns = array(
	'tx_extbase_type' => array(
		'exclude' => 1,
		'label'   => 
'LLL:EXT:myext/Resources/Private/Language/locallang_db.xml:tt_address.tx_extbase_type',
		'config' => array(
			'type' => 'select',
			'items' => array(
				array('undefined', '0'),
				array('Billing Address', 'Tx_MyExt_Domain_Model_BillingAddress')
			),
				array('Shipping Address', 'Tx_MyExt_Domain_Model_ShippingAddress')
			),
			'size' => 1,
			'maxitems' => 1,
			'default' => '0'
		)
	)
);
t3lib_div::loadTCA('tt_address');
t3lib_extMgm::addTCAcolumns('tt_address', $tempColumns, 1);
t3lib_extMgm::addToAllTCAtypes('tt_address', 'tx_extbase_type');
$TCA['tt_address']['ctrl']['type'] = 'tx_extbase_type';
$TCA['tt_address']['types']['Tx_MyExt_Domain_Model_BillingAddress'] = 
$TCA['tt_address']['types']['0'];
$TCA['tt_address']['types']['Tx_MyExt_Domain_Model_ShippingAddress'] = 
$TCA['tt_address']['types']['0'];

Define your mapping rule:

config.tx_extbase {
	persistence{
		classes {
			Tx_MyExt_Domain_Model_Address {
				subclasses {
					Tx_MyExt_Domain_Model_BillingAddress = 
Tx_MyExt_Domain_Model_BillingAddress
					Tx_MyExt_Domain_Model_ShippingAddress = 
Tx_MyExt_Domain_Model_ShippingAddress
				}
				mapping {
					tableName = tt_address
				}
			}
			Tx_MyExt_Domain_Model_BillingAddress {
				mapping {
					tableName = tt_address
				}
			}
			Tx_MyExt_Domain_Model_BillingAddress {
				mapping {
					tableName = tt_address
				}
			}
			Tx_MyExt_Domain_Model_ShippingAddress {
				mapping {
					tableName = tt_address
				}
			}
		}
	}
}

You can now have a Tx_MyExt_Domain_Repository_AddressRepository to fetch 
Addresses of any type or more specialized repositories 
Tx_MyExt_Domain_Repository_ShippingAddressRepository and 
Tx_MyExt_Domain_Repository_BillingAddressRepository.

This config should work but is not tested (+1 by writing and reading ;-) ).

Regards
Jochen


More information about the TYPO3-project-typo3v4mvc mailing list