0
我做了一個簡單的保存,並沒有輸入所有的數據。我有兩個模型,消息和電子郵件消息。消息表正在填充正常,但emailmessage表只獲取外鍵message_id。以下是代碼。任何人都注意到任何不確定的事情不保存所有數據在cakephp
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] => [email protected]
)
)
感謝
--mark
編輯:
這裏是表
EmailMessages - http://pastebin.com/zXTT5VHd
消息 - http://pastebin.com/Rd1MP0CJ
爲什麼不在這裏發佈代碼? – 2010-04-06 11:53:15
對不起,我寧願使用pastebin大塊。 – madphp 2010-04-06 11:55:42
也,tbh,代碼粘貼在這裏並不是它可能的。我會更好地粘貼在這裏嗎? – madphp 2010-04-06 12:00:44