Vitaly回答后更新和工作正常:)
我在一个开源项目工作:https://github.com/open-ecommerce/helptext
我正试图将字段添加到dektrium Yii2-user模块。在repo中有一个关于如何向用户表添加额外字段的示例:https://github.com/dektrium/yii2-user/blob/master/docs/adding-new-field-to-user-model.md但我不能使它与模块自带的配置文件表一起工作。
我使用的是dmstr/yii2-cms- metpackage中的以下repos:
"dektrium/yii2-user": "^0.9.6",
"dektrium/yii2-rbac": "~0.2",
例如,我想要一个新的字段调用'技能'在管理配置文件的形式,所以
我首先做了一个迁移,将字段添加到表
然后在main.php中定义了我自己的类的路径(我使用高级模板,但通过基本简化)
Yii::setAlias('@app', dirname(__DIR__).'/..');
Yii::setAlias('@runtime', dirname(__DIR__).'/../../runtime');
Yii::setAlias('@web', dirname(__DIR__).'/../web');
Yii::setAlias('@webroot', dirname(__DIR__).'/web');
Yii::setAlias('@root', '@app');
$common = [
'id' => 'app',
'language' => '',
'basePath' => dirname(__DIR__),
'vendorPath' => '@app/../vendor',
'runtimePath' => '@app/../runtime',
'bootstrap' => [
'log',
],
'aliases' => [
'@frontmedia' => '@app/media',
'@admin-views' => '@app/modules/backend/views',
],
'components' => [
'assetManager' => [
// Note: For using mounted volumes or shared folders
'dirMode' => YII_ENV_PROD ? 0777 : null,
'bundles' => getenv('APP_ASSET_USE_BUNDLED') ?
require(__DIR__.'/gen/bundle-prod.php') :
[
// Note: if your asset bundle includes bootstrap, you can disable the default asset
#'yiibootstrapBootstrapAsset' => false,
],
'basePath' => '@app/../web/assets',
],
'authManager' => [
'class' => 'yiirbacDbManager',
],
'cache' => [
'class' => 'yiicachingFileCache',
],
'db' => [
'class' => 'yiidbConnection',
'dsn' => getenv('DATABASE_DSN'),
'username' => getenv('DATABASE_USER'),
'password' => getenv('DATABASE_PASSWORD'),
'charset' => 'utf8',
'tablePrefix' => getenv('DATABASE_TABLE_PREFIX'),
'enableSchemaCache' => YII_ENV_PROD ? true : false,
],
'i18n' => [
'translations' => [
'*' => [
'class' => 'yiii18nDbMessageSource',
'db' => 'db',
'sourceLanguage' => 'en',
'sourceMessageTable' => '{{%language_source}}',
'messageTable' => '{{%language_translate}}',
'cachingDuration' => 86400,
'enableCaching' => YII_DEBUG ? false : true,
],
],
],
'mailer' => [
'class' => 'yiiswiftmailerMailer',
//'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => YII_ENV_PROD ? false : true,
],
// Note: enable db sessions, if multiple containers are running
#'session' => [
# 'class' => 'yiiwebDbSession'
#],
'settings' => [
'class' => 'phemesettingscomponentsSettings',
],
'urlManager' => [
'class' => 'codemixlocaleurlsUrlManager',
'enablePrettyUrl' => getenv('APP_PRETTY_URLS') ? true : false,
'showScriptName' => getenv('YII_ENV_TEST') ? true : false,
'enableDefaultLanguageUrlCode' => true,
'baseUrl' => '/',
'rules' => [
'docs/<file:[a-zA-Z0-9_-./]+>' => 'docs',
#'docs' => 'docs/default/index',
],
'languages' => explode(',', getenv('APP_LANGUAGES')),
],
'user' => [
'class' => 'appcomponentsUser',
'enableAutoLogin' => true,
'loginUrl' => ['/user/security/login'],
'identityClass' => 'dektriumusermodelsUser',
'rootUsers' => ['admin'],
],
'view' => [
'theme' => [
'pathMap' => [
'@vendor/dektrium/yii2-user/views/admin' => '@app/views/user/admin',
'@yii/gii/views/layouts' => '@admin-views/layouts',
],
],
],
],
'modules' => [
'backend' => [
'class' => 'appmodulesbackendModule',
'layout' => '@admin-views/layouts/main',
],
'pages' => [
'class' => 'dmstrmodulespagesModule',
'layout' => '@admin-views/layouts/main',
'availableRoutes' => [
'/site/index' => '/site/index',
],
],
'prototype' => [
'class' => 'dmstrmodulesprototypeModule',
'layout' => '@admin-views/layouts/box',
],
'user' => [
'class' => 'dektriumuserModule',
'modelMap' => [
'profile' => 'appmodelsProfile',
],
'layout' => '@app/views/layouts/container',
'defaultRoute' => 'profile',
'adminPermission' => 'user-module',
'enableFlashMessages' => false,
],
'rbac' => [
'class' => 'dektriumrbacModule',
'layout' => '@admin-views/layouts/box',
'enableFlashMessages' => false,
],
'settings' => [
'class' => 'phemesettingsModule',
'layout' => '@admin-views/layouts/box',
'accessRoles' => ['settings-module'],
],
'translatemanager' => [
'class' => 'lajaxtranslatemanagerModule',
'root' => '@app/views',
'layout' => '@admin-views/layouts/box',
'allowedIPs' => ['*'],
'roles' => ['translate-module'],
],
'gridview' => [
'class' => 'kartikgridModule',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
//'i18n' => [],
],
'treemanager' => [
'class' => 'kartiktreeModule',
'layout' => '@admin-views/layouts/main',
'treeViewSettings' => [
'nodeView' => '@vendor/dmstr/yii2-pages-module/views/treeview/_form',
'fontAwesome' => true,
],
],
],
'params' => [
'adminEmail' => getenv('APP_ADMIN_EMAIL'),
'yii.migrations' => [
getenv('APP_MIGRATION_LOOKUP'),
'@yii/rbac/migrations',
'@dektrium/user/migrations',
'@vendor/lajax/yii2-translate-manager/migrations',
'@vendor/pheme/yii2-settings/migrations',
'@vendor/dmstr/yii2-prototype-module/src/migrations',
],
],
];
$web = [
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
],
// Logging
'log' => [
'targets' => [
// writes to php-fpm output stream
[
'class' => 'codemixstreamlogTarget',
'url' => 'php://stdout',
'levels' => ['info', 'trace'],
'logVars' => [],
'enabled' => YII_DEBUG,
],
[
'class' => 'codemixstreamlogTarget',
'url' => 'php://stderr',
'levels' => ['error', 'warning'],
'logVars' => [],
],
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => getenv('APP_COOKIE_VALIDATION_KEY'),
],
'user' => [
'identityClass' => 'dektriumusermodelsUser',
//'admins' => ['admin'],
],
],
];
$console = [
'controllerNamespace' => 'appcommands',
'controllerMap' => [
'db' => 'dmstrconsolecontrollersMysqlController',
'migrate' => 'dmstrconsolecontrollersMigrateController',
'translate' => 'lajaxtranslatemanagercommandsTranslatemanagerController',
],
'components' => [
],
];
$allowedIPs = [
'127.0.0.1',
'::1',
'192.168.*',
'172.17.*',
];
// detecting current application type based on `php_sapi_name()` since we've no application ready yet.
if (php_sapi_name() == 'cli') {
// Console application
$config = yiihelpersArrayHelper::merge($common, $console);
} else {
// Web application
if (YII_ENV_DEV) {
// configuration adjustments for web 'dev' environment
$common['bootstrap'][] = 'debug';
$common['modules']['debug'] = [
'class' => 'yiidebugModule',
'allowedIPs' => $allowedIPs,
];
}
$config = yiihelpersArrayHelper::merge($common, $web);
}
if (YII_ENV_DEV || YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yiigiiModule',
'allowedIPs' => $allowedIPs,
];
$giiant = require __DIR__.'/giiant.php';
$config = yiihelpersArrayHelper::merge($config, $giiant);
}
if (file_exists(getenv('APP_CONFIG_FILE'))) {
// Local configuration, if available
$local = require getenv('APP_CONFIG_FILE');
$config = yiihelpersArrayHelper::merge($config, $local);
}
return $config;
调用models/Profile.php类
namespace appmodels;
use Yii;
use dektriumusermodelsProfile as BaseProfile;
class Profile extends BaseProfile
{
public function scenarios()
{
$scenarios = parent::scenarios();
// add skils to scenarios
$scenarios['create'][] = 'skills';
$scenarios['update'][] = 'skills';
$scenarios['register'][] = 'skills';
return $scenarios;
}
public function rules()
{
$rules = parent::rules();
// add skills rules
$rules['skillsRequired'] = ['skills', 'required'];
$rules['skillsLength'] = ['skills', 'string', 'max' => 255];
return $rules;
}
/** @inheritdoc */
public function attributeLabels()
{
return [
'skills' => Yii::t('userextended', 'This are the skills'),
];
}
/**
* @return yiidbActiveQueryInterface
*/
public function getAccount()
{
return $this->hasOne($this->module->modelMap['Account'], ['user_id' => 'user_id']);
}
/**
* @return yiidbActiveQueryInterface
*/
public function getAccountAttributes()
{
return $this->hasOne($this->module->modelMap['Account'], ['user_id' => 'user_id'])->asArray()->one();
}
}
然后在views/user/admin/_profile.php
<?php
/*
* This file is part of the Dektrium project
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
use yiibootstrapActiveForm;
use yiihelpersHtml;
/**
* @var yiiwebView $this
* @var dektriumusermodelsUser $user
* @var dektriumusermodelsProfile $profile
*/
?>
<?php $this->beginContent('@dektrium/user/views/admin/update.php', ['user' => $user]) ?>
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'fieldConfig' => [
'horizontalCssClasses' => [
'wrapper' => 'col-sm-9',
],
],
]); ?>
<?= $form->field($profile, 'name') ?>
<?= $form->field($profile, 'public_email') ?>
<?= $form->field($profile, 'website') ?>
<?= $form->field($profile, 'location') ?>
<?= $form->field($profile, 'gravatar_email') ?>
<?= $form->field($profile, 'bio')->textarea() ?>
<?= $form->field($profile, 'skills')->textarea() ?>
<div class="form-group">
<div class="col-lg-offset-3 col-lg-9">
<?= Html::submitButton(Yii::t('user', 'Update'), ['class' => 'btn btn-block btn-success']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<?php $this->endContent() ?>
我没有得到一个错误,但字段没有更新表。
如果我直接在数据库中填充字段,我可以看到表单中的值,但它不会在更新
时改变欢迎您的帮助
将'User' => 'appmodelsProfile'
改为'Profile' => 'appmodelsProfile'