不保存cakephp中的所有数据

时间:2010-04-06 11:48:23

标签: php cakephp

我正在做一个简单的保存,而不是输入所有数据。我有两个模型,消息和电子邮件消息。消息表正在填充ok,但emailmessage表只获取外键message_id。下面是代码。有人注意到任何不正常的事情吗?

EmailMessageModel

class EmailMessage extends AppModel {

var $name = 'EmailMessage';

var $belongsTo = array('Message');

var $validate = array(
    'user_name' => array(
        'rule' => 'notEmpty',
        'message' => 'Please enter your name'
    ),
    'user_email' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter your name'
        ),
        'email' => array(
            'rule' => 'email',
            'message' => 'Please enter a valid email address'
        )
    )
);

function beforeSave()
{
    $this->data[$this->name]['verified'] = 1;
    return true;
}
}

MessageModel

class Message extends AppModel {
var $name = 'Message';

var $actsAs = array('Containable');

var $hasOne = array(
    'EmailMessage' => array(
        'className' => 'EmailMessage',
        'foreignKey' => 'message_id'
    ),
    'TwitterMessage' => array(
        'className' => 'TwitterMessage',
        'foreignKey' => 'message_id'
    ),
    'FacebookMessage' => array(
        'className' => 'FacebookMessage',
        'foreignKey' => 'message_id'
    ),
    'VideoMessage' => array(
        'className' => 'VideoMessage',
        'foreignKey' => 'message_id'
    )
);

var $belongsTo = array('Fact' => array('className' => 'Fact', 'foreignKey' => 'fact_id'));

function beforeSave() {
    if (isset($this->data[$this->name]['ip']) && empty($this->data[$this->name]['ip'])) {
        $this->data[$this->name]['ip'] = getRealIp();
    }
    return true;
}

function __findVerified($options=array()) {
    $query = array(
        'conditions' => array(),
        'contain' => array(
            'FacebookMessage' => array( 'conditions' => array('FacebookMessage.verified' => 1) ),
            'TwitterMessage' => array( 'conditions' => array('TwitterMessage.status' => 'verified') ),
            'EmailMessage',
            'VideoMessage',
            'Fact'
        ),
        'order' => 'Message.created DESC'
    );

    // Merge the query with any additional options passed in to the function
    $options = Set::merge($query, $options);

    // Do we want the query only? Then just return the query.
    if($this->isQuery($options)) {
        return $options;
    }

    // If not, return the records
    return parent::find('all', $options);
}
}

EmailMessageController

class EmailMessagesController extends AppController {

function register(){

    Configure::write('debug', 2);

    if (isset($this->data)){

        $this->EmailMessage->saveAll($this->data);

        debug($this->data);

    }

    $this->layout = "ajax";
    $this->render("add");

}

}

数据

    Array
(
[Message] => Array
    (
        [fact_id] => 3
    )

[EmailMessage] => Array
    (
        [user_name] => Joe Bloggs
        [user_email] => here@there.com
    )

)

由于

- 标记

编辑:

这是表格

EmailMessages - http://pastebin.com/zXTT5VHd

消息 - http://pastebin.com/Rd1MP0CJ

1 个答案:

答案 0 :(得分:1)

有问题的表email_messages的名称是?我注意到在您的模型中user_name和user_email不能为空。但是您的表允许这些字段的 NULL 值。