E
E
EVOSandru62015-07-16 13:52:23
Yii
EVOSandru6, 2015-07-16 13:52:23

Why didn't safeOnly in setAttributes work in Yii?

Good afternoon,
There is a Bid model with a certain number of properties that are not in the database, but are necessary in afterSave

class Bid extends ActiveRecord
{
    public $adressFrom;
    public $adressTo;
    public $placeFrom;
    public $placeTo;
    public $categoryCargo;
    public $timeStart;
    public $timeFinish;
    public $placeStart;
    public $placeFinish;

...

public function rules()
  {
    return
        [
             ...
['placeStart, placeFinish, adressStart, adressFinish, timeStart, timeFinish, categoryCargo, placeFrom, placeTo, adressFrom, adressTo, description, description_en, description_kz', 'safe'],

...

There is such a problem in the create action, attributes cannot be assigned in bulk:
...
if(isset($_POST['Bid']))
        {
            $model->setAttributes($_POST['Bid'], false);   // safeOnly
            //My::printArr($_POST['Bid']);
            My::printArr($model->attributes);
            // die();
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }
...

Here is this print_r:
My::printArr($model->attributes);
Does not display fields that are not related to the table in the database for some reason:
Array
(
    [urgency] => 2
    [wish_price] => 5000
    [description] => 
    [id] => 
    [customer_id] => 
    [driver_id] => 
    [views] => 
    [status] => 
    [name] => 
    [name_kz] => 
    [name_en] => 
    [name_alias] => 
    [description_en] => 
    [description_kz] => 
    [meta_d] => 
    [meta_k] => 
    [order_sort] => 
    [sys_date] => 
    [sys_date_update] => 
    [sys_user] => 
    [exist] => 
    [shifr] => 
    [name_full] => 
    [name_full_kz] => 
)

Everyone except them.
The fields for these properties are similarly written in the form:
<?php echo $form->labelEx($model,'adressFrom'); ?>
<?php echo $form->textField($model,'adressFrom',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'adressFrom'); ?>

What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimon, 2015-07-16
@Glimor

wrote the rules incorrectly, it must be so

['placeStart', 'placeFinish', 'adressStart', 'adressFinish', 'timeStart', 'safe'],

or so
[['placeStart', 'placeFinish', 'adressStart', 'adressFinish', 'timeStart'], 'safe'],
that is, each attribute separately, in quotes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question