你能告诉我tableName()在Yii2中返回值{{%table_name}}和'table_name'的类中的函数有什么区别?
public static function tableName(){
return {{%admin}};
}
public static function tableName(){
return 'admin';
}
Run Code Online (Sandbox Code Playgroud) 我有一个表格
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?php echo $form->field($userformmodel, 'user_image')->fileInput(); ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
Run Code Online (Sandbox Code Playgroud)
我正在上传表单中的文件并提交
In the model I have written the code as
public function rules()
{
return [
[['user_image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg']
];
}
public function upload()
{
if ($this->validate()) {
$this->user_image->saveAs('uploads/' . $this->user_image->baseName . '.' . $this->user_image->extension);
return true;
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中
public function actionProfile()
{ …Run Code Online (Sandbox Code Playgroud) yii2 yii2-advanced-app yii2-basic-app yii2-model yii2-validation
我有两个表名列相同的表,使用两个不同的搜索模型(都使用ActiveDataProvider's)所以当我按列排序时,另一个也会受到影响.
我试过在第二次设置这个GridView:
'sorter' => [
'class' => 'yii\widgets\LinkSorter',
'sortParam' => 'sortB',
],
Run Code Online (Sandbox Code Playgroud)
但无济于事.
编辑:排序参数是传递给服务器的GET变量:?sort=amount,或?param=1&sort=created_at.
根据下面的代码,我想将 customer 中的所有内容更新为 status = 2 的状态 1。但是,我想运行下面的代码,其中 customer_id 不在 (1, 3, 5, 8) 中。
$customerNotIn = array(1, 3, 5 ,8);
Customer::updateAll(['status' => 1], 'status = 2');
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在尝试通过setUsr_firstname对名为的属性使用setter方法对属性进行数据转换usr_firstname.
class User extends ActiveRecord implements IdentityInterface {
public function rules() {
return [
[['usr_firstname', 'usr_lastname', ... ], 'required'],
...
];
}
...
public function setUsr_firstname($value) {
$this->usr_firstname = fix_wrong_title_case($value);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我做以下事情:
$model = new User(['scenario' => User::SCENARIO_REGISTER]);
$model->usr_firstname = 'John';
Run Code Online (Sandbox Code Playgroud)
但是从不调用setter方法!我试过将方法命名为各种各样的东西 - 例如.setUsrFirstname - 但没有任何作用.我如何让它工作?
UPDATE
弄清楚它不适用于任何ActiveRecord属性,在属性名称中有或没有下划线.
我有一个yii2错误:common\models\Book没有名为"favorite"的关系.
当我尝试添加:
public function search($params) {
$query = Book::find();
$query->joinWith(['author', 'profile', 'favorite']);
Run Code Online (Sandbox Code Playgroud)
在书模型中,我确实有公共功能:
public function getFavoritedIcon() {
if (isset($this->favorite)) {
return '<i class="glyphicon glyphicon-asterisk books-form"></i>';
} else {
return '';
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个额外的功能来获取图标
public function getFavoritedIcon() {
if (isset($this->favorite)) {
return $icon;
} else {
return '';
}
}
Run Code Online (Sandbox Code Playgroud)
这在我希望添加排序和过滤的网格中工作正常:
[
'label' => 'Favorites',
'attribute' => 'favorite',
'value' => 'favoritedIcon',
'hAlign' => 'center',
'vAlign' => 'middle',
'format' => 'raw',
'width' => '50px',
],
Run Code Online (Sandbox Code Playgroud)
我从我使用的其他模型做了一些不同的事情:
abstract class extends extends\yii\db\ActiveRecord …
我在 Yii2 中编写了一个应用程序,不幸的是,我们丢失了所有数据库备份。我们现在拥有的只是应用程序文件。是否有更短的方法根据现有模型重新创建 98 个数据库表?我注意到大约 22 个表的架构被缓存在“/app/runtime/cache”下。有人做过这样的事吗?
在标记为重复问题之前,请仔细阅读.我这里有不同的问题.我想使用yii2搜索模型生成这种查询.
select * from t1 innerjoin t2 on (t1.id = t2.id) where ((t1.price >= '1000' and t1.price <= '5000') OR
( t2.price >= '1000' and t2.price <= '5000' ))
Run Code Online (Sandbox Code Playgroud)
加入不是问题.主要问题是where子句.我尝试了这个,但没有工作.
$query->andFilterWhere([
'and',
['>=', 't1.price', $this>start_price],
['<=', 't1.price', $this->end_price]
])
->orFilterWhere([
'and',
['>=', 't2.price', $this->start_price],
['<=', 't2.price', $this->end_price]
]);
Run Code Online (Sandbox Code Playgroud) 只是一个简单的问题.我需要在Yii2中创建一个查询查询,其中只有当变量为true时才会出现.
public function getCheckBoxItems($holiday=false)
{
$Items = Items::find()->where(['type'=>'checkbox']);
Yii::info(print_r($Items,1), 'c');
if($holiday)
$Items->andWhere(['<>','category','holiday']);
$Items->All();
foreach($Items as $Item)
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
这不起作用
然而,这个deos工作,我期待它.
$Items = Items::find()->where(['type'=>'checkbox'])->andWhere(['<>','category','holiday'])->All();
Run Code Online (Sandbox Code Playgroud)
我如何只根据$holiday变量添加andWhere
提前致谢
问候
利亚姆
更新 我找到了一种方法,但我相信有更好的方法
if($holiday)
$Items = Items::find()->where(['type'=>'checkbox'])->andWhere(['<>','category','holiday'])->All();
else
$Items = Items::find()->where(['type'=>'checkbox'])->All();
Run Code Online (Sandbox Code Playgroud) 我可以使用Gii轻松地从MySQL / MariaDB视图生成模型,但是当我尝试生成CRUD时,我会收到以下错误消息:
与frontend \ models \ MyModel关联的表必须具有主键。
另请参见Yii Framework论坛中的讨论。