数据保存为空白CakePHP中

问题描述:

我有一个foreach循环,节省了一系列的记录到使用CakePHP数据保存为空白CakePHP中

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

所有节省除了其他利益

UPDATE罚款:

我已经做了以下

如果我这样做,我可以完美地展现出来

$this->data['UserInterest']['other_interest'] = $other; 
echo $this->data['UserInterest']['other_interest']; 
        $UserInterest = new UserInterest; 

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

我的数据库布局看起来像这样

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`) 
) 

和我的模型是这样

<?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') 
      ) 
     ); 
    } 
} 
?> 

难道我做错了什么?

UPDATE:

我做了以下

$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结果,其中以下,并且它仍然没有保存

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" 
+2

不知道这是否能解决您的问题,但有更好的方法来初始化您的模型!在控制器类中,可以在类定义中使用$ uses数组,如下所示:$ uses = array('UserInterest');有些人不喜欢这样,因为它可能需要很多内存。在这种情况下,你可以这样做:$ this-> UserInterest = ClassRegistry :: init('UserInterest');尝试这些东西,而不是做$ UserInterest = new UserInterest;并看看它是否有帮助。 – user470714

+0

谢谢,值得一试,但它并没有诀窍,我更新了我的问题 – Roland

+1

最奇怪的是,只有那一个领域不会保存。只有其他的事情我能想到的是,由于像你使用的CHARACTER SET和COLLATE这些关键字,它在这个领域遇到了一些困难。你可以重新创建没有这个表,看看它是否工作? (或添加一个新的varchar字段没有任何关键字) – user470714

老的文章,但想补充这里这里....

总是有

$this->Modelname->create(); 

在循环中用循环保存多个记录。这通常是导致错误的原因。