[TYPO3-mvc] Casting objects
Xavier Perseguers
typo3 at perseguers.ch
Thu Jul 9 08:44:13 CEST 2009
Hi Sebastian,
> that's an interesting use-case! We wanted to write some mixin-support
> somewhen in time, so that you can add functionality and behavior to
> existing domain objects. However, we wanted to prevent overriding methods.
>
> Would the above approach solve your problem as well?
As long as the findOneBy... and other related methods can be extended to
return the new class, it is what I need because in fact I don't want to
override existing methods, just add some functionality.
> For me it would be very helpful if you could explain your exact use-case
> for the "casting".
Well, first of all I use Extbase naming conventions and partly base
classes but I cannot use it fully because when I last tried to use it
more than before (about 2 months ago) there was no support for my use
case, namely modeling a legacy MSSQL database where there is no "uid",
"pid", and the rest columns and I already had written my own
"DataMapper" specifically for this case. Note that for the time being, I
still don't persist anything, I just read data but I'll have too in a
near future and that the rest of TYPO3 runs on standard MySQL.
Now, I made this business model in a dedicated extension and then add it
as a dependency in my other extensions, may it be frontend plugins or
backend modules.
Last week I decided to make a statistic frontend plugin with graphs
(using sav_jpgraph) or plain-old "tables" of data. I use views in MSSQL
to cleanly "prepare" data as I try as much as possible to use TYPO3
standard query methods (with DBAL/AdoDB in this case) but the SQL parser
is so "dumb" that this is a clear no go to create even "simple" queries
involving subqueries:
SELECT ... FROM (SELECT ... FROM ... WHERE ...)
Thus my use case is that I wanted statistics to be put into my domain
model (because it's related to the objects I have there) but still
without "polluting" it for all extensions. This is why I extended the
domain in a "statistics" extension does contains the extended domain
model as well as related FE plugins/BE modules.
I attached the helper class I wrote yesterday to let me cast my domain
objects. I basically now have this for instance for a Person and
PersonRepository I would have to use with statistics:
class Tx_MyExtStatistics_Domain_Model_Person extends
Tx_MyExtBusiness_Domain_Model_Person {
public function getStatistics() {
// code
}
}
class Tx_MyExtStatistics_Domain_Model_PersonRepository extends
Tx_MyExtBusiness_Domain_Model_PersonRepository {
public function findOneByUid($uid) {
$person = parent::findOneByUid($uid);
return
Tx_MyExtStatistics_Helpers_Helper::castToClass('Tx_MyExtStatistics_Domain_Model_Person',
$person);
}
}
As said, Tx_MyExtStatistics_Helpers_Helper is attached to this message.
One more thing, I used some home-made lazy loading that did not yet
exist in Extbase (and I guess still does not). For instance if my person
is part of a company, I have a companyId in the database for the person
table. But I do not want every time to have the related company object
recreated when I get a person. This is why I have a setter on my person
object to set the companyId but I have a getter for a company object,
not for the companyId (this later explains part of the code you find in
my castToClass static method:
public function setCompanyId($id) {
$this->companyId = $id;
// Reset lazy loading
$this->company = null;
}
public function getCompany() {
if (!$this->company) {
$companyRepository =
t3lib_div::makeInstance('Tx_MyExtBusiness_Domain_Model_CompanyRepository');
/* @var ... */
$this->company =
$companyRepository->findOneByUid($this->companyId);
$this->_cleanProperties['company'] = $this->company;
}
return $this->company;
}
--
Xavier Perseguers
DBAL-Member
http://xavier.perseguers.ch/en/tutorials/typo3.html
More information about the TYPO3-project-typo3v4mvc
mailing list