Update instead of insert - DataWriter

Marc

Well-known member
OK, thanks to Ragtek/Kier/James I have now got to the point where I have got the mapping working with no problems, and after saving the personal details my datawriter updates my table with coordinates ONLY if the location field as changed, and a valid Geocoded location has been entered.

So now my question. My datawriter only currently works when a new item is added to the database, and if the user already exists in my table throws an error due to a duplicated primary key. This is expected as I only want people to enter onto the map once. Now Im guessing that the datawriter can check if it exists and update the data instead, however Im not sure how to go about this.

Any pointers?

PHP:
<?php
  
class dcXenMapMe_DataWriter_NewMarker extends XenForo_DataWriter

{

/**

* Gets the fields that are defined for the table. See parent for explanation.

*

* @return array

*/

protected function _getFields()

{

return array(

'dc_MapMe_Markers' => array(

'member_id'    => array('type' => self::TYPE_UINT,   'required' => true),

'lon'  => array('type' => self::TYPE_FLOAT,   'required' => true),

'lat'    => array('type' => self::TYPE_FLOAT, 'required' => true)

)

);

}
protected function _getExistingData($data)
{
//NOT SURE IF I NEED ANYTHING HERE
}

protected function _getUpdateCondition($tableName)
{
//GUESSING I NEED SOMETHING HERE, SO IT UPDATES RATHER THAN INSERTS IF MEMBER_ID PRESENT
}

}
 
PHP:
protected function _getExistingData($data)
{
//NOT SURE IF I NEED ANYTHING HERE
}

protected function _getUpdateCondition($tableName)
{
//GUESSING I NEED SOMETHING HERE, SO IT UPDATES RATHER THAN INSERTS IF MEMBER_ID PRESENT
}
there's a reason why you had to implement this 2 methods in your dw!

....yes you need them!!!!!
 
PHP:
protected function _getExistingData($data)
{
//NOT SURE IF I NEED ANYTHING HERE
}

protected function _getUpdateCondition($tableName)
{
//GUESSING I NEED SOMETHING HERE, SO IT UPDATES RATHER THAN INSERTS IF MEMBER_ID PRESENT
}
there's a reason why you had to implement this 2 methods in your dw!

....yes you need them!!!!!

Yeah thats just me commentin late at night, sorry wasnt part of my question ....... However can see how it was misleading given what I asked above LOL Just not sure how it works tbh
 
NEW _getUpdateCondition ...... Just not sure on the existing data part now

PHP:
protected function _getUpdateCondition($tableName)
{
return 'member_id = ' . $this->_db->quote($this->getExisting('member_id'));
}
 
time to start reading code;)

check some of the existing datawriters

OK, point definately taken ..

MY Writer

PHP:
<?php
  
class dcXenMapMe_DataWriter_NewMarker extends XenForo_DataWriter

{

/**

* Gets the fields that are defined for the table. See parent for explanation.

*

* @return array

*/

protected function _getFields()

{

return array(

'dc_MapMe_Markers' => array(

//'marker_id'    => array('type' => self::TYPE_UINT,    'autoIncrement' => true),

'member_id'    => array('type' => self::TYPE_UINT,   'required' => true),

'lon'  => array('type' => self::TYPE_FLOAT,   'required' => true),

'lat'    => array('type' => self::TYPE_FLOAT, 'required' => true)

)

);

}
protected function _getExistingData($data)
{
if (!$id = $this->_getExistingPrimaryKey($data, 'member_id'))
{
return false;
}

return array('dc_MapMe_Markers' => $this->getModelFromCache('dcXenMapMe_Model_MapMe')->getMarkerByMemberID($Id));
  
}

protected function _getUpdateCondition($tableName)
{
return 'member_id = ' . $this->_db->quote($this->getExisting('member_id'));
}

}

And added to my MapMe Model

PHP:
public function getMarkerByMemberID($memberId)
{
return $this->_getDb()->fetchRow('
SELECT
member_id,
lon,
lat
FROM dc_mapme_markers
WHERE member_id = ?
', $memberId);
}
 
And is it working?*g*

THANKYOU FOR GETTING ME TO READ THE DAMNED THING!!!!!!!!!!!!!!!!!!!!!!!!!!!

now changed to

PHP:
protected function _getExistingData($data)
{
if (!$userid = $this->_getExistingPrimaryKey($data, 'member_id'))
{
return false;
}

return array('dc_MapMe_Markers' => $this->getModelFromCache('dcXenMapMe_Model_MapMe')->getMarkerByMemberID($userid));
  
}

Changed the $id to $userid as that is what I am setting the variable to when setting the existing data

PHP:
$writer->setExistingData($userId);

Working like a bloody charm ........ And tbh I didnt realise just how many comments there are in the framework. Extremely usefull!
 
if ...->getMarkerByMemberID($userid) returns null, then you will take error (XenForo_DataWriter->_haveErrorsPreventSave()) instend of insert the new data
 
Top Bottom