Hi,
Im doing a simple save, and its not entering all the data. I have two models, message and emailmessage. The message table is getting populated ok, but the emailmessage table, is only getting the foreign key message_id. Below is the code. Anyone notice anything unsual?
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");
}
}
Data
Array
(
[Message] => Array
(
[fact_id] => 3
)
[EmailMessage] => Array
(
[user_name] => Joe Bloggs
[user_email] => [email protected]
)
)
Thanks
--Mark
EDIT:
Here is the tables
EmailMessages - http://pastebin.com/zXTT5VHd
Messages - http://pastebin.com/Rd1MP0CJ