Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
292a6516
Commit
292a6516
authored
Jan 19, 2017
by
Marco Pivetta
Committed by
GitHub
Jan 19, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2609 from deeky666/DBAL-2511
[DBAL-2511] Fix type list extraction in connection update method
parents
6d7b45f3
ab794d1b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
146 additions
and
19 deletions
+146
-19
Connection.php
lib/Doctrine/DBAL/Connection.php
+41
-19
ConnectionTest.php
tests/Doctrine/Tests/DBAL/ConnectionTest.php
+105
-0
No files found.
lib/Doctrine/DBAL/Connection.php
View file @
292a6516
...
...
@@ -579,16 +579,20 @@ class Connection implements DriverConnection
throw
InvalidArgumentException
::
fromEmptyCriteria
();
}
$columnList
=
array
();
$criteria
=
array
();
$paramValues
=
array
();
foreach
(
array_keys
(
$identifier
)
as
$columnName
)
{
foreach
(
$identifier
as
$columnName
=>
$value
)
{
$columnList
[]
=
$columnName
;
$criteria
[]
=
$columnName
.
' = ?'
;
$paramValues
[]
=
$value
;
}
return
$this
->
executeUpdate
(
'DELETE FROM '
.
$tableExpression
.
' WHERE '
.
implode
(
' AND '
,
$criteria
),
array_values
(
$identifier
)
,
is_string
(
key
(
$types
))
?
$this
->
extractTypeValues
(
$
identifier
,
$types
)
:
$types
$paramValues
,
is_string
(
key
(
$types
))
?
$this
->
extractTypeValues
(
$
columnList
,
$types
)
:
$types
);
}
...
...
@@ -646,23 +650,31 @@ class Connection implements DriverConnection
*/
public
function
update
(
$tableExpression
,
array
$data
,
array
$identifier
,
array
$types
=
array
())
{
$columnList
=
array
();
$set
=
array
();
$criteria
=
array
();
$paramValues
=
array
();
foreach
(
$data
as
$columnName
=>
$value
)
{
$columnList
[]
=
$columnName
;
$set
[]
=
$columnName
.
' = ?'
;
$paramValues
[]
=
$value
;
}
if
(
is_string
(
key
(
$types
)))
{
$types
=
$this
->
extractTypeValues
(
array_merge
(
$data
,
$identifier
),
$types
);
foreach
(
$identifier
as
$columnName
=>
$value
)
{
$columnList
[]
=
$columnName
;
$criteria
[]
=
$columnName
.
' = ?'
;
$paramValues
[]
=
$value
;
}
$params
=
array_merge
(
array_values
(
$data
),
array_values
(
$identifier
));
if
(
is_string
(
key
(
$types
)))
{
$types
=
$this
->
extractTypeValues
(
$columnList
,
$types
);
}
$sql
=
'UPDATE '
.
$tableExpression
.
' SET '
.
implode
(
', '
,
$set
)
.
' WHERE '
.
implode
(
' = ? AND '
,
array_keys
(
$identifier
))
.
' = ?'
;
.
' WHERE '
.
implode
(
' AND '
,
$criteria
);
return
$this
->
executeUpdate
(
$sql
,
$params
,
$types
);
return
$this
->
executeUpdate
(
$sql
,
$param
Value
s
,
$types
);
}
/**
...
...
@@ -682,29 +694,39 @@ class Connection implements DriverConnection
return
$this
->
executeUpdate
(
'INSERT INTO '
.
$tableExpression
.
' ()'
.
' VALUES ()'
);
}
$columnList
=
array
();
$paramPlaceholders
=
array
();
$paramValues
=
array
();
foreach
(
$data
as
$columnName
=>
$value
)
{
$columnList
[]
=
$columnName
;
$paramPlaceholders
[]
=
'?'
;
$paramValues
[]
=
$value
;
}
return
$this
->
executeUpdate
(
'INSERT INTO '
.
$tableExpression
.
' ('
.
implode
(
', '
,
array_keys
(
$data
)
)
.
')'
.
' VALUES ('
.
implode
(
', '
,
array_fill
(
0
,
count
(
$data
),
'?'
)
)
.
')'
,
array_values
(
$data
)
,
is_string
(
key
(
$types
))
?
$this
->
extractTypeValues
(
$
data
,
$types
)
:
$types
'INSERT INTO '
.
$tableExpression
.
' ('
.
implode
(
', '
,
$columnList
)
.
')'
.
' VALUES ('
.
implode
(
', '
,
$paramPlaceholders
)
.
')'
,
$paramValues
,
is_string
(
key
(
$types
))
?
$this
->
extractTypeValues
(
$
columnList
,
$types
)
:
$types
);
}
/**
* Extract ordered type list from
two associate key lists of data and types
.
* Extract ordered type list from
an ordered column list and type map
.
*
* @param array $
data
* @param array $
columnList
* @param array $types
*
* @return array
*/
private
function
extractTypeValues
(
array
$
data
,
array
$types
)
private
function
extractTypeValues
(
array
$
columnList
,
array
$types
)
{
$typeValues
=
array
();
foreach
(
$
data
as
$k
=>
$_
)
{
$typeValues
[]
=
isset
(
$types
[
$
k
])
?
$types
[
$
k
]
foreach
(
$
columnList
as
$columnIndex
=>
$columnName
)
{
$typeValues
[]
=
isset
(
$types
[
$
columnName
])
?
$types
[
$
columnName
]
:
\PDO
::
PARAM_STR
;
}
...
...
tests/Doctrine/Tests/DBAL/ConnectionTest.php
View file @
292a6516
...
...
@@ -298,6 +298,111 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase
$conn
->
insert
(
'footable'
,
array
());
}
/**
* @group DBAL-2511
*/
public
function
testUpdateWithDifferentColumnsInDataAndIdentifiers
()
{
$driverMock
=
$this
->
createMock
(
'Doctrine\DBAL\Driver'
);
$driverMock
->
expects
(
$this
->
any
())
->
method
(
'connect'
)
->
will
(
$this
->
returnValue
(
new
DriverConnectionMock
()));
$conn
=
$this
->
getMockBuilder
(
'Doctrine\DBAL\Connection'
)
->
setMethods
(
array
(
'executeUpdate'
))
->
setConstructorArgs
(
array
(
array
(
'platform'
=>
new
Mocks\MockPlatform
()),
$driverMock
))
->
getMock
();
$conn
->
expects
(
$this
->
once
())
->
method
(
'executeUpdate'
)
->
with
(
'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND name = ?'
,
[
'some text'
,
true
,
1
,
'foo'
,
],
[
'string'
,
'boolean'
,
'integer'
,
'string'
,
]
);
$conn
->
update
(
'TestTable'
,
[
'text'
=>
'some text'
,
'is_edited'
=>
true
,
],
[
'id'
=>
1
,
'name'
=>
'foo'
,
],
[
'text'
=>
'string'
,
'is_edited'
=>
'boolean'
,
'id'
=>
'integer'
,
'name'
=>
'string'
,
]
);
}
/**
* @group DBAL-2511
*/
public
function
testUpdateWithSameColumnInDataAndIdentifiers
()
{
$driverMock
=
$this
->
createMock
(
'Doctrine\DBAL\Driver'
);
$driverMock
->
expects
(
$this
->
any
())
->
method
(
'connect'
)
->
will
(
$this
->
returnValue
(
new
DriverConnectionMock
()));
$conn
=
$this
->
getMockBuilder
(
'Doctrine\DBAL\Connection'
)
->
setMethods
(
array
(
'executeUpdate'
))
->
setConstructorArgs
(
array
(
array
(
'platform'
=>
new
Mocks\MockPlatform
()),
$driverMock
))
->
getMock
();
$conn
->
expects
(
$this
->
once
())
->
method
(
'executeUpdate'
)
->
with
(
'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND is_edited = ?'
,
[
'some text'
,
true
,
1
,
false
,
],
[
'string'
,
'boolean'
,
'integer'
,
'boolean'
,
]
);
$conn
->
update
(
'TestTable'
,
[
'text'
=>
'some text'
,
'is_edited'
=>
true
,
],
[
'id'
=>
1
,
'is_edited'
=>
false
,
],
[
'text'
=>
'string'
,
'is_edited'
=>
'boolean'
,
'id'
=>
'integer'
,
]
);
}
public
function
testFetchAssoc
()
{
$statement
=
'SELECT * FROM foo WHERE bar = ?'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment