简体   繁体   English

为什么我无法在typo3扩展名中将对象保存到数据库中?

[英]Why am I having trouble saving objects to the database in a typo3 extension?

Why won't my user and coupon objects get saved to the database when a new record is created? 创建新记录时,为什么我的用户和优惠券对象不会保存到数据库中?

I am using typo3 v 4.5.30 and making a little extension (my first) to manage some coupons. 我正在使用typo3 v 4.5.30,并做了一些扩展(我的第一个)来管理一些优惠券。 When I create a coupon I save the creator (a frontenduser) and it gets saved to the DB correctly. 创建优惠券时,我保存了创建者(前端用户)并将其正确保存到数据库中。 But when I do the same thing for a coupon user that user ( and the coupon ) do not get saved to the db. 但是,当我对优惠券用户执行相同的操作时,该用户(和优惠券)不会保存到数据库中。 Consider this code frag which attempts to save the user and the coupon in a usedcoupon table. 考虑一下此代码片段,该代码片段尝试将用户和优惠券保存在usedcoupon表中。 The usedcoupon table basically just has 2 columns one for the user and one for the coupon. 用过的优惠券表基本上只有两列,一列用于用户,一列用于优惠券。

To get a usedcoupon object I called the objectmanagers create method. 为了获得一个使用优惠券的对象,我调用了objectmanagers create方法。 The user and coupon objects I already have and look right when I var_dump them. 我已经拥有的用户和优惠券对象,并且在var_dump时看起来正确。 Even when I get them from the usedcoupon object they look ok but they don't get saved to the db even when the new record gets created. 即使当我从usedcoupon对象中获取它们时,它们看起来也不错,但是即使创建了新记录,它们也不会保存到数据库中。 This code in in my CouponController in an action method. 这段代码在我的CouponController中的一个动作方法中。

             $used = $this->objectManager>create('Tx_BpsCoupons_Domain_Model_UsedCoupon');    
             $used->setCoupon($coupon);
             $used->setUser($user);
             $used->setGuest("myemail@ddd.com");

             $userx = $used->getUser();
             $coupx = $used->getCoupon();
             /// var_dumps of userx and coupx  show good objects

             $this->usedCouponRepository->add($used);
             //after this I can examine the db and see the new record but the user and coupon fields are empty, and no errors are seen

Thanks 谢谢

PS here is my TCA from Usedcoupon.php PS这里是我来自Usedcoupon.php的TCA

    <?php
if (!defined ('TYPO3_MODE')) {
    die ('Access denied.');
}

$TCA['tx_bpscoupon_domain_model_usedcoupon'] = array(
    'ctrl' => $TCA['tx_bpscoupon_domain_model_usedcoupon']['ctrl'],
    'interface' => array(
        'showRecordFieldList' => 'sys_language_uid, l10n_parent, l10n_diffsource, hidden, user, coupon, guest',
    ),
    'types' => array(
        '1' => array('showitem' => 'sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource, hidden;;1, user, coupon, guest,--div--;LLL:EXT:cms/locallang_ttc.xml:tabs.access,starttime, endtime'),
    ),
    'palettes' => array(
        '1' => array('showitem' => ''),
    ),
    'columns' => array(
        'sys_language_uid' => array(
            'exclude' => 1,
            'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.language',
            'config' => array(
                'type' => 'select',
                'foreign_table' => 'sys_language',
                'foreign_table_where' => 'ORDER BY sys_language.title',
                'items' => array(
                    array('LLL:EXT:lang/locallang_general.xml:LGL.allLanguages', -1),
                    array('LLL:EXT:lang/locallang_general.xml:LGL.default_value', 0)
                ),
            ),
        ),
        'l10n_parent' => array(
            'displayCond' => 'FIELD:sys_language_uid:>:0',
            'exclude' => 1,
            'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.l18n_parent',
            'config' => array(
                'type' => 'select',
                'items' => array(
                    array('', 0),
                ),
                'foreign_table' => 'tx_bpscoupon_domain_model_usedcoupon',
                'foreign_table_where' => 'AND tx_bpscoupon_domain_model_usedcoupon.pid=###CURRENT_PID### AND tx_bpscoupon_domain_model_usedcoupon.sys_language_uid IN (-1,0)',
            ),
        ),
        'l10n_diffsource' => array(
            'config' => array(
                'type' => 'passthrough',
            ),
        ),
        't3ver_label' => array(
            'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.versionLabel',
            'config' => array(
                'type' => 'input',
                'size' => 30,
                'max' => 255,
            )
        ),
        'hidden' => array(
            'exclude' => 1,
            'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.hidden',
            'config' => array(
                'type' => 'check',
            ),
        ),
        'starttime' => array(
            'exclude' => 1,
            'l10n_mode' => 'mergeIfNotBlank',
            'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.starttime',
            'config' => array(
                'type' => 'input',
                'size' => 13,
                'max' => 20,
                'eval' => 'datetime',
                'checkbox' => 0,
                'default' => 0,
                'range' => array(
                    'lower' => mktime(0, 0, 0, date('m'), date('d'), date('Y'))
                ),
            ),
        ),
        'endtime' => array(
            'exclude' => 1,
            'l10n_mode' => 'mergeIfNotBlank',
            'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.endtime',
            'config' => array(
                'type' => 'input',
                'size' => 13,
                'max' => 20,
                'eval' => 'datetime',
                'checkbox' => 0,
                'default' => 0,
                'range' => array(
                    'lower' => mktime(0, 0, 0, date('m'), date('d'), date('Y'))
                ),
            ),
        ),
        'user' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:bpscoupon/Resources/Private/Language/locallang_db.xml:tx_bpscoupon_domain_model_usedcoupon.user',
            'config' => array(
                'type' => 'select',
                'foreign_table' => 'fe_users'
            ),
        ), 
        'coupon' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:bpscoupon/Resources/Private/Language/locallang_db.xml:tx_bpscoupon_domain_model_usedcoupon.coupon',
            'config' => array(
                'type' => 'select',
                'foreign_table' => 'tx_bpscoupons_domain_model_coupon'
            ),
        ),
        'guest' => array(
            'exclude' => 0,
            'label' => 'LLL:EXT:bpscoupon/Resources/Private/Language/locallang_db.xml:tx_bpscoupon_domain_model_usedcoupon.guest',
            'config' => array(
                'type' => 'input',
                'size' => 30,
                'eval' => 'trim'
            ),
        ),
    ),
);

?>

PPS adding usedcoupon model code: PPS添加了使用的优惠券模型代码:

    <?php

/***************************************************************
 *  Copyright notice
 *
 *  (c) 2013 Cory Taylor <cory@bigplayersystems.com>, Big Player Systems
 *  
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

/**
 *
 *
 * @package bps_coupons
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
 *
 */
class Tx_BpsCoupons_Domain_Model_UsedCoupon extends Tx_Extbase_DomainObject_AbstractEntity {


    /**
     * Used By
     *
     * @var Tx_BpsCoupons_Domain_Model_FrontendUser
     */
    protected $user;


    /**
     * Returns the $user
     *
     * @return Tx_BpsCoupons_Domain_Model_FrontendUser $user
     */
    public function getUser() {
            echo "<br/>" . __FUNCTION__ . __LINE__ . "  <br/>";
            return $this->user;
    }

    /**
     * Sets the $user
     *
     * @param @param Tx_BpsCoupons_Domain_Model_FrontendUser $user  
     * @return void
     */
    public function setUser(Tx_BpsCoupons_Domain_Model_FrontendUser $user) {
        $this->user = $user;
    }

     /**
     * the used coupon
     *
     * @var Tx_BpsCoupons_Domain_Model_Coupon
     */
    protected $coupon;


    /**
     * Returns the $coupon
     *
     * @return Tx_BpsCoupons_Domain_Model_Coupon $coupon
     */
    public function getCoupon() {
        return $this->coupon;
    }

    /**
     * Sets the $coupon
     *
     * @param @param Tx_BpsCoupons_Domain_Model_Coupon $coupon  
     * @return void
     */
    public function setCoupon(Tx_BpsCoupons_Domain_Model_Coupon $coupon) {
        $this->coupon = $coupon;
    }


    /**
 * the guest email
 *
 * @var string
 */
protected $guest;


/**
 * Returns the $guest
 *
 * @return string $guest
 */
public function getGuest() {
    return $this->guest;
}

/**
 * Sets the $guest email
 *
 * @param string $guest
 * @return void
 */
public function setGuest( $guest) {
    $this->guest = $guest;
}

}
?>

PPPS: I tried adding a basic text field for email addresses but it turns out they don't get saved either. PPPS:我尝试为电子邮件地址添加一个基本文本字段,但事实证明它们也没有保存。 I had thought the issue was that the user and coupon filed were references to rows in other tables but now it turns out that simpler things do not get saved either. 我以为问题是用户和优惠券文件是对其他表中行的引用,但是现在事实证明,更简单的内容也无法保存。

PP PP S: may as well look into my ext_tables file too: PP PP S:也可以查看我的ext_tables文件:

<?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}

Tx_Extbase_Utility_Extension::registerPlugin(
    $_EXTKEY,
    'Bpscoupons',
    'BPS_Coupons'
);

t3lib_extMgm::addStaticFile($_EXTKEY, 'Configuration/TypoScript', 'BPS_Coupons');

t3lib_extMgm::addLLrefForTCAdescr('tx_bpscoupons_domain_model_coupon', 'EXT:bps_coupons/Resources/Private/Language/locallang_csh_tx_bpscoupons_domain_model_coupon.xml');
t3lib_extMgm::allowTableOnStandardPages('tx_bpscoupons_domain_model_coupon');
$TCA['tx_bpscoupons_domain_model_coupon'] = array(
    'ctrl' => array(
        'title' => 'LLL:EXT:bps_coupons/Resources/Private/Language/locallang_db.xml:tx_bpscoupons_domain_model_coupon',
        'label' => 'name',
        'tstamp' => 'tstamp',
        'crdate' => 'crdate',
        'cruser_id' => 'cruser_id',
        'dividers2tabs' => TRUE,
        'sortby' => 'sorting',
        'versioningWS' => 2,
        'versioning_followPages' => TRUE,
        'origUid' => 't3_origuid',
        'languageField' => 'sys_language_uid',
        'transOrigPointerField' => 'l10n_parent',
        'transOrigDiffSourceField' => 'l10n_diffsource',
        'delete' => 'deleted',
        'enablecolumns' => array(
            'disabled' => 'hidden',
            'starttime' => 'starttime',
            'endtime' => 'endtime',
        ),
        'searchFields' => 'name,description,expiry,hall,date_created,creator,barcode,',
        'dynamicConfigFile' => t3lib_extMgm::extPath($_EXTKEY) . 'Configuration/TCA/Coupon.php',
        'iconfile' => t3lib_extMgm::extRelPath($_EXTKEY) . 'Resources/Public/Icons/tx_bpscoupons_domain_model_coupon.gif'
    ),
);



t3lib_extMgm::addLLrefForTCAdescr('tx_bpscoupons_domain_model_usedcoupon', 'EXT:bpscoupons/Resources/Private/Language/locallang_csh_tx_bpscoupons_domain_model_usedcoupon.xml');
t3lib_extMgm::allowTableOnStandardPages('tx_bpscoupons_domain_model_usedcoupon');
$TCA['tx_bpscoupons_domain_model_usedcoupon'] = array(
    'ctrl' => array(
        'title' => 'LLL:EXT:bpscoupons/Resources/Private/Language/locallang_db.xml:tx_bpscoupons_domain_model_usedcoupon',
        'label' => 'user',
        'tstamp' => 'tstamp',
        'crdate' => 'crdate',
        'cruser_id' => 'cruser_id',
        'dividers2tabs' => TRUE,

        'versioningWS' => 2,
        'versioning_followPages' => TRUE,
        'origUid' => 't3_origuid',
        'languageField' => 'sys_language_uid',
        'transOrigPointerField' => 'l10n_parent',
        'transOrigDiffSourceField' => 'l10n_diffsource',
        'delete' => 'deleted',
        'enablecolumns' => array(
            'disabled' => 'hidden',
            'starttime' => 'starttime',
            'endtime' => 'endtime',
        ),
        'searchFields' => 'user,coupon,guest,',
        'dynamicConfigFile' => t3lib_extMgm::extPath($_EXTKEY) . 'Configuration/TCA/Usedcoupon.php',
        'iconfile' => t3lib_extMgm::extRelPath($_EXTKEY) . 'Resources/Public/Icons/tx_bpscoupons_domain_model_usedcoupon.gif'
    ),
);


?>

If that happens, most probably your objects are not persisted. 如果发生这种情况,很可能您的对象将不会保留。 Normally Extbase persists changes to objects after the end of a request. 通常,在请求结束后,Extbase会保留对对象的更改。 But if you eg return your response as JSON and then exit your action, the persistenceManager is not called. 但是,如果您将响应返回为JSON,然后退出操作,则不会调用persistenceManager。

You can persist manually by injecting the persistenceManager to your controller: 您可以通过将persistenceManager注入控制器来手动持久化:

/**
* @var Tx_Extbase_Persistence_Manager
*/
protected $persistenceManager;

/**
* @param Tx_Extbase_Persistence_Manager $persistanceManager
* @return void
*/
public function injectPersistenceManager(Tx_Extbase_Persistence_Manager $persistenceManager) {
  $this->persistenceManager = $persistenceManager;
}

and then call it after you added the new object: 然后在添加新对象后调用它:

$persistenceManager->persistAll();

In TYPO3 4.7+, you can use the @inject annotation in the doc comment of $persistenceManager to inject the persistenceManager and don't need the inject function. 在TYPO3 4.7+中,可以在$ persistenceManager的文档注释中使用@inject批注来注入persistenceManager,而无需注入功能。

If this doesn't solve the problem, maybe there's a problem with your TCA. 如果这不能解决问题,则说明您的TCA可能有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM