How to call a function in Cakephp 3.x

Asked

Viewed 571 times

4

I have a view add which is an open form called help desk (referring to function add, of course), and I have this function upload which is to attach file to request, which is linked with a Component, but I don’t want to leave them in view different, because if I just send a file through the view upload, the image "gets lost" and does not call. I need that within the function add I can call the function upload, to join the views. If I call you through $this->upload(); , or simply do all the checking within add, does not find the Component, returning me an error (which I will put below), I believe the conflict is in the request->date, but I don’t know if there’s a way to put it together the way I explained it.

    public function add()
{

    $post = $this->Posts->newEntity();

    if ($this->request->is(['post', 'put'])) {
        $this->Posts->patchEntity($post, $this->request->data);
            $post->user_id = $this->Auth->user('id');



        if ($this->Posts->save($post)) {
            $this->Flash->success(__('Chamado enviado'));
            return $this->redirect(['action' => 'listar']);
        }

        $this->Flash->error(__('Chamado nao enviado'));
    }


        $this->set(compact('post'));

}

public function upload()

{

    if ( !empty( $this->request->data ) ) {
        $this->Upload->send($this->request->data(['uploadfile']));
        return $this->redirect(['action'=>'add']);
    }

}

Component:

 public function send( $data )
{
    if ( !empty( $data) ) {
        if ( count( $data) > $this->max_files ) {
            throw new InternalErrorException("Error Processing Request. Max number files accepted is {$this->max_files}", 1);
        }

        foreach ($data as $file) {
            $filename = $file['name']; //linha 32, que da o erro abaixo;
            $file_tmp_name = $file['tmp_name']; //33
            $dir = WWW_ROOT.'img'.DS.'Anexos';
            $allowed = array('png', 'jpg', 'jpeg');
            if ( !in_array( substr( strrchr( $filename , '.') , 1 ) , $allowed) ) {
                throw new InternalErrorException("Error Processing Request.", 1);
            }elseif( is_uploaded_file( $file_tmp_name ) ){
                $filename = Text::uuid().'-'.$filename;

                $filedb = TableRegistry::get('Arquivos');
                $entity = $filedb->newEntity();
                $entity->filename = $filename;
                $filedb->save($entity);

                move_uploaded_file($file_tmp_name, $dir.DS.$filename);
            }
        }
    }
}

error calling upload();

Warning (2): Illegal string offset 'name' [APP/Controller\Component\UploadComponent.php, line 32]

Warning (2): Illegal string offset 'tmp_name' [APP/Controller\Component\UploadComponent.php, line 33]

view:

  <?php
        //essa é minha view add;
        echo $this->Form->input('id' );
        echo $this->Form->input('titulo');
        echo $this->Form->input('ip');
        echo $this->Form->input('mensagem');

    ?>
       //e essa é minha view upload, que eu gostaria de juntar com add;

      <?php echo $this->Form->create(null, ['type' => 'file']); ?>
      <label>Arquivos</label>
      <?php
      echo $this->Form->file('uploadfile.', ['multiple']);
      echo $this->Form->button('Anexar', ['action' => 'submit']);
      echo $this->Form->end();

       ?>

I’m going to raise this question because I haven’t solved it yet, follow the image of mine view, how I want to do, but if I do so (joining the functions add) gives the error I mentioned above. I can put a link directing to attach a file to another view, Well, saved in the folder and in the bank, but the file is lost, is not tied to the request, and it is not possible that the cake don’t let me do it.

inserir a descrição da imagem aqui

  • you checked if within the $date comes the parameter you want ? I say the 'name' and the 'tmp_name' ?

  • within $data is coming the "add" parameter, here is the question, how to make the $ "upload date" search for 'name' and 'tmp_name' being inside the "add".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.