gpt4 book ai didi

cakephp - saveAll() 使用 CakePHP 2.3.1 失败

转载 作者:行者123 更新时间:2023-12-01 03:58:40 26 4
gpt4 key购买 nike

我正在构建一个 CakePHP 2.3 应用程序,并且终其一生都无法弄清楚为什么 saveAll 总是失败。

我有以下型号:

  • Artist hasMany ArtistPreview
  • ArtistPreview属于 Artist

  • 我保存我的 Artist 没有问题带 save() 的模型功能,但是一旦我尝试使用 saveAll方法,Cake 似乎验证失败 - 这很疯狂,因为我什至从我的模型中完全删除了所有验证规则。

    这是我的观点,没什么疯狂的:
    <?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();
    ?>

    这是我的 add Controller 方法:
    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'))
    ));
    }

    正如我上面提到的,saveAll 方法每次都失败。我检查了输出的内容,而 Cake 输出的是一堆空白 <div> s 与 error-message类,但没有消息。表单上的每个字段都会获得这些 div 之一。

    现在,当我更改代码以使用 save函数而不是 saveAll ,除了 ArtistPreview 之外,所有内容都被正确保存数据,在意料之中。甚至我的 HABTM 标签也被正确保存,没有任何问题无法通过验证。

    我从 1.3 天开始就一直在使用 Cake,所以我对它非常熟悉,甚至完全按照我在之前项目中的要求完成了任务。我倾向于这是一个错误,除非我在这里遗漏了一些东西。有任何想法吗?

    编辑

    我也试过 saveAssociated方法,以及设置 deep key true尝试保存时。这将让我保存艺术家,但不会保存相关数据 (ArtistPreview)。

    编辑 2

    根据要求,这里有两个表:
    -- -----------------------------------------------------
    -- 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;

    这是我的 setData函数,它只是合并两个数组。我没有使用隐藏字段,而是在 Controller 中设置了不由用户输入填充的字段:
    public function setData(&$original, $new) {
    $original = array_merge($original, $new);
    }

    最佳答案

    所以我终于想通了这一点,哇,这是一个菜鸟的错误。我想我昨天写代码的时候一定是半睡半醒。问题出在我的模型中:

    class Artist extends AppModel {
    public $belongsTo = array(
    'User',
    'ArtistSection',
    'ArtistStatus'
    );

    public $hasMany = array(
    'ArtistPicture',
    'Artist' // <--- problem was here, had 'Artist' instead of 'ArtistPreview'
    );

    public $hasAndBelongsToMany = array(
    'Tag'
    );

    public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);

    $this->validate = array(
    'name' => array(
    'rule' => 'notEmpty',
    'message' => __('Artist name cannot be blank.')
    ),
    'website' => array(
    'rule' => 'url',
    'message' => __('Must be a valid URL.'),
    'allowEmpty' => true
    ),
    'artist_section_id' => array(
    'rule' => 'notEmpty',
    'message' => __('Section is required.')
    )
    );
    }
    }

    感谢那些花时间看这个问题的人,即使我发布了错误的信息。

    关于cakephp - saveAll() 使用 CakePHP 2.3.1 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15189885/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com