I have the following setup “Many Users can have Many Projects (Collaborators)“
/**
* @Entity @HasLifeCycleCallbacks
* @Table(name="projects")
*/
class Project implements \Zend_Acl_Resource_Interface {
/**
* @ManyToMany(targetEntity="User", mappedBy="projects")
* @OrderBy({"displayName" = "ASC", "username" = "ASC"})
*/
protected $collaborators;
..
}
/**
* @Entity
* @Table(name="users")
*/
class User implements \Zend_Acl_Role_Interface {
/**
* @ManyToMany(targetEntity="Project", inversedBy="collaborators")
*/
protected $projects;
...
}
I tried to remove a collaborator using the following
$user = Application_DAO_User::findById($this->_getParam('userid'));
$proj = Application_DAO_Project::getProjectById($this->_getParam('id'));
Application_DAO_Project::removeCollaborator($proj, $user); // <---
// Application_DAO_User
public static function findById($id) {
return self::getStaticEm()->find('Application\Models\User', $id);
}
// Application_DAO_Project
public static function getProjectById($id) {
return self::getStaticEm()->find('Application\Models\Project', $id);
}
public static function removeCollaborator(Project $proj, User $collaborator) { // <---
$proj->getCollaborators()->remove($collaborator);
$collaborator->getProjects()->remove($proj);
self::getStaticEm()->flush();
}
And there isn’t any errors but the database stays the same …
This may be well over due but was just experiencing the same problem myself… According to the doctrine 2 documents, the function
ArrayCollection->remove($i)is for removing by array index.What you are after is: