使用extbase存储库方法TYPO3的tt_content字段

时间:2015-05-27 10:43:55

标签: typo3 fluid extbase typo3-6.2.x

我正在尝试使用TYPO3 extbase存储库方法开发自定义扩展。我想要获取tt_content" pi_flexform"我的扩展中的字段值。

为实现这一点,我遵循了持久性方法。

在typoscript配置文件中,我已经添加了这样的内容;

plugin.tx_myext{
   persistence {
        classes {
            \TYPO3\MyExt\Domain\Model\Myext {
                mapping {
                    tableName = tt_content
                    recordType = \TYPO3\MyExt\Domain\Model\Myext
                    columns {
                        pi_flexform.mapOnProperty = piflexform
                    }
                }
            }
        }
    }
}

在我的模型文件中,我实现了以下代码;

/**
 * @var string
 */
protected $piflexform;

/**
 * @return the $piflexform
 */
public function getPiflexform()
{
    return $this->piflexform;
}

/**
 * @param string $piflexform
 */
public function setPiflexform($piflexform)
{
    $this->piflexform = $piflexform;
}

还在存储库文件中;

$queryStmt = "SELECT pi_flexform FROM tt_content WHERE CType = 'dce_dce10' AND pid = 1 AND sys_language_uid = 0";
$query = $this->createQuery();
$query->statement( $queryStmt );
return $query->execute();

但它返回一个空对象。

TYPO3\CMS\Extbase\Persistence\Generic\QueryResultprototypeobject (empty)
   0 => TYPO3\VstRssfeed\Domain\Model\Vstrssfeedprototypepersistent entity (uid=0, pid=0)
      piflexform => NULL
      uid => 0 (integer)
      _localizedUid => 0 (integer)modified
      _languageUid => 0 (integer)modified
      _versionedUid => 0 (integer)modified
      pid => 0 (integer)

你能帮助我解决这个问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

  

如何有效使用Extbase的存储库和查询对象?

     

http://blog.typoplanet.de/2010/01/27/the-repository-and-query-object-of-extbase/

以下是您的存储库应如何显示的示例:

<强> myext /类别/域/存储库/ ContentRepository.php

<?php
namespace TYPO3\MyExt\Domain\Repository;

/**
 * Class ContentRepository
 * @package TYPO3\MyExt\Domain\Repository
 */
class ContentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {

    /**
     * Initializes the repository.
     *
     * @return void
     */
    public function initializeObject() {
        /** @var $querySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
        $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
        $querySettings->setRespectStoragePage(FALSE);
        $this->setDefaultQuerySettings($querySettings);
    }

    /**
     * Find By CType
     *
     * @param string $cType
     * @param integer $pid
     * @return  \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
     */
    public function findByCtype($cType, $pid = FALSE) {
        $constraints = array();
        $query = $this->createQuery();

        $constraints[] = $query->equals('CType', $cType);
        if ($pid) {
            $constraints[] = $query->equals('pid', $pid);
        }

        if ($constraints) {
            if (count($constraints) == 1) {
                $query->matching($constraints[0]);  
            } else {
                $query->matching($query->logicalAnd($constraints));
            }
        }

        return $query->execute();
    }

}

以下是控制器应该如何完成工作的示例:

<强> myext /类别/控制器/ SomeController.php

class SomeController extends \TYPO3\CMS\E... {

    /**
     * @var \TYPO3\MyExt\Domain\Repository\ContentRepository
     * @inject
     */
    protected $contentRepository = NULL;

    public function someAction() {
        $contents = $this->contentRepository->findByCtype('dce_dce10');
        if ($contents) {
            foreach ($contents as $content) {
                echo '<h1>'.$content->getUid().'</h1>';
                echo PHP_EOL.'<br />';
                echo $content->getPiflexform();
                echo PHP_EOL.'<br />';
            }

        }
    }
}