简体   繁体   English

来自$ _GET的Yii质量分配未按预期工作

[英]Yii mass assignment from $_GET not working as expected

I'm trying to perform a mass assignment of 2 variables I'm sending via GET to another model::controller (from project::actionCreate to client::actionCreate) 我正在尝试通过GET向另一个模型:: controller发送2个变量的大量分配(从project :: actionCreate到client :: actionCreate)

In the _form view for project::actionCreate I've got the following: 在project :: actionCreate的_form视图中,我得到了以下内容:

<?php echo "&nbsp;".Chtml::link('+New client',array('client/create',array('Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id))));?>

With the goal of creating an array "Client" with attributes "redir" and "redirId". 目标是创建一个具有“redir”和“redirId”属性的数组“Client”。

In client::actionCreate I want to do something like 在client :: actionCreate中我想做类似的事情

if(isset($_GET['Client']))
{
    $model->attributes=$_GET['Client'];
}

Now I noticed that my $_GET var puts client inside subarray 0, so I've tried this with 现在我注意到我的$ _GET var将客户端置于子数组0中,所以我试过这个

$_GET[0]['Client']

as well, but no luck. 同样,但没有运气。 However if I manually assign the variables like this: 但是,如果我手动分配这样的变量:

$model->redir = $_GET[0]['Client']['redir'];
$model->redirId = $_GET[0]['Client']['redirId'];

Then it works. 然后它工作。

Any idea what is up? 有什么想法吗? The goal is to allow someone to create a new client while creating/updating a project record, by sending them to client::actionCreate, but redirecting them back to their original project::actionCreate if they were linked there from my "+New Client" link. 目标是允许某人在创建/更新项目记录时创建新客户端,方法是将它们发送到client :: actionCreate,但是如果它们是从我的“+新客户端”链接到它们,则将它们重定向回原始项目:: actionCreate “链接。

I think the client array is put inside subarray 0 because you've added an array around the parameters. 我认为client数组放在子数组0中,因为你在参数周围添加了一个数组。 Try removing the array like the following: 尝试删除以下数组:

<?php 
    Chtml::link('+New client',array('client/create', 'Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id)));
?>

I don't know what your model looks like but if the fields aren't assigned they are probably not safe . 我不知道你的模型是什么样的,但是如果没有分配字段,它们可能safe You can make them safe by adding them to the rules part of your model. 您可以通过将它们添加到模型的规则部分来使它们安全。 Or you could try the following, by specifying the false parameter it will be possible to assign values to unsafe attributes. 或者您可以尝试以下操作,通过指定false参数,可以将值分配给不安全的属性。 ( http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail ) http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail

$model->setAttributes($_GET['Client'], false);

I am not sure creating a link like you want is possible. 我不确定创建一个你想要的链接是可能的。 I have asked something similar some time ago Yii link with [ as a parameter I just could never get the link to how I wanted it. 我前段时间曾问过类似的东西Yii链接[作为参数我根本无法获得我想要的链接。 In the end I just created the link the old fashion way, not using CHTML. 最后我刚刚创建了旧时尚方式的链接,而不是使用CHTML。

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

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