Skip to content
Permalink
Browse files

Skip scalar data in belongsToMany relations.

Scalar data shouldn't get converted into an entity, as it is likely to
be a mistake/naughty user. Add coverage for other scalar values in both
hasMany and belongsToMany associations.

Refs #6562
  • Loading branch information...
markstory committed May 16, 2015
1 parent 8e85e1c commit 95a0fb67769fc0c23263c3376075b2326fc5a61f
Showing with 57 additions and 0 deletions.
  1. +6 −0 src/ORM/Marshaller.php
  2. +51 −0 tests/TestCase/ORM/MarshallerTest.php
@@ -214,6 +214,9 @@ protected function _prepareDataAndOptions($data, $options)
*/
protected function _marshalAssociation($assoc, $value, $options)
{
if (!is_array($value)) {
return;
}
$targetTable = $assoc->target();
$marshaller = $targetTable->marshaller();
$types = [Association::ONE_TO_ONE, Association::MANY_TO_ONE];
@@ -286,6 +289,9 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
$primaryCount = count($primaryKey);
foreach ($data as $i => $row) {
if (!is_array($row)) {
continue;
}
if (array_intersect_key($primaryKey, $row) === $primaryKey) {
$keys = array_intersect_key($row, $primaryKey);
if (count($keys) === $primaryCount) {
@@ -669,6 +669,34 @@ public function testBelongsToManyWithMixedJoinDataOutOfOrder()
$this->assertEquals(1, $result->tags[2]->_joinData->active);
}
/**
* Test belongsToMany association with scalars
*
* @return void
*/
public function testBelongsToManyInvalidData()
{
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'tags' => [
'id' => 1
]
];
$article = $this->articles->newEntity($data, [
'associated' => ['Tags']
]);
$this->assertEmpty($article->tags, 'No entity should be created');
$data['tags'] = 1;
$article = $this->articles->newEntity($data, [
'associated' => ['Tags']
]);
$this->assertEmpty($article->tags, 'No entity should be created');
}
/**
* Test belongsToMany association with mixed data array
*
@@ -742,6 +770,29 @@ public function testHasManyWithIds()
$this->assertEquals($user->comments[1], $commentTable->get(2));
}
/**
* Test HasMany association with invalid data
*
* @return void
*/
public function testHasManyInvalidData()
{
$data = [
'title' => 'new title',
'body' => 'some content',
'comments' => [
'id' => 1
]
];
$article = $this->articles->newEntity($data, ['associated' => ['Comments']]);
$this->assertEmpty($article->comments);
$data['comments'] = 1;
$article = $this->articles->newEntity($data, ['associated' => ['Comments']]);
$this->assertEmpty($article->comments);
}
/**
* Test one() with deeper associations.
*

0 comments on commit 95a0fb6

Please sign in to comment.
You can’t perform that action at this time.