mysql - cakephp 中的 var_dump() save() 函数未显示所有字段

标签 mysql cakephp insert save

我正在尝试将一些数据保存到我的表 gal_photographers 中。虽然我的表结构是:

CREATE TABLE IF NOT EXISTS `gal_photographers` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `gal_provider_id` int(4) NOT NULL,
  `starting_month` varchar(2) NOT NULL DEFAULT '01',
  `starting_year` varchar(4) NOT NULL DEFAULT '2010',
  `starting_package_wedding` int(7) NOT NULL,
  `starting_package_prewedding` int(7) NOT NULL,
  `starting_package_studio` int(7) NOT NULL,
  `willing_to_travel` tinyint(1) NOT NULL DEFAULT '0',
  `candid_photography` tinyint(1) NOT NULL DEFAULT '0',
  `traditional_photography` tinyint(1) NOT NULL DEFAULT '0',
  `pre-wedding` tinyint(1) NOT NULL DEFAULT '0',
  `portraiture` tinyint(1) NOT NULL DEFAULT '0',
  `portfolios` tinyint(1) NOT NULL DEFAULT '0',
  `location_shoots` tinyint(1) NOT NULL DEFAULT '0',
  `videography` tinyint(1) NOT NULL DEFAULT '0',
  `awards1` varchar(100) NOT NULL,
  `awards2` varchar(100) NOT NULL,
  `awards3` varchar(100) NOT NULL,
  `video_title1` varchar(150) NOT NULL,
  `video_link1` varchar(150) NOT NULL,
  `video_title2` varchar(150) NOT NULL,
  `video_link2` varchar(150) NOT NULL,
  `video_title3` varchar(150) NOT NULL,
  `video_link3` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=455 ;

我正在尝试保存:

$sv = $this->GalPhotographer->save();
var_dump($sv);
if($sv) {
    echo 'SUCCESS';
}else{ echo 'NO SUCCESS !'; 
}

但是上面的save()并没有插入所有数据。此外, var_dump($sv) 显示:

array (size=1)
  'GalPhotographer' => 
    array (size=10)
      'starting_month' => string '01' (length=2)
      'starting_year' => string '2010' (length=4)
      'willing_to_travel' => string '0' (length=1)
      'candid_photography' => string '0' (length=1)
      'traditional_photography' => string '0' (length=1)
      'pre-wedding' => string '0' (length=1)
      'portraiture' => string '0' (length=1)
      'portfolios' => string '0' (length=1)
      'location_shoots' => string '0' (length=1)
      'videography' => string '0' (length=1)

当然,尽管我的所有数据都没有插入,但我收到了 SUCCESS 消息!

最佳答案

我有点不清楚你想从哪里获取数据。我猜它应该来自带有表单的 View ,表单提交正确吗?

save() 现在实际保存的是您在 sql 命令中定义的默认值。

使用 Cakes Form Helper 创建表单并使用模型的所有字段,并使其提交适当的操作。这些操作可能应该如下所示...

public function add($gal_provider_id = null) {
    if (!$gal_provider_id) {
        throw new NotFoundException(__('Invalid Gal Provider'));
    }
    $this->set('provider', $gal_provider_id);
    if ($this->request->is('post') || $this->request->is('put')) {
        $this->GalPhotographer->create();
        if ($this->GalPhotographer->save($this->request->data)) {
            $this->Session->setFlash(__('Success!'));
        } else {
            $this->Session->setFlash(__('No success!'));
        }
    }
}

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid GalPhotographer'));
    }
    $phtgrpher = $this->GalPhotographer->findById($id);
    if (!$phtgrpher) {
        throw new NotFoundException(__('Invalid GalPhotographer'));
    }

    if ($this->request->is('post') || $this->request->is('put')) {
        $this->GalPhotographer->create();
        if ($this->GalPhotographer->save($this->request->data)) {
            $this->Session->setFlash(__('Success!'));
        } else {
            $this->Session->setFlash(__('No success!'));
        }
    }

    if (!$this->request->data) {
        $this->request->data = $phtgrpher;
    }
}

关于mysql - cakephp 中的 var_dump() save() 函数未显示所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21157883/

相关文章:

php - 来自 MySQL Db 的 Em Dash 和 En Dash 与 PHP

php - CakePHP 3 - 使用 LocalStringFormat 解析日期以更正 SQL 格式和更正验证

php - 循环列表始终使用复选框插入列表中的第一个

sql - 我如何知道何时根据 C# 列表插入、删除或更新?

MySQL 相当于 Pandas 的 to_datetime 函数

javascript - 如何在 chart.js 中使用 JSON 数据?

cakephp - 更新 Cakephp3 应用程序核心库的推荐方法是什么

tsql - 如何在 T-SQL 中插入只有自动生成列的行?

php - 无法使用 array_merge 合并变量 : undefined index

javascript - Cakephp 中的可重复子表单