S
S
Sergey Beloventsev2016-10-09 22:49:33
Yii
Sergey Beloventsev, 2016-10-09 22:49:33

Explain why this strange behavior when saving?

here is the actual code

$basePath='page/2016/10/';
    $field='id_page';
    $id=1;
    $name='podborka_vecher_01.jpg,%, podborka_vecher_17.jpg'
    
    public function baseSave($basePath,$field,$id,$name){
            $transliterator= new Translit();
            $bp = array();
            if(preg_match('/,%,/',$name)){
                $mathes=explode(',%,',$name);
                foreach ($mathes as $math){
                    $bp[] = $basePath;
                    $names=$transliterator->traranslitImg($math);
                    $theImage= Image::findOne(['path'=>$basePath, 'name'=>$name ]);
                    if(!isset($theImage)){
                        $this->path=$basePath;
                        $this->$field=$id;
                        $this->name=$names;
                        $this->insert();
                    }
                }
            }else{
                $names=$transliterator->traranslitImg($name);
                $theImage= Image::findOne(['path'=>$basePath, 'name'=>$name ]);
                if(!isset($theImage)) {
                    $this->path = $basePath;
                    $this->$field = $id;
                    $this->name = $names;
                    $this->save();
                }
            }
        }

I am getting the following error
SQLSTATE[HY000]: General error: 1364 Field 'path' doesn't have a default value
    The SQL being executed was: INSERT INTO `abh_image` (`name`) VALUES ('-podborka_vecher_17.jpg')

I don't understand where to work with this
$this->path=$basePath;
    $this->$field=$id;

and why it only works $this->name=$names;
$this->$field = $idhere the $fieldname of the model field comes in in this case 'id_page'$this->paththis is the model field here they are all
'id', 'id_gods', 'id_post', 'id_cat', 'id_page'  ,'path'  ,'name'
the bottom line is that at the first iteration of foreach everything works on the second iteration, such an error appears.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-10-10
@Sergalas

1. you $basePath='page/2016/10/';or public $basePath='page/2016/10/';?
2. What's in your rules?
3. For an accurate answer - lay out the entire model, not a piece of it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question