blog.Resource

Archive:

News-Feeds:


RSS 2.0
RSS 0.91
RDF
ATOM 0.3
July 11, 2009

Extbase: Persistence rewrite has landed!

By: Sebastian Kurfürst

Extbase has just gotten a new, shiny, persistence layer! This is the foundation for many requested features such as lazy loading.

Jochen's work of the last months finally landed into Extbase trunk: The complete rewrite of the persistence layer! It is now even closer to the FLOW3 architecture, implementing the same query language as FLOW3.

For you as a programmer, this mostly means that you do not have to write a single line of SQL anymore - a great benefit in terms of security - no SQL injections possible anymore!

Most SQL queries in today's web applications are things like "SELECT * FROM users WHERE username='john'" - these are even easier by using the magic methods provided in every repository. To get the User object with username "john" you can do the following in your controller:

$user = $this->userRepository->findOneByName('john');
// $user is a user object now!

Pay special attention to the naming of the finder method. You always have findBy* and findOneBy* methods available on every repository for all properties. Additionally, you get back an object! Thus, no fiddling with arrays and the like anymore!

Now, you might ask yourself how you can write more complex queries. They are placed inside the repository class, and can look like this:

$query = $this->createQuery();
$resultingUser = $query->matching(
  $query->logicalAnd(
    $query->equals('username', $username),
    $query->equals('password', $password)
))->execute();

It takes some time to get accustomed to the above syntax, but it is the same syntax as in FLOW3! You will not notice if a relational database or a content repository is queried. Isn't that cool?

Lazy Loading

Actually, you might wonder if it is worth it to have such complex code changes for so little things which change on the surface. However, we definitely think yes: Because of the new, clean architecture of the persistence layer, Jochen has been implementing Lazy Loading and some other neat features already.

Next steps

We are currently reviewing the code, and fixing issues, and then update the version in TYPO3 trunk. If you want to play around with it, you can find the bleeding edge in the SVN.

Please give us some feedback!


comments

comment #1
Gravatar: Fabien Udriot Fabien Udriot July 11, 2009 20:56
Nice landing, indeed! New query syntax needs some time to get used to it but looking forward to take off with ExtBase. :)

comment #2
Gravatar: Sebastian Sebastian July 12, 2009 01:22
Great to see progress, but I'm wondering (not for the first time) why re-inventing the wheel, e.g. when there already are great ORMs like Doctrine out there? Is it just "not invented here" or are there better reasons?

Sebastian

comment #3
Gravatar: Sebastian Kurfürst Sebastian Kurfürst July 12, 2009 09:49
Hi Sebastian,
Doctrine and all the other ORMs do not follow the domain object conventions, f.e. there, you have to call "save()" on objects explicitly. I currently do not know a PHP based ORM which makes persistence with a unit of work of plain PHP objects possible (like hibernate does in the java world).

Greets,
Sebastian

comment #4
Gravatar: Jonas Jonas July 13, 2009 10:08
Great work! That's realy the right direction to bring this already to 4.x!

We use propel/symfony some times, so we'r use to get data from the database with similar methods.

But most important question to me:

How does this integrate with TCE:
--> Workspace preview
--> Sys_history / sys_log
--> Show / Hide fields
--> Deleted Field
--> l18n_parent / translation handling
--> Start / Stop fields
--> fe_groups permissions

The big advantage of object oriented database abstraction is not the fact, that SQL injections are not possible anymore. It's the fact that you can implement such features realy easy for all kind of data without changing any query.

Isn't it?

comment #5
Gravatar: Sebast Sebast July 22, 2009 18:10
(I hope there are no double-posts, had some problems when submitting the comment)

Hello Sebastian,

> Doctrine and all the other ORMs do not follow the domain object
> conventions, f.e. there, you have to call "save()" on objects
> explicitly. I currently do not know a PHP based ORM which makes
> persistence with a unit of work of plain PHP objects possible
> (like hibernate does in the java world).

thanks for your answer!

Couldn't this be easily built on top of existing ORMs like Doctrine?

I did a quick search and found this for Doctrine:

http : / / www.doctrine-project.org/documentation/cookbook/1_0/en/creating-a-unit-of-work-using-doctrine
(see also: http : / / lebensold.net/php/a-unit-of-work-in-php)

And here's some more information: http : / / trac.doctrine-project.org/wiki/DocSnippetsChangeTrackingUow


Regards,
Sebastian

comment #6
Gravatar: romanb romanb July 27, 2009 11:16
> Doctrine and all the other ORMs do not follow the domain
> objectconventions, f.e. there, you have to call "save()"
> on objects explicitly. I currently do not know a PHP
> based ORM which makes persistence with a unit of work of
> plain PHP objects possible (like hibernate does in the
> java world).

Here it is: http://trac.doctrine-project.org/browser/trunk

;-)

Roman

Sorry, comments are closed for this post.