Tras estar "brujuleando" por foros, blogs, etc, esta es mi primera toma de contacto decidida con cakephp. El punto de partida es el codigo generado con bake (cake 1.1.13).
Estoy intentando registrar datos en una tabla HATBM, y no se hacerlo :-( Se que es un tema recurrente, y por eso os pido perdon de antemano O:-) Tengo un modelo "artista" y otro "contacto". Desde el index de artistas puedo mostrar la informacion de uno de ellos "view", y una vez aqui le añado un contacto que creo en el momento. Es decir, estoy en Contact.add (artist_id). Grabo en la tabla contacts, pero no en artists_contacts. En en manual parece que todo esto deberia ser automatico ¿? He estado buscando en el grupo en ingles y en los blogs de referencia y vi que las funciones de rossoft ( http://rossoft.wordpress.com/2006/08/23/working-with-habtm-associations/) para trabajar con asociaciones HABTM era lo que necesitaba. Pero tampoco Aqui os pongo mis modelos, el controlador de Contacts donde grabo y las trazas. En estas trazas podeis ver que (creo yo) tengo toda la informacion necesaria... a ver si algun alma caritativa me puede dar alguna indicación... Contact --------------------------------- <?php class Contact extends AppModel { var $name = 'Contact'; //$belongsTo eliminadas var $hasAndBelongsToMany = array( 'Artist' => array('className' => 'Artist', 'joinTable' => 'artists_contacts', 'foreignKey' => 'contact_id', 'associationForeignKey' => 'artist_id', ), ); } ?> Artist ------------------------------------- <?php class Artist extends AppModel { var $name = 'Artist'; var $hasAndBelongsToMany = array( 'Contact' => array('className' => 'Contact', 'joinTable' => 'artists_contacts', 'foreignKey' => 'artist_id', 'associationForeignKey' => 'contact_id', ) ); } ?> Codigo de contacts_controller.php ---------------------------------- $this->cleanUpFields(); $this->traza( $this->name, $this->data ); if($this->Contact->save($this->data)) { $this->Session->setFlash('The Contact has been saved'.$this->data); $this->traza( $this->name,$this->Contact->getLastInsertId(). ' Artist '. $this->data['Artist']['Artist']); $this->Contact->addAssoc($this->Contact->getLastInsertId(), 'Artist', $this->data['Artist']['Artist']); $this->redirect('/contacts/index'); ----------------------------------- Trazas --------------------------------------------------- 2007-03-25 17:27:15 Debug: ContactsControler Contacts 2007-03-25 17:27:15 Debug: Array ( [Contact] => Array ( [country_id] => 5 [state_id] => 2 [name] => nombre contacto [address] => direccion [city] => [postcode] => [telephone] => [email] => [fax] => [observation] => ) [Artist] => Array ( [Artist] => 2 ) ) 2007-03-25 17:27:15 Debug: ContactsControler Contacts 2007-03-25 17:27:15 Debug: 854 Artist 2 ----------------------------------------------------- Modelo de datos ---------------- CREATE TABLE artists ( id SMALLINT UNSIGNED NOT NULL, code CHAR(30) NOT NULL, name VARCHAR(100) NOT NULL, surname VARCHAR(100) NULL, pseudonym VARCHAR(30) NULL, born VARCHAR(75) NULL, death VARCHAR(75) NULL, rights CHAR(40) NULL, deleted BOOL NULL, created DATETIME NULL, updated DATETIME NULL, PRIMARY KEY(id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; CREATE TABLE artists_contacts ( artist_id SMALLINT UNSIGNED NOT NULL, contact_id SMALLINT UNSIGNED NOT NULL, created DATETIME NULL, PRIMARY KEY(artist_id, contact_id), INDEX autores_has_contactos_FKIndex1(artist_id), INDEX autores_has_contactos_FKIndex2(contact_id), FOREIGN KEY(artist_id) REFERENCES artists(id) ON DELETE NO ACTION ON UPDATE NO ACTION, FOREIGN KEY(contact_id) REFERENCES contacts(id) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; CREATE TABLE contacts ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, state_id SMALLINT UNSIGNED NOT NULL, country_id SMALLINT UNSIGNED NOT NULL, name VARCHAR(100) NULL, address VARCHAR(150) NULL, city VARCHAR(40) NULL, postcode VARCHAR(8) NULL, telephone CHAR(12) NULL, email CHAR(30) NULL, fax CHAR(12) NULL, observation VARCHAR(200) NULL, created DATETIME NULL, updated DATETIME NULL, PRIMARY KEY(id), INDEX contacts_FKIndex1(state_id), INDEX contacts_FKIndex2(country_id), FOREIGN KEY(state_id) REFERENCES states(id) ON DELETE NO ACTION ON UPDATE NO ACTION, FOREIGN KEY(country_id) REFERENCES countries(id) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; -- --------------------------------------------------- Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information? T.S.Elliot --~--~---------~--~----~------------~-------~--~----~ Has recibido este mensaje porque estás suscrito a Grupo "CakePHP-es" de Grupos de Google. Si quieres publicar en este grupo, envía un mensaje de correo electrónico a [email protected] Para anular la suscripción a este grupo, envía un mensaje a [EMAIL PROTECTED] Para obtener más opciones, visita este grupo en http://groups.google.com/group/CakePHP-es?hl=es. -~----------~----~----~----~------~----~------~--~---
