-1

Its posible change model name in post/get?

I have model with large names, like "VerLargeModelName" and many parameters.

It does not fit in GET (query string limit).

Update:

i need just change generated inputs from CActiveForm (change LongModelName[a] to short[a])

Bogdan
  • 171
  • 3
  • 13
  • According to http://www.boutell.com/newfaq/misc/urllength.html your queries should be really long. Could you post an example? – Bfcm May 11 '15 at 10:26
  • I am not clear where you are getting the error, and how you are using the model name. If you render the view from the controller, the model name is what you define there. eg. public function actionContact() { $model = new ContactForm(); return $this->render('contact', [ 'model' => $model, //A longer model name is 'A_VeryVeryLongModelName' => $model ]); } } – gpr May 11 '15 at 10:49
  • @Bfcm no, i need URL ( if rename model to single charset - everything will be fine)" – Bogdan May 11 '15 at 10:50
  • @girish not a option, because CActiveForm used (e.g. echo $form->hiddenField($model, 'sort'); ) Error: URL length limits – Bogdan May 11 '15 at 10:53
  • I am still not clear what is the issue you are facing, Is it possible to put the POST request, using the firebug? – gpr May 11 '15 at 11:07
  • Its not issue, i have search form with model (VERY LARGE NAME) but somtimes i have very large GET query string (it depends on selected parametrs) Renaming model (VeryLargeModelNameString to "A") solve GET problem but model "A.php" - bad idea – Bogdan May 11 '15 at 11:34
  • Why are your names that long? http://stackoverflow.com/a/812962/980615, according to this answer you are able to put in 4000 characters before Apache stops. – Jelle de Fries May 11 '15 at 11:47
  • Assigning a long name to the models isn't good. You can simply choose a **reasonable** word for your models. Also, when you have large set of fields in your views, it's extremely better to use `POST` instead of `GET`. – hamed May 11 '15 at 11:50
  • 1. I use nginx. 2. names very long ( 20 models ) in application-level logic. 3. if i rename models - application level logic crash (not a option). still need rename feature on CActiveRecord – Bogdan May 11 '15 at 12:15
  • Related issue in yii2: https://github.com/yiisoft/yii/issues/470 – Bogdan May 11 '15 at 12:30
  • @hamed iam not **set** model name. Model names alredy set in production, and i can only use this names ( i need this feature in forms ) – Bogdan May 11 '15 at 13:48
  • Do you really need to rename the model? Can you not simply create the input fields with a shorter name. so `a['name']` and then after the post simple do `$VerLargeModelName->attributes = $_POST['a'];` in the controller? If this is a workable solution I could write it out as a full answer if you like. – Jeroen May 11 '15 at 14:54
  • @Jeroen possible, but i have multiple views with CActiveForm usage ( i.e. multiple view with multiple CActiveForm strings like `$form->hiddenField($model, 'sort');`). And i need **not rename the model**, just rename generated form inputs ( i.e. `name="LARGE[xx]"` to `name="small[xx]"`. – Bogdan May 11 '15 at 15:02

1 Answers1

1

You can just change the name. You can do this like this echo $form->textFieldBlock($model,'name',array('name' => 'x["name"]') or whatever you want. You could also create a class (widget) with does this for your.

class MyActiveForm extends CActiveForm {
    public function hiddenField($model, $attribute, $htmlOptions = array()) {
         if(isset($htmlOptions['shortName'])) {
            $htmlOptions['name'] = $htmlOptions['shortName'] . "[".$attribute."]";
            unset($htmlOptions['shortName']);
        }
        return parent::hiddenField($model, $attribute, $htmlOptions);
    }
}  

You change CActiveFrom from the widget to MyActiveForm. Then use $form->textFieldBlock($model,'name',array('shortName' => 'x'). You could also change the above code to always change to a shortname without the htmlOptions. So that it is always x. However you could not have two form at once in this case. Benifit is that you would not need to add array('shortName' => 'x') to all of them, but just change CActiveFrom to MyActiveForm. So that would save you time, but cost your flexibility (with you might need later on maybe).

You have to create a function offcourse for every input field you want to use from ActiveRecord. The name of the element would become x['name']

In the controller you could simply do $model->attributes = $_POST['x'].

Jeroen
  • 579
  • 5
  • 19