我不确定这是否仅限于Yii2,或者它是否也可以与其他框架或甚至一般性讨论.
在Yii2中,我可以在模型对象上调用validate().这将返回true或false.如果为false,我可以调用getErrors()来查看此对象中当前存在哪些验证问题.
为什么这种行为以这种方式实现?为什么validate()不会抛出一些ValidationExceptions?我发现错误也是模型对象的一部分.为什么他们不属于这种例外?
这种实现的优点是什么?为什么这样做?我更喜欢例外.有了这个,我可以更好地区分所需的和特殊的处理.所有不需要的东西都在catch块中处理.期望发生在try块中.这不是个好主意吗?
当我使用函数获取属性的值并且它使用Gridview正常工作时,我收到错误.我做错了什么?
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'subject_type',
'value' => function ($data) {
return Lookup::item("SubjectType", $data->subject_type);
},
'filter' => Lookup::items('SubjectType'),
],
'id',
'subject_nature',
],
]) ?>
Run Code Online (Sandbox Code Playgroud) 如果我记录异常,应该怎么办?例:
Yii::error(new Exception('test'));
Run Code Online (Sandbox Code Playgroud)
目前,使用我的基本应用程序模板没有任何反应。什么都不会记录(进一步的error()调用也不会记录)。它是否正确?配置为:
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
Run Code Online (Sandbox Code Playgroud)
我曾期望异常会被适当地记录。我应该如何记录异常,特别是。如果我想看看痕迹?
更新:
参见问题在GitHub上。使用Yii 2.0.6可以记录异常。
如果捕获异常并引发另一个异常,这可能会很有用。然后,您可以记录原始问题。但是,如果引发基于Yii异常的异常,则可以经常(或始终?)将原始异常附加为$previous。如果未在任何地方捕获到此类异常,则会自动将其与上一个异常一起记录。
有什么方法可以观察 ES6 地图和集合的添加和删除吗?Object.observe不起作用,因为它仅适用于被观察对象的直接属性。假设size可以观察到该属性,但不会提供具体发生了什么变化的指示。另一个想法是用代理版本替换对象set和函数。get有没有更好的办法?如果没有,我很惊讶在为 ES6 编写提案时没有人想到这一点。
我正在使用yii2框架.我有两页业务和联系.我使用CRUD生成页面.现在,当我使用商业页面时,我必须得到一个子模块来访问联系人.我必须从业务页面访问使用URL进行联系.一旦我点击商业页面中的联系人,它应该将我重定向到联系页面.我该怎么办?我试图用gii创建模块.但我也没有获得类IndexAction.
<?php
namespace app\modules\help\controllers;
use yii\base\Action;
class IndexAction extends Action
{
public function run()
{
$this->controller->render('index');
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个页面表.页面可以有父母,也可以是页面.我想选择parent_id = NULL及其'children'的所有页面.但是当我试着这个
public function getPages()
{
return $this->hasMany(Page::className(), ['parent_id' => 'id']);
}
Run Code Online (Sandbox Code Playgroud)
我得到一个1066不唯一的表/别名:'page'错误...我知道如何在Yii1中解决这个问题,但我无法弄清楚在Yii2中修复它.
我想在Yii2分页中使用引导glyphicon图标作为上一个和下一个按钮。我已经在下面的代码中设置了他们的css类,但是没有用。它显示图标,但没有显示在正确的位置,并且没有链接。
'pager' => [
'options' => ['class'=>'pagination'], // set clas name used in ui list of pagination
'prevPageLabel' => '>', // Set the label for the "previous" page button
'nextPageLabel' => '<', // Set the label for the "next" page button
'firstPageLabel' => '>>', // Set the label for the "first" page button
'lastPageLabel' => '<<', // Set the label for the "last" page button
'nextPageCssClass' => 'next', // Set CSS class for the "next" page button
'prevPageCssClass' => 'perv', …Run Code Online (Sandbox Code Playgroud) 在我的 Yii2 配置中,我有:
'components' => [
'urlManager' => [
'baseUrl' => '',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'search' => 'site/index',
...
],
...
Run Code Online (Sandbox Code Playgroud)
如果我去site.com/search它工作。如果我去site.com/site/index它也可以工作并显示相同的内容。如何重定向呼叫而不是仅显示站点/索引的响应?它还必须使用参数(site.com/site/index?param=1-> site.com/search?param=1)重定向
在单个操作中处理多个表单的正确方法是什么?
这是我的模型/ MembersBans.php
<?php
namespace app\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use app\models\Members;
class MembersBans extends \yii\db\ActiveRecord {
public $username;
public static function tableName() {
return '{{%members_bans}}';
}
public static function primaryKey() {
return array('ban_id');
}
public function behaviors() {
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'date_added',
'updatedAtAttribute' => 'last_updated',
],
];
}
public function rules() {
return [
[['ban_id', 'ban_memberid', 'date_added', 'last_updated'], 'integer'],
[['username', 'end_date'], 'safe'],
['end_date', 'date', 'format' => 'yyyy-mm-dd'],
[['ban_ip'], 'string', 'max' => 40],
[['reason'], 'string', …Run Code Online (Sandbox Code Playgroud) 我想扩展自己的用户模型,使用一个名为getFirstname的函数.该函数应返回数据库中的自定义字段.
但是当我扩展用户模型时.它说"调用未知方法:yii\web\User :: getFirstname()"
我在app\models\user中的用户模型.我删除了文件中与此问题无关的方法.
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
/**
* User model
*
*/
class User extends ActiveRecord implements IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [];
}
/**
* Finds user by email
*
* @param string $email
* @return …Run Code Online (Sandbox Code Playgroud) yii2 ×9
php ×2
activerecord ×1
dictionary ×1
ecmascript-6 ×1
exception ×1
forms ×1
gridview ×1
logging ×1
model ×1
pagination ×1
self-join ×1
set ×1
validation ×1
view ×1
yii ×1
yii2-module ×1
yii2-user ×1