/** * Generates the code to support many to one associations * @param OutletAssociationConfig $config configuration * @return string many to one function code */ function createManyToOneFunctions(OutletAssociationConfig $config) { $local = $config->getLocal(); $foreign = $config->getForeign(); $key = $config->getKey(); $refKey = $config->getRefKey(); $getter = $config->getGetter(); $setter = $config->getSetter(); if ($config->getLocalUseGettersAndSetters()) { $keyGetter = 'get' . $key . '()'; } else { $keyGetter = $key; } if ($config->getForeignUseGettersAndSetters()) { $refKey = 'get' . $refKey . '()'; } $c = ''; $c .= " function {$getter}() { \n"; $c .= "\tif (is_null(\$this->{$keyGetter})) return parent::{$getter}(); \n"; $c .= "\tif (is_null(parent::{$getter}())) { \n"; //&& isset(\$this->$keyGetter)) { \n"; $c .= "\t parent::{$setter}( self::\$_outlet->load('{$foreign}', \$this->{$keyGetter}) ); \n"; $c .= "\t} \n"; $c .= "\treturn parent::{$getter}(); \n"; $c .= " } \n"; $c .= " function {$setter}({$foreign} \$ref" . ($config->isOptional() ? '=null' : '') . ") { \n"; $c .= "\tif (is_null(\$ref)) { \n"; if ($config->isOptional()) { $c .= "\t \$this->{$keyGetter} = null; \n"; } else { $c .= "\t throw new OutletException(\"You can not set this to NULL since this relationship has not been marked as optional\"); \n"; } $c .= "\t return parent::{$setter}(null); \n"; $c .= "\t} \n"; //$c .= " \$mapped = new OutletMapper(\$ref); \n"; //$c .= " \$this->$key = \$mapped->getPK(); \n"; if ($config->getLocalUseGettersAndSetters()) { $c .= "\t\$this->set{$key}(\$ref->{$refKey}); \n"; } else { $c .= "\t\$this->{$key} = \$ref->{$refKey}; \n"; } $c .= "\treturn parent::{$setter}(\$ref); \n"; $c .= " } \n"; return $c; }
/** * Constructs a new instance of OutletManyToManyConfig * @param OutletConfig $config configuration * @param object $local local entity * @param object $foreign foreign entity * @param array $options options * @return OutletManyToManyConfig instance */ public function __construct(OutletConfig $config, $local, $foreign, array $options) { if (!isset($options['table'])) { throw new OutletConfigException("Entity {$local}, association with {$foreign}: You must specify a table when defining a many-to-many relationship"); } $this->table = $options['table']; $this->tableKeyLocal = $options['tableKeyLocal']; $this->tableKeyForeign = $options['tableKeyForeign']; parent::__construct($config, $local, $foreign, $options); }