mass relate 3

This commit is contained in:
yuri
2015-02-27 12:10:16 +02:00
parent 74c9d5a8e6
commit 64b98a7a3c
4 changed files with 66 additions and 16 deletions
+31 -15
View File
@@ -231,27 +231,39 @@ class Record extends Base
public function actionCreateLink($params, $data)
{
if (empty($params['id']) || empty($params['link'])) {
throw BadRequest();
}
$id = $params['id'];
$link = $params['link'];
$foreignIds = array();
if (isset($data['id'])) {
$foreignIds[] = $data['id'];
}
if (isset($data['ids']) && is_array($data['ids'])) {
foreach ($data['ids'] as $foreignId) {
$foreignIds[] = $foreignId;
if (!empty($data['massRelate'])) {
if (empty($data['where'])) {
throw new BadRequest();
}
$where = json_decode(json_encode($data['where']), true);
return $this->getRecordService()->linkEntityMass($id, $link, $where);
} else {
$foreignIds = array();
if (isset($data['id'])) {
$foreignIds[] = $data['id'];
}
if (isset($data['ids']) && is_array($data['ids'])) {
foreach ($data['ids'] as $foreignId) {
$foreignIds[] = $foreignId;
}
}
}
$result = false;
foreach ($foreignIds as $foreignId) {
if ($this->getRecordService()->linkEntity($id, $link, $foreignId)) {
$result = $result || true;
$result = false;
foreach ($foreignIds as $foreignId) {
if ($this->getRecordService()->linkEntity($id, $link, $foreignId)) {
$result = true;
}
}
if ($result) {
return true;
}
}
if ($result) {
return true;
}
throw new Error();
@@ -262,6 +274,10 @@ class Record extends Base
$id = $params['id'];
$link = $params['link'];
if (empty($params['id']) || empty($params['link'])) {
throw BadRequest();
}
$foreignIds = array();
if (isset($data['id'])) {
$foreignIds[] = $data['id'];
+3 -1
View File
@@ -427,7 +427,9 @@ abstract class Mapper implements IMapper
$sql = "INSERT INTO `".$relTable."` (".$fieldsPart.") (".$subSql.") ON DUPLICATE KEY UPDATE deleted = '0'";
return $this->pdo->query($sql);
if ($this->pdo->query($sql)) {
return true;
}
break;
}
@@ -274,6 +274,10 @@ class RDB extends \Espo\ORM\Repository
return false;
}
public function massRelate(Entity $entity, $relationName, array $params = array())
{
return $this->getMapper()->massRelate($entity, $relationName, $params);
}
public function getAll()
{
+28
View File
@@ -598,6 +598,34 @@ class Record extends \Espo\Core\Services\Base
}
}
public function linkEntityMass($id, $link, $where)
{
$entity = $this->getEntity($id);
$entityType = $entity->getEntityType();
$foreignEntityType = $entity->relations[$link]['entity'];
if (!$this->getAcl()->check($entity, 'edit')) {
throw new Forbidden();
}
if (empty($foreignEntityType)) {
throw new Error();
}
if (!$this->getAcl()->check($foreignEntityType, 'edit')) {
throw new Forbidden();
}
if (!is_array($where)) {
$where = array();
}
$params['where'] = $where;
$selectParams = $this->getRecordService($foreignEntityType)->getSelectParams($params);
return $this->getRepository()->massRelate($entity, $link, $selectParams);
}
public function massUpdate($attributes = array(), $ids = array(), $where = array())
{
$idsUpdated = array();