Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Handling Duplicates and Indexes in Structures

dodgeriddodgerid
edited January 2012 in Vanilla 2.0 - 2.8

Hi,

Is it possible to add two or more column indexes to your database structure.php ? (To prevent duplicates)

Also if it is possible what would be the correct method in garden of handling the error returned for inserting a duplicates to prevent bonking ?

Can I have the Developer role please, so I can put these questions in the right place ?

Best Answer

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    Answer ✓

    patience is all it takes :)

    There was an error rendering this rich post.

Answers

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    Answer ✓

    patience is all it takes :)

    There was an error rendering this rich post.

  • Hehe, ok will extend Gdn_Model for my App and pass data through a duplicate check, I wonder if........ yep save allows duplicates.

    Will have to save form data differently also.

  • Ok, very simple, very sweet solution:

    public function IsDuplicate($table = '', $where) { $Duplicate = $this->SQL->Select('*')->From($table)->Where($where)->Get(); if ($Duplicate) { return True; } else { return False; } }

  • The Code Above Is Flawed.

    It Should Be:

    public function IsDuplicate($table = '', $where) { $Duplicate = $this->SQL->Select('*')->From($table)->Where($where)->Get(); if ($Duplicate->FirstRow() == false) { return False; } else { return True; } }

  • dodgeriddodgerid
    edited January 2012

    Ok version 3 of the above, this one seems to work flawlessly where as I kept getting errors with the above, although this version only allows you to compare one value against the values returned by a particular reference.

    I.e If your looking for a message subject by a particular user your $CompareIndex would be 'User_ID' say, your $CompareIndexValue would be the users user_id and the $CompareValue would be the subject your looking for and would then return False if its not found and True if it is.

    public function IsDuplicate($table = '', $CompareIndex, $CompareIndexValue, $CompareValue) {
            $Records = $this->SQL->Select('*')->From($table)->Where($CompareIndex . ' =', $CompareIndexValue)->Get();
            foreach ($Records as $Record)
            {
                foreach($Record as $Entry)
                {
                    if ($Entry == $CompareValue)
                        return True;
                }
            }
            return False;
        }

    Ok so still not perfect but closer.

    Not being funny but I'm thinking that the prevention of duplicate entries or the handling of, really should be in the base functionality here.

    Just an after thought, but part of the problem I have had is checking the value posted back from a form in the IsDuplicate check.

    You cannot just go $userid = $this->Form->UserID; as the values posted back for obvious reasons are protected.

    So instead, what you have to do is $userid = $this->Form->GetValue('UserID');

    Viva la Vanilla!

Sign In or Register to comment.