首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CakePHP中将数据另存为空白

在CakePHP中将数据另存为空白
EN

Stack Overflow用户
提问于 2011-09-29 05:48:30
回答 1查看 1.5K关注 0票数 1

我有一个foreach循环,它使用cakePHP将一堆记录保存到一个表中

代码语言:javascript
复制
foreach($interests as $id){
                $this->data['UserInterest']['user_id'] = $user_id;
                $this->data['UserInterest']['interest_id'] = $id;
                $this->data['UserInterest']['other_interest'] = $other;
                $UserInterest = new UserInterest;
                if(!$UserInterest->save($this->data)) {
                    $this->Session->setFlash(__l('Failed to save Interests.') , 'default', null, 'error');
                }
            }

除了其他兴趣之外,所有保存都很好

更新:

我做了以下工作

如果我这样做,我就能把它完美地表达出来。

代码语言:javascript
复制
$this->data['UserInterest']['other_interest'] = $other;
echo $this->data['UserInterest']['other_interest'];
                    $UserInterest = new UserInterest;

但不知何故它保存为空白?

我的数据库布局如下所示

代码语言:javascript
复制
CREATE TABLE IF NOT EXISTS `users_interests` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) NOT NULL,
  `interest_id` bigint(20) NOT NULL,
  `other_interest` varchar(150) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
)

我的模型是这样的

代码语言:javascript
复制
<?php
class UserInterest extends AppModel
{
    var $name = 'UserInterest';

    var $useTable="users_interests";

    //$validate set in __construct for multi-language support
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
        ) ,
        'Interests' => array(
            'className' => 'Interest',
            'foreignKey' => 'interest_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
        )
    );
    function __construct($id = false, $table = null, $ds = null) 
    {
        parent::__construct($id, $table, $ds);
        $this->validate = array(
            'user_id' => array(
                'rule' => 'numeric',
                'allowEmpty' => false,
                'message' => __l('Required')
            )
        );
    }
}
?>

我做错了什么吗?

更新:

我做了以下工作

代码语言:javascript
复制
$this->data['UserInterest']['user_id'] = $user_id;
                $this->data['UserInterest']['interest_id'] = $id;
                $this->data['UserInterest']['other_interest'] = $other;
                 echo '<pre>';
                var_dump($this->data['UserInterest']);
                var_dump($other);
                var_dump($this->data['UserInterest']['other_interest']);

                $this->UserInterest = ClassRegistry::init('UserInterest');

var_dump的结果如下所示,但仍然没有保存

代码语言:javascript
复制
array(3) {
  ["user_id"]=>
  string(3) "659"
  ["interest_id"]=>
  string(2) "10"
  ["other_interest"]=>
  string(17) "Share Investments"
}
string(17) "Share Investments"
string(17) "Share Investments"
EN

回答 1

Stack Overflow用户

发布于 2012-03-24 16:48:50

和老帖子,但想在这里补充这一点...

一直都有

代码语言:javascript
复制
$this->Modelname->create();

在循环中使用循环保存多条记录。最常见的情况就是导致错误的情况。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7590041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档