Skip to content

Instantly share code, notes, and snippets.

@mrself
Last active April 22, 2018 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrself/6e110701ad2182f53c3443dc4bd2cfbf to your computer and use it in GitHub Desktop.
Save mrself/6e110701ad2182f53c3443dc4bd2cfbf to your computer and use it in GitHub Desktop.
Set doctrine association
<?php
abstract class BaseEntity {
public function setAssoc($entityName) {
$inflector = ICanBoogie\Inflector::get();
$localProp = $inflector->pluralize($entityName);
foreach ($this->$localProp as $localAssocProp) {
$inParam = null;
foreach ($newAssocProps as $index => $item) {
if ($item->getId() &&
$localAssocProp->getId() === $item->getId()) {
unset($newAssocProps[$index]);
$inParam = $item;
}
}
if (!$inParam) {
$this->$localProp->remove($localAssocProp);
}
}
$addMethodName = 'add' . ucfirst($entityName);
foreach ($newAssocProps as $localAssocProp) {
$this->$addMethodName($localAssocProp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment