【问题标题】:var_dump() the save() function in cakephp not showing all the fieldscakephp 中的 var_dump() save() 函数未显示所有字段
【发布时间】:2014-01-16 09:27:21
【问题描述】:

我正在尝试将一些数据保存到我的表 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 消息,尽管我的所有数据都没有插入!

【问题讨论】:

  • 这些值是什么?
  • 模型长什么样?你有字段验证吗?

标签: mysql cakephp insert save


【解决方案1】:

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

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

使用Cakes Form Helperall 模型的字段创建一个表单,并使其提交给适当的操作。这些操作应该看起来像下面这样......

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;
    }
}

【讨论】:

    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多