saveAll()在CakePHP 2.3.1中失败
我正在构建一个CakePHP2.3应用程序,我一辈子都搞不懂为什么saveAll总是失败 我有以下型号:saveAll()在CakePHP 2.3.1中失败,cakephp,cakephp-2.3,Cakephp,Cakephp 2.3,我正在构建一个CakePHP2.3应用程序,我一辈子都搞不懂为什么saveAll总是失败 我有以下型号: Artist有许多ArtistPreview ArtistPreviewbelongsToArtist 我用save()函数保存我的Artist模型没有问题,但是当我尝试使用saveAll方法时,Cake似乎没有通过验证-这很疯狂,因为我甚至已经从模型中完全删除了所有验证规则 我的观点是,没什么疯狂的: <?php echo $this->Form->creat
有许多Artist
ArtistPreview
belongsToArtistPreview
Artist
save()
函数保存我的Artist
模型没有问题,但是当我尝试使用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();
?>
如上所述,saveAll方法每次都失败。我已经检查了获得输出的内容,Cake使用错误消息
类输出了一堆空白的
s,但是没有消息。表单上的每个字段都有一个div
现在,当我将代码更改为使用save
功能而不是saveAll
时,除了ArtistPreview
数据之外,所有内容都得到了正确保存,这是意料之中的。甚至我的HABTM标签也被正确保存,没有任何东西不能通过验证
我从1.3天就开始使用蛋糕了,所以我对它非常熟悉,甚至完全按照我在以前项目中的要求做了。我倾向于这是一个错误,除非我在这里遗漏了什么。有什么想法吗
编辑 我还尝试了
saveAssociated
方法,并在尝试保存时将deep
键设置为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
函数,它只是合并了两个数组。我没有使用隐藏字段,而是在控制器中设置了不由用户输入填充的字段:
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.')
)
);
}
}
类艺术家扩展AppModel{
public$belongsTo=数组(
“用户”,
"艺术组",,
“艺人身份”
);
public$hasMany=array(
“艺术图片”,
'Artist'//validate=array(
'name'=>数组(
'rule'=>'notEmpty',
'消息'=>。'('艺术家名称不能为空')
),
“网站”=>数组(
'规则'=>'url',
'message'=>\'('必须是有效的URL'),
“allowEmpty”=>true
),
“艺术家\u节\u id”=>数组(
'rule'=>'notEmpty',
'message'=>\uuuu('部分是必需的')
)
);
}
}
感谢大家花时间看了这个问题,尽管我发布了错误的信息。您确定它没有验证吗?您是否尝试过通过$this->Model->validate()手动验证?没有验证规则,所以应该没有什么要验证的!我可以尝试
$this->Model->validate
,但我必须单独设置模型数据。当我用deep
键将数组传递给saveAll/saveAssociated方法时,至少艺术家被保存了。但它只是不想保存相关数据。我要走了用一个干净的Cake安装来执行测试,看看它是否是一个bug。你能显示你正在使用的表的模式吗?这可能会有一些启发吗?另外,函数$this->setData
做什么?+1因为我从你的代码中学到了一些东西。
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.')
)
);
}
}