/** * @covers Foote\Ginny\Map\BaseMap::update */ public function testupdate() { $field = new BaseField('name'); $field->type = 'integer'; $field->size = 1; $model = new BaseModel('Thing'); $model->addField($field); $bundle = new BaseBundle('Bundle'); $bundle->addModel($model); $map = new BaseMap('Admin'); $map->addBundle($bundle); $map->update(); //ensure $field->update() was invoked by $bundle->update() $this->assertEquals('boolean', $field->type); }
public function map() { foreach ($this->schema->bundles as $_bundle) { $bundle = new BaseBundle($_bundle->name); $this->map->addBundle($bundle); # add models to bundle foreach ($_bundle->models as $_model) { if (strpos($_model->name, ':') != false) { continue; } $model = new BaseModel($_model->name); $bundle->addModel($model); $model->description = $_model->description; $model->table = $model->bundle->snake . '_' . $model->snakes; if (!empty($_model->ormAttributes)) { foreach ($_model->ormAttributes as $key => $val) { if ('table' == $key) { $model->table = $val; } } } # add fields to model foreach ($_model->fields as $_field) { $field = new BaseField($_field->name); $model->addField($field); $field->type = $_field->type; $field->size = $_field->size; $field->default = $_field->default; $field->required = $_field->required; $field->unique = $_field->unique; $field->primary = $_field->primary; $field->autoIncrement = $_field->autoIncrement; } } # add relationships to models foreach ($_bundle->models as $_model) { if (strpos($_model->name, ':') != false) { continue; } $model = $bundle->models->get($_model->name); foreach ($_bundle->associations as $_association) { # belongsTo (ex. User belongsTo Site) if ($model->name == $_association->from) { $parent = $bundle->models->get($_association->to); new BaseAssociation($parent->name, ['owner' => $model, 'ownerKey' => $_association->field->from, 'type' => 'belongsTo', 'target' => $parent, 'targetKey' => $_association->field->to]); } # hasOne and hasMany (ex. User hasOne Profile, User hasMany Comment) if ($model->name == $_association->to) { $child = $bundle->models->get($_association->from); new BaseAssociation($child->name, ['owner' => $model, 'ownerKey' => $_association->field->to, 'type' => 'hasMany', 'target' => $child, 'targetKey' => $_association->field->from]); /** * for User hasOne Profile, if Profile.user is set to unique, the association * will change to "hasOne" on $association->update() */ } } # manyToMany foreach ($_bundle->manyToManys as $_manyToMany) { if ($model->name == $_manyToMany->name) { /** * example: User <--> UserRole <--> Role */ // identify UserRole as a manyToMany table $model->manyToMany = true; // ex: Role $manyModel1 = $bundle->models->get($_manyToMany->owner->name); //Role // ex: User $manyModel2 = $bundle->models->get($_manyToMany->inverse->name); //User new BaseAssociation($manyModel2->name, ['owner' => $model, 'ownerKey' => $_manyToMany->inverse->field->from, 'type' => 'belongsTo', 'target' => $manyModel2, 'targetKey' => $_manyToMany->owner->field->to]); new BaseAssociation($manyModel1->name, ['owner' => $model, 'ownerKey' => $_manyToMany->owner->field->from, 'type' => 'belongsTo', 'target' => $manyModel1, 'targetKey' => $_manyToMany->inverse->field->to]); new BaseAssociation($manyModel1->name, ['owner' => $manyModel2, 'ownerKey' => $_manyToMany->owner->field->to, 'type' => 'belongsToMany', 'target' => $manyModel1, 'targetKey' => $_manyToMany->inverse->field->to, 'pivot' => $model]); new BaseAssociation($manyModel2->name, ['owner' => $manyModel1, 'ownerKey' => $_manyToMany->inverse->field->to, 'type' => 'belongsToMany', 'target' => $manyModel2, 'targetKey' => $_manyToMany->owner->field->to, 'pivot' => $model]); } } } } $this->map->update(); $this->map->validate(); //s($this->map->dump()); return $this->map; }