get teams and relations save

This commit is contained in:
Yuri Kuznetsov
2013-12-23 13:59:06 +02:00
parent 43ee44b6e9
commit 452910b02c
4 changed files with 82 additions and 14 deletions

View File

@@ -7,8 +7,7 @@ class Repository extends \Espo\ORM\Repository
public function save(Entity $entity)
{
$nowString = date('Y-m-d H:i:s', time());
$nowString = date('Y-m-d H:i:s', time());
$restoreData = array();
if ($entity->isNew()) {
@@ -47,6 +46,40 @@ class Repository extends \Espo\ORM\Repository
parent::save($entity);
$entity->set($restoreData);
$this->handleSpecifiedRelations($entity);
}
protected function handleSpecifiedRelations(Entity $entity)
{
foreach ($entity->getRelations() as $name => $defs) {
if ($defs['type'] == $entity::HAS_MANY || $defs['type'] == $entity::MANY_MANY) {
$fieldName = $name . 'Ids';
if ($entity->has($fieldName)) {
$specifiedIds = $entity->get($fieldName);
if (is_array($ids)) {
$toRemoveIds = array();
$existingIds = array();
foreach ($entity->get($name) as $foreignEntity) {
$existingIds[] = $foreignEntity->id;
}
foreach ($existingIds as $id) {
if (!in_array($id, $specifiedIds)) {
$toRemoveIds[] = $id;
}
}
foreach ($specifiedIds as $id) {
if (!in_array($id, $existingIds)) {
$this->relate($entity, $name, $id);
}
}
foreach ($toRemoveIds as $id) {
$this->unrelate($entity, $name, $id);
}
}
}
}
}
}
}

View File

@@ -171,13 +171,7 @@ abstract class Entity implements IEntity
}
public function toArray()
{
/*$arr = $this->valuesContainer;
if (isset($this->id)) {
$arr['id'] = $this->id;
}
return $arr;*/
{
$arr = array();
if (isset($this->id)) {
$arr['id'] = $this->id;
@@ -192,6 +186,16 @@ abstract class Entity implements IEntity
return $arr;
}
public function getFields()
{
return $this->fields;
}
public function getRelations()
{
return $this->relations;
}
public function getFetchedValue($fieldName)
{
if (isset($this->initialValuesContainer[$fieldName])) {

View File

@@ -131,14 +131,29 @@ class Repository
return $this->mapper->countRelated($entity, $relationName, $params);
}
public function relate(Entity $entity, $relationName, $foreignEntity)
public function relate(Entity $entity, $relationName, $foreign)
{
return $this->mapper->relate($entity, $relationName, $foreignEntity);
if ($foreign instanceof Entity) {
return $this->mapper->relate($entity, $relationName, $foreign);
}
if (is_string($foreign)) {
return $this->mapper->addRelation($entity, $relationName, $foreign);
}
return false;
}
public function unrelate(Entity $entity, $relationName, $foreignEntity)
public function unrelate(Entity $entity, $relationName, $foreign)
{
return $this->mapper->unrelate($entity, $relationName, $foreignEntity);
if ($foreign instanceof Entity) {
return $this->mapper->unrelate($entity, $relationName, $foreign);
}
if (is_string($foreign)) {
return $this->mapper->removeRelation($entity, $relationName, $foreign);
}
if ($foreign === true) {
return $this->mapper->removeAllRelations($entity, $relationName);
}
return false;
}
public function getAll()

View File

@@ -77,7 +77,23 @@ class Record extends \Espo\Core\Services\Base
public function getEntity($id = null)
{
return $this->getRepository()->get($id);
$entity = $this->getRepository()->get($id);
if (!empty($id)) {
if ($entity->hasRelation('teams')) {
$teams = $entity->get('teams');
$ids = array();
$names = array();
foreach ($teams as $team) {
$ids[] = $team->id;
$names[$team->id] = $team->name;
}
$entity->set('teamsIds', $ids);
$entity->set('teamsNames', $names);
}
}
return $entity;
}
protected function getSelectManager()