Question

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)

In the _form view for project::actionCreate I've got the following:

<?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".

In client::actionCreate I want to do something like

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[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.

Était-ce utile?

La solution

I think the client array is put inside subarray 0 because you've added an array around the parameters. 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. 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. (http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail)

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

Autres conseils

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. In the end I just created the link the old fashion way, not using CHTML.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top