Last active
April 22, 2018 14:01
-
-
Save mrself/6e110701ad2182f53c3443dc4bd2cfbf to your computer and use it in GitHub Desktop.
Set doctrine association
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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