I am creating a CakePHP 2.3 application and cannot understand for life why saveAll continues to fail.
I have the following models:
Artist hasMany ArtistPreviewArtistPreview owned by Artist
I have no problem saving my Artist model using the save() function, however, as soon as I try to use the saveAll method, Cake seems to fail - this is crazy because I even completely deleted all of my model validation rules.
Here is my opinion, nothing crazy:
<?php echo $this->Form->create('Artist', array('inputDefaults' => array('class' => 'input-block-level'))); echo $this->Form->input('Artist.name', array('label' => __('Name'))); echo $this->Form->input('Artist.artist_section_id', array('label' => __('Section'), 'empty' => true)); ?> <label>Tags</label> <div class="checkboxes"> <?php echo $this->Form->input('Tag', array('label' => false, 'multiple' => 'checkbox', 'class' => false));; ?> </div> <?php echo $this->Form->input('Artist.website', array('label' => __('Website'))); echo $this->Form->input('ArtistPreview.0.url', array('label' => __('Artist Preview'))); echo $this->Form->input('Artist.description', array('label' => __('Description'))); ?> <?php echo $this->Form->submit(__('Add'), array('class' => 'btn btn-primary btn-large', 'div' => false)) . ' '; echo $this->Form->button(__('Cancel'), array('type' => 'button', 'class' => 'btn btn-large', 'div' => false, 'onclick' => 'closeDialog()')); echo $this->Form->end(); ?>
And here is my add controller method:
public function add() { $this->layout = 'form'; if (!$this->request->is('ajax')) { throw new ForbiddenException(); } if ($this->request->is('post')) { $this->setData($this->request->data['Artist'], array( 'user_id' => AuthComponent::user('id'), 'date' => $this->Date->gmt() )); if ($this->Artist->saveAll($this->request->data)) { $this->redirectAjax('/artists/view/' . $this->Artist->id); } } $this->set(array( 'title_for_layout' => 'Add Artist', 'artistSections' => $this->Artist->ArtistSection->find('list', array('order' => 'name')), 'tags' => $this->Artist->Tag->find('list', array('order' => 'name')) )); }
As I mentioned above, the saveAll method fails every time. I checked that it was getting output and Cake was outputting an empty <div> with the error-message class, but there was no message. Each field in the form receives one of these divs.
Now, when I change my code to use the save function instead of saveAll , everything will be saved correctly, except for the ArtistPreview data, which is to be expected. Even my HABTM tags are saved properly and nothing can be verified.
I have been using Cake since 1.3 days, so I am very familiar with it and even did what I ask in previous projects. I tend to be a mistake if I donβt miss something here. Any ideas?
EDIT
I also tried the saveAssociated method, and when trying to save it deep - the deep key. This will allow me to save the artist, but he will not save the associated data (ArtistPreview).
EDIT 2
As requested, here are two tables:
-- ----------------------------------------------------- -- Table `artists` -- ----------------------------------------------------- DROP TABLE IF EXISTS `artists` ; CREATE TABLE IF NOT EXISTS `artists` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT , `user_id` INT UNSIGNED NULL , `artist_section_id` INT UNSIGNED NULL , `artist_status_id` INT UNSIGNED NULL , `name` VARCHAR(200) NULL , `website` VARCHAR(500) NULL , `description` TEXT NULL , `date` DATETIME NULL , `comment_count` INT UNSIGNED NOT NULL DEFAULT 0 , PRIMARY KEY (`id`) , INDEX `FK_artists_users_idx` (`user_id` ASC) , INDEX `FK_artists__artist_sections_idx` (`artist_section_id` ASC) , INDEX `FK_artists__artist_statuses_idx` (`artist_status_id` ASC) , CONSTRAINT `FK_artists__users` FOREIGN KEY (`user_id` ) REFERENCES `users` (`id` ) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `FK_artists__artist_sections` FOREIGN KEY (`artist_section_id` ) REFERENCES `artist_sections` (`id` ) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `FK_artists__artist_statuses` FOREIGN KEY (`artist_status_id` ) REFERENCES `artist_statuses` (`id` ) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE = InnoDB; -- ----------------------------------------------------- -- Table `artist_previews` -- ----------------------------------------------------- DROP TABLE IF EXISTS `artist_previews` ; CREATE TABLE IF NOT EXISTS `artist_previews` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT , `artist_id` INT UNSIGNED NULL , `url` VARCHAR(500) NULL , `date` DATETIME NULL , PRIMARY KEY (`id`) , INDEX `FK_artist_previews__artists_idx` (`artist_id` ASC) , CONSTRAINT `FK_artist_previews__artists` FOREIGN KEY (`artist_id` ) REFERENCES `artists` (`id` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB;
Here is my setData function, which simply concatenates two arrays. Instead of using hidden fields, I set the fields in the controller that are not populated by the user:
public function setData(&$original, $new) { $original = array_merge($original, $new); }