Save Various Id in related table Cakephp3

0

Hello everyone, as you will see I have the following model with the tables Advertisements and AdvertisementsPictures here your models

class AdvertisementsTable extends Table{
public function initialize(array $config) {
    parent::initialize($config);
    $this->table('advertisements');
    $this->displayField('id_advertisement');
    $this->primaryKey('id_advertisement');
    $this->entityClass('App\Model\Entity\advertisement');

    $this->hasMany('AdvertisementsImagenes',[
        'className' => 'AdvertisementsImagenes',
        'foreignKey' => 'id_advertisement',
    ]);
  }

Table AdvertisementsPictures

class AdvertisementsImagenesTable extends Table{

public function initialize(array $config) {
    parent::initialize($config);
    $this->table('advertisements_imagenes');
    $this->displayField('id_advertisement');
    $this->primaryKey('id_advertisement');
    $this->entityClass('App\Model\Entity\AdvertisementImagen');

    $this->belongsTo('Advertisements',[
        'foreignKey' => 'id_advertisement',
        'joinType' => 'INNER'
    ]);
    }
  }

Well well it is a one to many relationship and then in my advertisements_imagenes table I keep the id of the advertisements table, because when I do the registration for a record it does it very well now the topic is when there are several images that are related to a single id for which my controller AdvertisementsController.php tries to do this

public function publicadd() {
    $publicadd = $this->Advertisements->newEntity();
    if($this->request->is('post')){
        $publicadd = $this->Advertisements->patchEntity($publicadd,$this->request->data,['associated' => ['AdvertisementsImagenes']]);
        $images = $this->request->data['advertisements_imagenes']['name_imagen'];
        foreach ($images as $image){
            $publicadd->advertisements_imagenes[0]->name_imagen = $image['name'];
            new Folder(WWW_ROOT . 'anuncios',true,0755);
            $mv = new File($image['tmp_name']);
            $mv->copy(WWW_ROOT . 'anuncios/'.$image['name']);
        }
        $publicadd->id_user = $this->Auth->user('id_user');
        if ($this->Advertisements->save($publicadd)) {
            $this->Flash->success('Anuncio Publicado');
            $this->redirect(['controller' => 'Home','action' => 'index']);
        }  else {
            $this->Flash->error('Error al Publicar');
        }

What I need is to save the names of the images in the BD that are made to an id help please Graciassss

    
asked by Jonathan Cunza 16.08.2016 в 19:11
source

1 answer

0

In the end this was my solution I had to restructure my way of inserting the data that is how I did it

public function publicadd() {

    $publicadd = $this->Advertisements->newEntity($this->request->data);
    if($this->request->is('post')){
        $publicadd->id_user = $this->Auth->user('id_user');
        if ($this->Advertisements->save($publicadd)) {
            $id = $publicadd->id_advertisement;
        }
        $publicaddimg = $this->AdvertisementsImagenes->query();
        $images = $this->request->data['name_imagen'];
        foreach ($images as $image){
            $data = [
                'id_advertisement' => $id,
                'name_imagen' => $image['name']
            ];
            $publicaddimg->insert(['id_advertisement','name_imagen'])
                    ->values($data);
            new Folder(WWW_ROOT . 'anuncios',true,0755);
            $mv = new File($image['tmp_name']);
            $mv->copy(WWW_ROOT . 'anuncios/'.$image['name']);
        }
        $publicaddimg->execute();
        $this->Flash->success('Anuncio Publicado');
        $this->redirect(['controller' => 'Home','action' => 'index']);
    }

If someone out there has a better solution, please post it. Graciasss

    
answered by 25.08.2016 / 06:23
source