mutliple attachment multiple fields

This commit is contained in:
yuri
2019-07-02 15:46:10 +03:00
parent c95fcd6fbd
commit 1c8abd6e36
5 changed files with 80 additions and 69 deletions
@@ -35,20 +35,29 @@ class Attachments extends HasChildren
{
$parentRelation = parent::load($linkName, $entityName);
$relation = array(
$entityName => array (
'fields' => array(
$linkName.'Types' => array(
$relation = [
$entityName => [
'fields' => [
$linkName.'Types' => [
'type' => 'jsonObject',
'notStorable' => true,
),
),
),
);
],
],
'relations' => [
$linkName => [
'conditions' => [
'OR' => [
['field' => null],
['field' => $linkName],
],
],
],
],
],
];
$relation = \Espo\Core\Utils\Util::merge($parentRelation, $relation);
return $relation;
}
}
@@ -1,48 +0,0 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2019 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Utils\FieldManager\Hooks;
use Espo\Core\Exceptions\Conflict;
class AttachmentMultipleType extends Base
{
public function beforeSave($scope, $name, $defs, $options)
{
if (!empty($options['isNew'])) {
$fieldDefs = $this->getMetadata()->get(['entityDefs', $scope, 'fields'], array());
foreach ($fieldDefs as $field => $defs) {
$type = $this->getMetadata()->get(['entityDefs', $scope, 'fields', $field, 'type']);
if ($type === 'attachmentMultiple') {
throw new Conflict("Attachment-Multiple field already exists in '{$scope}'. There can be only one Attachment-Multiple field per entity type.");
}
}
}
}
}
+9 -7
View File
@@ -232,6 +232,10 @@ abstract class Mapper implements IMapper
$params['limit'] = 1;
}
if (!empty($relDefs['conditions']) && is_array($relDefs['conditions'])) {
$params['whereClause'][] = $relDefs['conditions'];
}
$resultArr = [];
$sql = $this->query->createSelectQuery($relEntity->getEntityType(), $params);
@@ -1005,16 +1009,14 @@ abstract class Mapper implements IMapper
. " AND "
. "{$relTable}.deleted = " . $this->pdo->quote(0) . "";
$conditions = $conditions ?? [];
if (!empty($relDefs['conditions']) && is_array($relDefs['conditions'])) {
foreach ($relDefs['conditions'] as $f => $v) {
$join .= " AND {$relTable}." . $this->toDb($f) . " = " . $this->pdo->quote($v);
}
$conditions = array_merge($conditions, $relDefs['conditions']);
}
if (!empty($conditions) && is_array($conditions)) {
foreach ($conditions as $f => $v) {
$join .= " AND {$relTable}." . $this->toDb($f) . " = " . $this->pdo->quote($v);
}
if (!empty($conditions)) {
$conditionsSql = $this->query->buildJoinConditionsStatement($entity, $relTable, $conditions);
$join .= " AND " . $conditionsSql;
}
return $join;
+35 -5
View File
@@ -1638,10 +1638,41 @@ abstract class Base
return implode(' ', $joinSqlList);
}
public function buildJoinConditionsStatement($entity, $alias = null, array $conditions)
{
$sql = '';
$joinSqlList = [];
foreach ($conditions as $left => $right) {
$joinSqlList[] = $this->buildJoinConditionStatement($entity, $alias, $left, $right);
}
if (count($joinSqlList)) {
$sql .= implode(" AND ", $joinSqlList);
}
return $sql;
}
protected function buildJoinConditionStatement($entity, $alias = null, $left, $right)
{
$sql = '';
if (is_array($right) && (is_int($left) || in_array($left, ['AND', 'OR']))) {
$logicalOperator = 'AND';
if ($left == 'OR') {
$logicalOperator = 'OR';
}
$sqlList = [];
foreach ($right as $k => $v) {
$sqlList[] = $this->buildJoinConditionStatement($entity, $alias, $k, $v);
}
$sql = implode(' ' .$logicalOperator . ' ', $sqlList);
return $sql;
}
$operator = '=';
$isNotValue = false;
@@ -1748,6 +1779,10 @@ abstract class Base
$alias = $this->sanitize($alias);
if (!empty($relOpt['conditions']) && is_array($relOpt['conditions'])) {
$conditions = array_merge($conditions, $relOpt['conditions']);
}
$type = $relOpt['type'];
switch ($type) {
@@ -1769,10 +1804,6 @@ abstract class Base
. " AND "
. "{$midAlias}.deleted = " . $this->pdo->quote(0);
if (!empty($relOpt['conditions']) && is_array($relOpt['conditions'])) {
$conditions = array_merge($conditions, $relOpt['conditions']);
}
$joinSqlList = [];
foreach ($conditions as $left => $right) {
$joinSqlList[] = $this->buildJoinConditionStatement($entity, $midAlias, $left, $right);
@@ -1797,7 +1828,6 @@ abstract class Base
. " AND "
. "{$alias}.deleted = " . $this->pdo->quote(0) . "";
$joinSqlList = [];
foreach ($conditions as $left => $right) {
$joinSqlList[] = $this->buildJoinConditionStatement($entity, $alias, $left, $right);
+18
View File
@@ -256,6 +256,24 @@ class QueryTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($expectedSql, $sql);
}
public function testJoinConditions3()
{
$sql = $this->query->createSelectQuery('Note', [
'select' => ['id'],
'leftJoins' => [['post', 'post', [
'OR' => [
['name' => 'test'],
['post.name' => null],
]
]]],
'withDeleted' => true,
]);
$expectedSql = "SELECT note.id AS `id` FROM `note` LEFT JOIN `post` AS `post` ON post.name = 'test' OR post.name IS NULL";
$this->assertEquals($expectedSql, $sql);
}
public function testJoinTable()
{
$sql = $this->query->createSelectQuery('Post', [