'yii2 and mssql insert varbinary into model

Hi everyone I have a sql server table that contains fields that are of type varbinary. The table model sees the field as a string, when it tries to save it it gives me the following error

SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Implicit conversion of data type from nvarchar to varbinary(max) is not allowed. To execute the query, use the CONVERT function.

I need to do this insert because I am saving files in the db, where they are already in varbinary format.

the rules of the

 */
public function rules()
{
    return [
        [['id_agenda'], 'integer'],
        [['nota'],'string'],
        [[ 'f_content', 'nome_file', 'uplfile', 'file'], 'string'],
        [['nome_file', 'estensione'], 'required'],
        [['descrizione'], 'string', 'max' => 200],
        [['estenzione'], 'string', 'max' => 30],
    ];

the controller save

            $model = new AgendaFiles();
        if ($model->load(Yii::$app->request->post())){
        print_R(  (Yii::$app->request->post())) ;
        $model->id_agenda=$_post['id_agenda'];
        $model->file=UploadedFile::getInstance($model, 'file');
        $model->nome_file=basename($model->file );
        $model->estenzione=pathinfo($model->file ,PATHINFO_EXTENSION);
       //$model->save();
        $model->upload();
        //$model->save();
        $tmf='0x'.bin2hex(file_get_contents(Yii::getAlias('@webroot').'/uploads/'. 
        str_replace(' ', '_',$model->nome_file) ));
        $model->f_content=$tmf;
        if ($model->save(false)) {
        echo 'save ';
        }
        else {
            echo 'notsave';
        }

any ideas? tks a lot!



Solution 1:[1]

solved using this code in the action

public function actionAjupd()
{
    $this->getuser();

    $model = new Allfiles();
  //  mb_internal_encoding( 'UTF-8' );

    if ($model->load(Yii::$app->request->post())){
        $x=$model->f_content;
        $tmpfile=UploadedFile::getInstance($model, 'f_content');
       $h=
       (binary)(file_get_contents($tmpfile->tempName));
      // var_dump($model); 
       $model->nomefile=basename($tmpfile);
       $model->origine='S';
       $model->estensione=pathinfo($tmpfile ,PATHINFO_EXTENSION);
        $model->setAttribute('f_content','');
       $model->save(false); 
       $id=$model->getPrimaryKey();
       $connection = Yii::$app->getDb();
       $command = $connection->createCommand("
       update all_files set f_content=convert(
        VARBINARY(max) ,  
        :contenuto ,1) where id=:id
       
       ",[':id'=>$id,':contenuto'=>'0x'.bin2hex($h)]);
        $command->execute();
       //\Yii::$app->response->format = Response::FORMAT_JSON;
       return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
    }

    return $this->renderAjax('create', [
        'model' => $model,
    ]);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marco cardinale