[Form] Fixed undefined index in writeProperty when saving value arrays#5257
[Form] Fixed undefined index in writeProperty when saving value arrays#5257gertvr wants to merge 1 commit intosymfony:masterfrom
Conversation
When removing an item from an array and the previous value contains more than one identical copy of that item, the second unset fails with undefined index.
|
ping @bschussek |
|
Can you please add a test case? |
|
@gertvr Can you add a unit test to prove the issue (and avoid any future regressions)? |
|
@gertvr ping |
|
I'm on holiday till the 22nd, I'll add it afterwards! |
|
@gertvr Will you have time in the coming days to add some unit tests? |
|
I made some tests and fellt on a problematic case which seems unsolvable. I'll try to explain, if I missed something, please tell me. Let's take following model entities: class Author
{
/** @var Article[] */
private $articles = array();
public function addArticle(Article $article)
{
$this->articles[] = $article;
return $this;
}
public function removeArticle(Article $article)
{
foreach ($this->articles as $index => $value) {
if ($article === $value) {
unset($this->articles[$index]);
}
}
return $this;
}
}
class Article
{
public $title;
}Now let build an author with some articles, then use $author = new Author();
$article1 = new Article();
$article2 = new Article();
$accessor = new PropertyAccessor();
// set initial condition of the use case
$author->addArticle($article1)->addArticle($article2)->addArticle($article1);
// and now, the problematic assignment
$accessor->setValue($author, 'articles', array($article1, $article2));The problem is that now, we have only
In our case, accessor:
Out |
|
What if you change public function addArticle(Article $article)
{
if (!$this->articles->contains($article)) {
$this->articles[] = $article;
}
return $this;
} |
|
@stloyd if I do that, this will only affect the initial |
|
@stloyd is right. Preventing duplicates in your collection should fix your problem. The property accessor currently expects collections to be free of duplicates. |
|
@bschussek that's why I told the original problem was unsolvable. |
|
I see. As far as I understand #5013 would solve your problem then? |
|
that is, by disabling adders and removers and using the setter instead. |
|
@bschussek What do we do with this PR? Should we merge this fix? |
When removing an item from an array and the previous value contains more than one identical copy of that item, the second unset fails with undefined index.