-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fixed undefined index in writeProperty when saving value arrays #5257
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
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.