1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Update instead of insert - DataWriter

Discussion in 'XenForo Development Discussions' started by Marc, May 27, 2011.

  1. Marc

    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
    }

    }
     
  2. ragtek

    ragtek Guest

    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!!!!!
     
  3. Marc

    Marc Well-Known Member

    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
     
  4. ragtek

    ragtek Guest

    time to start reading code;)

    check some of the existing datawriters
     
    Marc likes this.
  5. Marc

    Marc Well-Known Member

    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'));
    }
     
  6. Marc

    Marc Well-Known Member

    You are of course quite right..
     
  7. Marc

    Marc Well-Known Member

    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);
    }
     
  8. ragtek

    ragtek Guest

    And is it working?*g*
     
  9. Marc

    Marc Well-Known Member

    Is it balls LMAO ... Still lookin
     
  10. Marc

    Marc Well-Known Member

    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!
     
  11. rour

    rour Member

    if ...->getMarkerByMemberID($userid) returns null, then you will take error (XenForo_DataWriter->_haveErrorsPreventSave()) instend of insert the new data
     

Share This Page