E
E
EVOSandru62015-01-26 08:48:14
Yii
EVOSandru6, 2015-01-26 08:48:14

How to accept an array in yii?

Good afternoon, faced such problem:
1. in model created public property $faq_array;
2. set the rules for it with array('faq_array','safe')
3. the bottom line is that for the record I throw a few questions and answers - organized with the help of www.yiiframework.com/extension/jqrelcopy
4. in the behavior I prescribe:
public function beforeSave($event){
print_r($this->owner->faq_array);
...
}
This produces no results. But if instead of print_r I write:
print_r($_POST);
Then when I print the array, the one I need comes out:

[faq_array] => Array
        (
            [question] => Array
                (
                    [0] => Вопрос 1
                    [1] => Ответ 1
                )

            [answer] => Array
                (
                    [0] => Вопрос 2
                    [1] => Ответ 2
                )
)

Why doesn't the faq_array array get into the model until it's saved?
And another question is how fields can be organized in the form - the question is the answer, so that the structure of the incoming array changes to:
[faq_array] => Array
        (
             [0] =>Array
            (
               [question] => Вопрос 1
               [answer] => Ответ 1
            )   
            [1] =>Array
            (
               [question] => Вопрос 2
               [answer] => Ответ 2
            )   
         )

At the moment I have the following situation in the form:
<div class="alert alert-info copy_faq">
          <?php echo CHtml::label('Вопрос',''); ?>
          <?php echo CHtml::textField('faq_array[question][]',''); ?>
          <?php echo CHtml::label('Ответ',''); ?>
          <?php echo CHtml::textArea('faq_array[answer][]', ''); ?>
    </div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
andruxin, 2015-01-26
@andruxin

I hope there is?

$model = new Model;
$model->attributes  = $_POST;

then $model->faq_array will have what you need.

E
Eugene, 2015-01-26
@Nc_Soft

What is the event and owner in beforeSave?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question