[TYPO3-mvc] Casting objects
Jochen Rau
jochen.rau at typoplanet.de
Thu Jul 9 12:03:39 CEST 2009
Hi Xavier.
> 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.
You are right. Currently, the persistence layer expects an existing uid
column.
> 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 ...)
The new extbase persistence layer enables you to use joins as follows:
$joinCondition = $this->QOMFactory->equiJoinCondition(
$leftSelector,
'uid',
$rightSelector,
'parent_uid'
);
$source = $this->QOMFactory->join(
$leftSelector,
$rightSelector,
$joinType,
$joinCondition
);
$query = $this->queryFactory->create($className());
$query->setSource($source);
$query->matching($query->equals('name', 'foo');
$objects = $query->execute();
I am going to implement another feature (one of the last ones before
feature freeze) which enables you to say
$query = $this->queryFactory->create($className());
$query->setStatement('SELECT ... FROM (SELECT ... FROM ... WHERE ...)');
$objects = $query->execute();
> 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:
Lazy Loading works now (as a Proxy implementation). You have to specify
the loading strategy in the $TCA like for the Posts in a Blog:
'posts' => array(
[...]
'config' => array(
'type' => 'inline',
'loadingStrategy' => 'proxy', // Here
'foreign_class' => 'Tx_BlogExample_Domain_Model_Post',
'foreign_table' => 'tx_blogexample_domain_model_post',
'foreign_field' => 'blog_uid',
'foreign_table_field' => 'blog_table',
[...]
)
),
> 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;
> }
>
Regards
Jochen
--
Every nit picked is a bug fixed
More information about the TYPO3-project-typo3v4mvc
mailing list