Сохранение данных как пустых в CakePHP - PullRequest
1 голос
/ 29 сентября 2011

У меня есть цикл foreach, который сохраняет кучу записей в таблицу, используя 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');
                }
            }

Все отлично сохраняет, кроме других процентов

ОБНОВЛЕНИЕ:

I* * * * * * * * * * * * * * * * * * * * * * *

* * * * * * * * * * * * * *1014* * * * * * * * * * * * * * * * * * * * 10141015 *

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

Я что-то не так делаю?

ОБНОВЛЕНИЕ:

Я сделал следующее

$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"

1 Ответ

1 голос
/ 24 марта 2012

И старый пост, но хотел бы добавить это сюда ....

Всегда есть

$this->Modelname->create();

в циклах, чтобы сохранить несколько записей с циклом. Чаще всего это тот случай, который вызывает ошибку.

...