save()返回false,但CakePHP中没有错误

时间:2010-02-22 22:07:44

标签: cakephp cakephp-appmodel

我的调试值设置为2,它显示所有查询,除了我需要的查询。

我有Items控制器方法在User模型中调用此方法(项目属于用户):

function add_basic($email, $password) {
    $this->create();

    $this->set(array(
        'email' => $email,
        'password' => $password
    ));

    if($this->save()) {
        return $this->id;
    }
    else {
        return false;
    }
}

我已确认$email$password正确传递到函数中(并使用合法数据填充)。 emailpasswordUser模型中字段的名称。

我还确认在$this->save()上它正在返回false,但是当我查看发生这种情况的页面时,调试中没有打印查询,并且没有抛出错误,所以我不知道什么是错的。

关于如何查看错误或者为什么查询似乎没有被执行的任何想法?

这很奇怪,因为在此之后,我有另一个模型以完全相同的方式保存数据,它顺便说一句。

8 个答案:

答案 0 :(得分:41)

这可能会为您提供所需的信息(假设由于数据无效而无法保存):

if(!$this->save()){
    debug($this->validationErrors); die();
}

答案 1 :(得分:13)

包含validationErrors数组

if ($this->save()) {
    return $this->ModelName->id;
}
else {
    debug($this->ModelName->invalidFields());
    return false;
}

答案 2 :(得分:10)

您是否在模型或应用模型中获得了beforeValidate()beforeSave()方法?如果,他们回归真实吗?如果不这样做,请使用调试器,在您的IDE中的cake / libs / models / model.php save()方法的顶部设置一个断点,然后逐步执行代码,直到它返回false。没有添加die('here');次来电。

答案 3 :(得分:8)

试试这个:

if ($this->save()) {
    return $this->id;
}
else {
    var_dump($this->invalidFields());
    return false;
}

答案 4 :(得分:3)

请务必查看表格:

  • ID是否启用了自动增量功能?
  • id是您的主键吗?

auto_increment问题杀了我。 简单的检查方法:如果您的任何行ID = 0,则可能禁用auto_increment。

答案 5 :(得分:1)

@cakePHP 3.6:保存实体后,所有验证错误都将存储在实体本身上。它可以通过getErrors()方法读取。

\Cake\Log\Log::debug($this->ModelName->getErrors());

如果您已包含库

use \Cake\Log\Log;

然后您可以访问以下错误:

Example: log::debug($contactRelationship->getErrors());

参考:https://book.cakephp.org/3.0/en/orm/entities.html

答案 6 :(得分:0)

CakePHP无法报告任何 var argv = require('yargs').argv; var usehtml = true; var usertl = argv.usertl; var usescss = argv.usescss; var gulp = require('gulp'), $ = require('gulp-load-plugins')(), gutil = require('gulp-util'), gulpsync = require('gulp-sync')(gulp), path = require('path'), glob = require('glob'), del = require('del'), runSequence = require('run-sequence'), config = require('./gulp.config')($, usehtml); // production mode var isProduction = false; //--------------- // TASKS //--------------- // APP LESS gulp.task('styles', function () { log('Compiling styles to CSS..'); var stylesSrc = usescss ? config.scss.styles : config.less.styles; return gulp.src(stylesSrc) .pipe(isProduction ? gutil.noop() : $.sourcemaps.init()) .pipe(usescss ? $.sass() : $.less()) .on("error", handleError) .pipe($.if(usertl, $.rtlcss())) .pipe(isProduction ? $.minifyCss({processImport: false}) : gutil.noop()) .pipe(isProduction ? gutil.noop() : $.sourcemaps.write()) .pipe(gulp.dest(config.distCSS)); }); // BOOSTRAP gulp.task('bootstrap', function () { log('Compiling Bootstrap..'); var bsSrc = usescss ? config.scss.bootstrap : config.less.bootstrap; return gulp.src(bsSrc) .pipe(isProduction ? gutil.noop() : $.sourcemaps.init()) .pipe(usescss ? $.sass() : $.less()) .on("error", handleError) .pipe($.if(usertl, $.rtlcss())) .pipe(isProduction ? $.minifyCss({processImport: false}) : gutil.noop()) .pipe(isProduction ? gutil.noop() : $.sourcemaps.write()) .pipe(gulp.dest(config.distCSS)); }); // HTML gulp.task('markup', ['index', 'views']); gulp.task('views', buildMarkup(config.html.views, config.dist)); gulp.task('index', ['templatecache'], buildMarkup(config.html.index, '.', false, true)); gulp.task('templatecache', ['clean-scripts'], buildMarkup(config.html.templates, config.dist + 'js', true)); // SERVER // ----------------------------------- gulp.task('webserver', function () { log('Starting web server.. '); return gulp.src(config.webserver.webroot) .pipe($.webserver(config.webserver)); }); //--------------- // WATCH //--------------- // Rerun the task when a file changes gulp.task('watch', function () { log('Starting watch with live reload ...'); $.livereload.listen(); if (usescss) gulp.watch([config.scss.watch, config.scss.styles], ['styles']); else gulp.watch([config.less.watch, config.less.styles], ['styles']); if (usescss) gulp.watch(config.scss.bootstrap, ['bootstrap']); else gulp.watch(config.less.bootstrap, ['bootstrap']); gulp.watch(config.html.all, ['markup']); gulp.watch(config.html.templates, ['templatecache']); gulp .watch([].concat(config.less.watch, config.html.views, config.html.templates, config.js)) .on('change', function (event) { setTimeout(function () { $.livereload.changed(event.path); }, 1400); }); }); /** * Clean */ gulp.task('clean', ['clean-scripts', 'clean-styles', 'clean-markup']); gulp.task('clean-scripts', function (cb) { var js = config.distJS + '/*{js,map}'; clean(js, cb); }); gulp.task('clean-styles', function (cb) { var css = config.distCSS + '/*{css,map}'; clean(css, cb); }); gulp.task('clean-markup', function (cb) { var html = ['index.html', config.dist + 'views/']; clean(html, cb); }); gulp.task('clean-build', function (cb) { log('Removing development assets'); var delFiles = [ config.distJS + '/' + config.tplcache.file, config.distCSS + '/bootstrap.css', config.distCSS + '/styles.css' ]; clean(delFiles, cb); }); /** * vet the code and create coverage report */ gulp.task('lint', function () { log('Analyzing source with JSHint'); return gulp .src(config.lintJs) .pipe($.jshint()) .pipe($.jshint.reporter('jshint-stylish', {verbose: true})) .pipe($.jshint.reporter('fail')); }); //--------------- // Visualizer report //--------------- gulp.task('plato', function (done) { log('Analyzing source with Plato'); log('Browse to /report/plato/index.html to see Plato results'); startPlatoVisualizer(done); }); //--------------- // MAIN TASKS //--------------- // build for production gulp.task('build', [], function (cb) { runSequence('clean', 'production', 'compile', 'clean-build', cb); }); gulp.task('production', function () { isProduction = true; }); // default (no minify, sourcemaps and watch) gulp.task('default', function (callback) { runSequence('clean', 'compile', 'watch', 'done', callback); }).task('done', done); // serve development by default gulp.task('serve', function (cb) { runSequence('default', 'webserver', cb); }); // optional serve production gulp.task('serve-build', function (cb) { runSequence('build', 'webserver', cb); }); // run tasks without watch gulp.task('compile', function (cb) { runSequence( 'bootstrap', 'styles', 'templatecache', 'markup', cb); }); ///////////////// /** * Error handler */ function handleError(err) { console.log(err.toString()); this.emit('end'); } /** * Build html templates * @param {string} src source files folder * @param {string} dst target folder * @param {boolean} useTplcache Should generate angular template cache * @return {stream} */ function buildMarkup(src, dst, useTplcache, useMin) { return function () { log('Compiling HTML...'); if (useTplcache) log('Creating AngularJS templateCache..'); return gulp.src(src) .pipe(isProduction ? gutil.noop() : $.changed(dst, {extension: '.html'})) .pipe($.if(!usehtml, $.jade({ locals: { scripts: glob.sync(config.source + 'js/**/*.js') } }) ) ) .on("error", handleError) .pipe($.htmlPrettify(config.prettify)) // .pipe($.angularHtmlify()) .pipe(isProduction && useMin ? $.usemin(config.usemin) : gutil.noop() ) .pipe(useTplcache ? $.angularTemplatecache(config.tplcache.file, config.tplcache.opts) : gutil.noop() ) .pipe(gulp.dest(dst)) ; } } /** * Delete all files in a given path * @param {Array} path - array of paths to delete * @param {Function} done - callback when complete */ function clean(path, done) { log('Cleaning: ' + $.util.colors.blue(path)); del(path, done); } /** * Start Plato inspector and visualizer */ function startPlatoVisualizer(done) { log('Running Plato'); var files = glob.sync(config.plato.js); var excludeFiles = /.*\.spec\.js/; var plato = require('plato'); var options = { title: 'Plato Inspections Report', exclude: excludeFiles }; var outputDir = config.report + 'plato/'; plato.inspect(files, outputDir, options, platoCompleted); function platoCompleted(report) { var overview = plato.getOverviewReport(report); log(overview.summary); if (done) { done(); } } } /** * Just to be polite :) */ function done() { setTimeout(function () { // it's more clear to show msg after all log('Done.. Watching code and reloading on changes..'); }, 500); }; /** * Standard log */ function log(msg) { var prefix = '*** '; gutil.log(prefix + msg); } 并且没有其他错误的另一种情况可能是$this->Model->validationErrors不像Cake所期望的那样,只是忽略了您的数据,而不是保存,没有验证错误。例如,如果您的数据由DataTables提供,您可能会看到此格式$this->request->data

$this->request->data[0]['Model']['some_field']会起作用。

答案 7 :(得分:-1)

CakePHP 3.6

$entity = $this->Model->newEntity([
    'account_id' => $id,
    'gallery_id' => $gallery_id
]);

$result = $this->Model->save($entity);

print_r($entity->getErrors());