Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge in update() method will omit data #15

Closed
superthin opened this issue Nov 12, 2020 · 3 comments
Closed

Merge in update() method will omit data #15

superthin opened this issue Nov 12, 2020 · 3 comments

Comments

@superthin
Copy link

Line 179 - 183:

`       //merge data and where together
        $collection = array_merge($data, $where);

         //collect the values from collection
        $values = array_values($collection);`

Merge array $data and $where if they have same columns -> lost data.

The problem will occur when we want to update a column which appear in where (or in array $values). Eg.:

UPDATE contact SET phone = '012-345-6789', first_name='Patrick' WHERE phone = 012-345-9950';

How to solve the problem?

@ejayfl
Copy link

ejayfl commented Aug 25, 2021

Hi,

Did you resolve this issue?

@DizzySquirrel
Copy link
Contributor

Bit of an old issue, but updating the 'update' function should resolve this...

    public function update($table, $data, $where)
    {
        //collect the values from data and where
        $values = [];
        
        //setup fields
        $fieldDetails = null;
        foreach ($data as $key => $value) {
            $fieldDetails .= "$key = ?,";
            $values[] = $value;
        }
        $fieldDetails = rtrim($fieldDetails, ',');
        
        //setup where 
        $whereDetails = null;
        $i = 0;
        foreach ($where as $key => $value) {
            $whereDetails .= $i == 0 ? "$key = ?" : " AND $key = ?";
            $values[] = $value;
            $i++;
        }
        
        $stmt = $this->run("UPDATE $table SET $fieldDetails WHERE $whereDetails", $values);
        
        return $stmt->rowCount();
    }

@dcblogdev
Copy link
Owner

Do you want to open a pull request? I'm without my laptop for a few days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants