var_dump()cakephp中的save()函数没有显示所有字段

时间:2014-01-16 09:27:21

标签: 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消息!

1 个答案:

答案 0 :(得分:0)

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

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

使用Cakes Form Helper创建一个表单,并使用 all 创建模型的字段,并将其提交给相应的操作。这些行动可能看起来像下面......

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