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