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

insert update on duplicate key support. #86

Closed
shaq147 opened this issue Jun 5, 2017 · 5 comments
Closed

insert update on duplicate key support. #86

shaq147 opened this issue Jun 5, 2017 · 5 comments
Assignees
Milestone

Comments

@shaq147
Copy link

shaq147 commented Jun 5, 2017

I've implemented a quick support for inserting on duplicate key update for those interested.

Use case:

$db->insert( array('id', 'field1', 'field2', 'field3') )
    ->into('table')
    ->values('1', 'value1', 'value2', 'value3')
    ->updateOnDuplicate( array('field2' => 'value2', 'field3' => 'value3') );

PDO/Statement/InsertStatement.php added 2 functions, 3 private variables, and updated the __toString method

private $_updateOnDuplicate = false;
private $_updateValues = array();
private $_updateFields = array();

public function updateOnDuplicate($fieldsValues)
{
	$fieldValues = (array) $fieldValues;

	if(!empty($fieldValues) ) {
		$this->_updateOnDuplicate = true;
	        $this->_updateValues = array_values($fieldValues);
	        $this->_updateFields = array_keys($fieldValues);
        }	
        return $this;
}

protected function _prepareUpdateFields()
{
	$this->setValues($this->_updateValues);

	$str = '';
	$commaFlag = false;
	foreach($this->_updateFields as $field) {
		if($commaFlag) $str .= ', ';
		$str .= $field . ' = ?';
		$commaFlag = true;
	}
	
	return $str;
}

//Updated
public function __toString()
{
	if (empty($this->table)) {
		trigger_error('No table is set for insertion', E_USER_ERROR);
	}

	if (empty($this->columns)) {
		trigger_error('Missing columns for insertion', E_USER_ERROR);
	}

	if (empty($this->values)) {
		trigger_error('Missing values for insertion', E_USER_ERROR);
	}

	$sql = 'INSERT INTO '.$this->table;
	$sql .= ' '.$this->getColumns();
	$sql .= ' VALUES '.$this->getPlaceholders();
	
	if($this->_updateOnDuplicate) {
		$sql .= ' ON DUPLICATE KEY UPDATE '.$this->_prepareUpdateFields();
	}

	return $sql;
}


@shaq147 shaq147 mentioned this issue Jun 5, 2017
@stebogit
Copy link

Hello,
great job @shaq147!

How come this hasn't been added to the main project?
I'd say it's a convenient functionality, no?

@techjwalker
Copy link

techjwalker commented Dec 8, 2017

@shaq147 Did you submit a PR for this? If the functionality works, I would love to see this implemented.

EDIT: Made a PR for it myself, with documentation updates to reflect the changes.

@kwhat
Copy link
Collaborator

kwhat commented Sep 20, 2019

@shaq147 do you want to make pull request with the code above? I think we should add this to 2.1 or maybe even 2.0.

@kwhat kwhat self-assigned this Sep 20, 2019
@kwhat kwhat added this to the v2.0.0 milestone Sep 20, 2019
@kwhat
Copy link
Collaborator

kwhat commented Nov 5, 2019

This feature request will go hand 'n hand with the #104 request as the features are somewhat related.

@kwhat kwhat modified the milestones: v2.0.0, v2.1.0 Dec 5, 2019
@kwhat kwhat added vcs and removed vcs labels Dec 18, 2019
@kwhat
Copy link
Collaborator

kwhat commented Dec 18, 2019

I will be working on this tomorrow. I think we can just add a $pairs array property and a duplicate(?array $paris = null) method on the Statement\Insert class.

@kwhat kwhat mentioned this issue Dec 21, 2019
6 tasks
kwhat added a commit that referenced this issue Dec 21, 2019
@kwhat kwhat added the vcs label Dec 21, 2019
kwhat added a commit that referenced this issue Dec 21, 2019
kwhat added a commit that referenced this issue Dec 21, 2019
kwhat added a commit to kwhat/PDO that referenced this issue Jan 22, 2020
kwhat added a commit to kwhat/PDO that referenced this issue Jan 22, 2020
@kwhat kwhat modified the milestones: v2.1.0, v2.2.0 Nov 2, 2021
@kwhat kwhat closed this as completed Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants