propelorm/Propel — pull request #879 by ruben-podadera (2014-05-20 10:06:09+00:00)
Title: Versionable Behaviour : 2nd bug with multiple identical related objects

This PR is related with this one :
https://github.com/propelorm/Propel/pull/876

Still the same example, one contract, 2 companies. Here is the generated code in BaseCompany.php
```php
public function addVersion($con = null)
{
...
$version->setCompany($this);
if ($relateds = $this->getContractsRelatedByRenterCompanyId($con)->toKeyValue('Id', 'Version')) {
$version->setContractIds(array_keys($relateds));
$version->setContractVersions(array_values($relateds));
}
if ($relateds = $this->getContractsRelatedByTenantCompanyId($con)->toKeyValue('Id', 'Version')) {
$version->setContractIds(array_keys($relateds));
$version->setContractVersions(array_values($relateds));
}
$version->save($con);
...
```

If I have a company that is both refered as a tenant and a renter, the second if block will override contract ids set to version.

The correction I propose use add instead of set so the generated code looks like
```php
...
$version->setCompany($this);
if ($relateds = $this->getContractsRelatedByRenterCompanyId($con)->toKeyValue('Id', 'Version')) {
foreach(array_keys($relateds) as $id) {
$version->addContractId($id);
}
foreach(array_values($relateds) as $version_num) {
$version->addContractVersion($version_num);
}
}
if ($relateds = $this->getContractsRelatedByTenantCompanyId($con)->toKeyValue('Id', 'Version')) {
foreach(array_keys($relateds) as $id) {
$version->addContractId($id);
}
foreach(array_values($relateds) as $version_num) {
$version->addContractVersion($version_num);
}
}
$version->save($con);
...
```