I am trying to use the method ArrayCollection::contains to find if an object is already in my Collection, but when i am doing :
//My ArrayCollection
$lesRoles = $drt->getDrtApplication()->getRoles();
$leRole = $lesRoles->first();
echo "Property appNom : ".$leRole->getRleApplication()->getAppNom()."// Property appRole : ".$leRole->getRleId()." <br>";
$role = new \Casgaya\Role(2,$drt->getDrtApplication());
echo "Property appNom : ".$role->getRleApplication()->getAppNom()."// Property appRole : ".$role->getRleId()." <br>";
var_dump($lesRoles->contains($role));
The result is :
Property appNom : CORA// Property appRole : 2
Property appNom : CORA// Property appRole : 2
bool(false)
Since appNom and rleId are the only two properties that the entity Role own i was hopping it would return true.
EDIT NEW TEST CASE :
echo "Test object role : <br>";
var_dump($lesRoles==$role);
echo"<br>";
echo "Test integer property rleID from object role : <br>";
var_dump($role->getRleId() == $leRole->getRleId());
echo"<br>";
echo "Test Application object property RleApplication from object role : <br> ";
var_dump($role->getRleApplication() == $leRole->getRleApplication());
The result is :
Property appNom : CORA// Property appRole : 2
Property appNom : CORA// Property appRole : 2
Test object role :
bool(false)
Test integer property rleID from object role :
bool(true)
Test Application object property RleApplication from object role :
bool(true)
Notice that when i test the equality of the two properties, both of them are true. But when i test the equality of the both whole object, it's false.
So the question is no more about ArrayCollection::contains, but it's :
On what criteria two doctrine entities are compared in the case of equality ?
I have found the solution by myself, here's for the people who have the same issue :
I am using the method ArrayCollection::exists instead of contains, so i can specify on which criteria should an equality between object be established :
In my case :
$result = $lesRoles->exists(function($key,$element) use ($role)
{
return ($element->getRleApplication() == $role->getRleApplication() && $role->getRleId() == $element->getRleId());
});
Note that here $key and $element are the current object tested from the collection.