所以我目前正在尝试将我的网站转换为 YII 框架,但目前我主要是在玩弄并学习它是如何工作的。
有件小事困扰着我,我希望我的表单在刷新页面时不会弹出重新加载/重新发送框。在我的旧网站上,我使用重新header("Location: current page")加载页面并摆脱该框,然后使用会话变量我能够处理错误和其他内容。
我怎样才能实现让我的 Yii 表单具有相同的功能,无论是使用 php 标头重定向还是 javascript ?
我是 yii 的新手,所以我还没有完全理解它是如何工作的,但我已经了解了我认为的基础知识。
我想在我的模型上使用搜索功能上的CDbCriteria实现此查询
这是查询
select t.*
from transaksi t, stock_cabang sc, cabang c
where t.id_stock_cabang = sc.id
and sc.id_cabang = c.id and c.id =1;
Run Code Online (Sandbox Code Playgroud)
这是我的功能
public function search() {
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('jumlah_masuk', $this->jumlah_masuk);
$criteria->compare('id_user', $this->id_user);
$criteria->compare('id_stock_cabang', $this->id_stock_cabang);
$criteria->compare('waktu', $this->waktu, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Run Code Online (Sandbox Code Playgroud)
请帮我...
如何使用hasAttribute自定义字段?
代码:
if ($model->hasAttribute($attribute)) {
...
} else {
$this->_sendResponse(400, 'Parameter \''.$attribute.'\' is not supported.');
}
Run Code Online (Sandbox Code Playgroud)
例子
模型.php
class Model extends CActiveRecord
{
public $customField;
...
Run Code Online (Sandbox Code Playgroud)
代码:
$model = new Model;
$model->hasAttribute('customField'); // Returns False.
Run Code Online (Sandbox Code Playgroud) 我有下面的代码以 Yii 形式显示日历输入。
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name' => 'publish_date',
'attribute' => 'publish_date',
'model'=>$model,
'options'=> array(
'dateFormat' =>'yy-mm-dd',
'defaultDate' => '2014-3-4',
'altFormat' =>'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'appendText' => 'yyyy-mm-dd',
),
));
?>
Run Code Online (Sandbox Code Playgroud)
默认值适用于日历,但我想在表单渲染时默认在日历输入中显示它。
我该怎么做?
我通过 yii2 create 命令传递原始 sql,但我总是得到一个错误,属性必须是实现 QueryInterface 的类的实例,例如 yii\db\Query 或其子类
这是代码
$sql ="
SELECT
tblpritems.PRlineID
, tblpritems.Tracking_Code
, tblpritems.Description
, tblpritems.Quantity
, tblpritems.Unit_Price
, tblpritems.Extended_price
, tblpritems.PRID
, tblpritems.pr_solicitation_id
, tblpritems.date_item_received
, tblpritems.Quantity_received
, tblpritems.Remarks_on_receipt
, tblpritems.Received_by
FROM
prts.tblpritems
INNER JOIN prts.tblpr
ON (tblpritems.PRID = tblpr.PRID)
INNER JOIN prts.tblprsolicitations
ON (tblprsolicitations.PRID = tblpr.PRID) AND (tblpritems.pr_solicitation_id = tblprsolicitations.pr_solicitation_id)
INNER JOIN prts.tblprsuppliers
ON (tblprsuppliers.pr_solicitation_id = tblprsolicitations.pr_solicitation_id)
INNER JOIN prts.tblpo
ON (tblpo.pr_supplier_id = tblprsuppliers.pr_supplier_id)
where tblpr.PRID=".$val." and tblpo.PO_Status_ID=7 and item_received_status=0
";
$connection = Yii::$app->getDb(); …Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法只获取属性名称?在示例中,我的意思是:id, name, user。我不需要值,只需要属性名称。
app\models\Data Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 1
[name] =>
[user] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 1
[name] =>
[user] =>
)
)
Run Code Online (Sandbox Code Playgroud)
谢谢!
如何创建一个具有来自另一个数组的默认值的模型。
我的意思是,如果我有一个像这样的数组:
[
0 => [
'remarks' => 'ACETONE - '
'material' => '70.00'
]
1 => [
'remarks' => 'Leak Test 1 Bar'
'material' => '13.50'
]
2 => [
'remarks' => 'Foot Valve Incapsulated O-Ring 1 pcs - Replace'
'material' => '6.70'
]
3 => [
'remarks' => 'Seal Teflon 3\" Bottom Valve - Replace'
'material' => '10.50'
]
4 => [
'remarks' => 'Gasket Carton Bottom Valve 4 Hole 2 pcs - Replace'
'material' => '14.60'
] …Run Code Online (Sandbox Code Playgroud) 这是表1
id1 Name
------------
1 value1
2 value2
Run Code Online (Sandbox Code Playgroud)
这是表2
id2 Name id1
---------------------
1 value1 2
2 value2 1
Run Code Online (Sandbox Code Playgroud)
这是表3
id3 Name id2
---------------------
1 value1 2
2 value2 1
Run Code Online (Sandbox Code Playgroud)
这是表4
id4 Name id3
---------------------
1 value1 2
2 value2 1
Run Code Online (Sandbox Code Playgroud)
我想用模型连接 Yii2中的上述 4 个表
select * from table1
left join table2 on table2.id2 = table1.id1
left join table3 on table2.id3 = table1.id2
left join table4 on table2.id4 = table1.id3
Run Code Online (Sandbox Code Playgroud) 我有两张带有设计主细节的桌子。
主表名为Format2006FlatFileMaster。
明细表名为Format2006FlatFileDetail。
我的目标是,我想使用一个字段作为键,值是他们的 master 的孩子。
我的意思是这样
$details = ArrayHelper::map(Format2006FlatFileMaster::find()->all(), 'alias', function ($model) {
$model->format2006FlatFileDetails;
});
Run Code Online (Sandbox Code Playgroud)
我得到这样的数据,(例如,这是虚拟数据)
'details' => [
'HDR01' => [
0 => app\models\utilities\Format2006FlatFileDetail#1
(
[yii\db\BaseActiveRecord:_attributes] => [
'id' => 1
'2006_flat_file_master_id' => 1
'field_name' => 'Record Lable'
'start' => 1
'width' => 5
'decimal' => null
'type' => 'C'
'mandatory' => 'Y'
'note' => 'HDR01'
]
[yii\db\BaseActiveRecord:_oldAttributes] => [
'id' => 1
'2006_flat_file_master_id' => 1
'field_name' => 'Record Lable'
'start' => 1
'width' …Run Code Online (Sandbox Code Playgroud) 当我要“查看”视图时,没有加载任何内容,甚至是包含 HTML 页眉和页脚的默认布局,因此它在源代码中不显示任何内容。
来自控制器的代码:
public function actionArt($type){
$compositions = Compositions::find()
->select(['id', 'name'])
->where('type' == $type)
->asArray()
->all();
return $this->render('art', ['compositions' => $compositions]);
}
public function actionView($id){
$info = Compositions::find()->where($id)->all();
$this->render('view', ['info' => $info]);
}
Run Code Online (Sandbox Code Playgroud)
视图中的代码:
“艺术”视图:
<?php
/* @var $compositions */
use yii\helpers\Html;
use yii\helpers\Url;
?>
<div class="site-art">
<?php foreach ($compositions as $composition){
Html::a(Html::encode($composition['name']), ['site/info', 'id' => $composition['id']]);
echo "<a href='" . Url::to(['site/view', 'id' => $composition['id']], true) . "'>{$composition['name']}</a><br>";
} ?>
</div>
Run Code Online (Sandbox Code Playgroud)
“查看”查看:
<?php
/* @var $info */
use yii\helpers\Html; …Run Code Online (Sandbox Code Playgroud) yii ×10
php ×8
yii2 ×6
arrays ×1
forms ×1
frameworks ×1
jquery-ui ×1
left-join ×1
sql ×1
yii-widgets ×1
zii-widgets ×1