[BUGFIX] Respect tablenames, fieldname when updating mm record 05/32505/7
authorFrans Saris <franssaris@gmail.com>
Fri, 29 Aug 2014 10:14:31 +0000 (12:14 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 9 Sep 2014 10:47:23 +0000 (12:47 +0200)
The Typo3DbBackend doesn't respect the columns tablenames
and fieldname when updating mm releations. As result wrong
relations are adjusted.

Resolves: #61268
Releases: 6.2, 6.3
Change-Id: Ica1c04fb54b2f152ccf0f9bd766091854f01721a
Reviewed-on: http://review.typo3.org/32505
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/extbase/Classes/Persistence/Generic/Backend.php
typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php
typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/Classes/Domain/Model/Blog.php
typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example/ext_tables.php
typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/category-mm.xml
typo3/sysext/extbase/Tests/Functional/Persistence/RelationTest.php

index 57734fa..6bef97a 100644 (file)
@@ -715,13 +715,13 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
        }
 
        /**
-        * Inserts mm-relation into a relation table
+        * Updates mm-relation in a relation table
         *
         * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object The related object
         * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
         * @param string $propertyName The name of the parent object's property where the related objects are stored in
         * @param integer $sortingPosition Defaults to NULL
-        * @return integer The uid of the inserted row
+        * @return bool TRUE if update was successfully
         */
        protected function updateRelationInRelationTable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object, \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $propertyName, $sortingPosition = 0) {
                $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
@@ -747,7 +747,7 @@ class Backend implements \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
         *
         * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject The parent object
         * @param string $parentPropertyName The name of the parent object's property where the related objects are stored in
-        * @return boolean
+        * @return bool TRUE if delete was successfully
         */
        protected function deleteAllRelationsFromRelationtable(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $parentObject, $parentPropertyName) {
                $dataMap = $this->dataMapper->getDataMap(get_class($parentObject));
index 2e4727c..05cc5a0 100644 (file)
@@ -184,6 +184,15 @@ class Typo3DbBackend implements BackendInterface, \TYPO3\CMS\Core\SingletonInter
                unset($fieldValues['uid_local']);
                unset($fieldValues['uid_foreign']);
 
+               if (!empty($fieldValues['tablenames'])) {
+                       $where['tablenames'] = $fieldValues['tablenames'];
+                       unset($fieldValues['tablenames']);
+               }
+               if (!empty($fieldValues['fieldname'])) {
+                       $where['fieldname'] = $fieldValues['fieldname'];
+                       unset($fieldValues['fieldname']);
+               }
+
                $updateSuccessful = $this->databaseHandle->exec_UPDATEquery(
                        $tableName,
                        $this->resolveWhereStatement($where, $tableName),
index 28b4b95..749b770 100644 (file)
@@ -51,6 +51,11 @@ class Blog extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
        protected $posts = NULL;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\Category>
+        */
+       protected $categories = NULL;
+
+       /**
         * The blog's administrator
         *
         * @var \ExtbaseTeam\BlogExample\Domain\Model\Administrator
@@ -158,6 +163,42 @@ class Blog extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
        }
 
        /**
+        * Add category to a blog
+        *
+        * @param \TYPO3\CMS\Extbase\Domain\Model\Category $category
+        */
+       public function addCategory(\TYPO3\CMS\Extbase\Domain\Model\Category $category) {
+               $this->categories->attach($category);
+       }
+
+       /**
+        * Set categories
+        *
+        * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $categories
+        */
+       public function setCategories($categories) {
+               $this->categories = $categories;
+       }
+
+       /**
+        * Get categories
+        *
+        * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
+        */
+       public function getCategories() {
+               return $this->categories;
+       }
+
+       /**
+        * Remove category from blog
+        *
+        * @param \TYPO3\CMS\Extbase\Domain\Model\Category $category
+        */
+       public function removeCategory(\TYPO3\CMS\Extbase\Domain\Model\Category $category) {
+               $this->categories->detach($category);
+       }
+
+       /**
         * Sets the administrator value
         *
         * @param Administrator $administrator The Administrator of this Blog
index d0bf633..216b7ad 100644 (file)
@@ -116,5 +116,6 @@ if (is_array($TCA['fe_users']['columns']['tx_extbase_type'])) {
        array_push($TCA['fe_users']['columns']['tx_extbase_type']['config']['items'], array('LLL:EXT:blog_example/Resources/Private/Language/locallang_db.xml:fe_users.tx_extbase_type.Tx_BlogExample_Domain_Model_Administrator', 'Tx_BlogExample_Domain_Model_Administrator'));
 }
 
-// Categorize Post records
+// Categorize Blog,Post records
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable($_EXTKEY, 'tx_blogexample_domain_model_blog');
 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable($_EXTKEY, 'tx_blogexample_domain_model_post');
index debd04a..a927275 100644 (file)
@@ -7,7 +7,7 @@
                <tablenames>tx_blogexample_domain_model_post</tablenames>
                <fieldname>categories</fieldname>
                <sorting>1</sorting>
-               <sorting_foreign>2</sorting_foreign>
+               <sorting_foreign>1</sorting_foreign>
        </sys_category_record_mm>
        <sys_category_record_mm>
                <uid_local>2</uid_local>
@@ -23,7 +23,7 @@
                <tablenames>tx_blogexample_domain_model_post</tablenames>
                <fieldname>categories</fieldname>
                <sorting>1</sorting>
-               <sorting_foreign>2</sorting_foreign>
+               <sorting_foreign>3</sorting_foreign>
        </sys_category_record_mm>
 
        <!-- post 2 gets category 1 -->
                <sorting>1</sorting>
                <sorting_foreign>2</sorting_foreign>
        </sys_category_record_mm>
+
+       <!-- blog 1 gets categories 1,2,3 -->
+       <sys_category_record_mm>
+               <uid_local>1</uid_local>
+               <uid_foreign>1</uid_foreign>
+               <tablenames>tx_blogexample_domain_model_blog</tablenames>
+               <fieldname>categories</fieldname>
+               <sorting>1</sorting>
+               <sorting_foreign>1</sorting_foreign>
+       </sys_category_record_mm>
+       <sys_category_record_mm>
+               <uid_local>2</uid_local>
+               <uid_foreign>1</uid_foreign>
+               <tablenames>tx_blogexample_domain_model_blog</tablenames>
+               <fieldname>categories</fieldname>
+               <sorting>1</sorting>
+               <sorting_foreign>2</sorting_foreign>
+       </sys_category_record_mm>
+       <sys_category_record_mm>
+               <uid_local>4</uid_local>
+               <uid_foreign>1</uid_foreign>
+               <tablenames>tx_blogexample_domain_model_blog</tablenames>
+               <fieldname>categories</fieldname>
+               <sorting>1</sorting>
+               <sorting_foreign>3</sorting_foreign>
+       </sys_category_record_mm>
+
+       <!-- blog 2 gets category 2 -->
+       <sys_category_record_mm>
+               <uid_local>2</uid_local>
+               <uid_foreign>2</uid_foreign>
+               <tablenames>tx_blogexample_domain_model_blog</tablenames>
+               <fieldname>categories</fieldname>
+               <sorting>1</sorting>
+               <sorting_foreign>1</sorting_foreign>
+       </sys_category_record_mm>
 </dataset>
index 675590e..8b588ae 100644 (file)
@@ -491,6 +491,37 @@ class RelationTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
        }
 
        /**
+        * Test if adjusting existing mm relations do not relations with other objects
+        *
+        * @test
+        */
+       public function adjustingMmRelationWithTablesnameAndFieldnameFieldDoNotTouchOtherRelations() {
+               /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
+               $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
+               /** @var \ExtbaseTeam\BlogExample\Domain\Model\Post $post */
+               $post = $postRepository->findByUid(1);
+               // Move category down
+               foreach ($post->getCategories() as $category) {
+                       $post->removeCategory($category);
+                       $post->addCategory($category);
+                       break;
+               }
+               $postRepository->update($post);
+               $this->persistentManager->persistAll();
+
+               // re-fetch Post and Blog
+               $newBlogCategoryCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
+                       'uid_local',
+                       'sys_category_record_mm',
+                       'tablenames = "tx_blogexample_domain_model_blog"
+                       AND fieldname = "categories"
+                       AND uid_foreign = ' . $this->blog->getUid() . ''
+               );
+
+               $this->assertSame($this->blog->getCategories()->count(), $newBlogCategoryCount);
+       }
+
+       /**
         * Helper method for persisting blog
         */
        protected function updateAndPersistBlog() {