This repository has been archived on 2026-07-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
espocrm-base/upgrades/5.8/scripts/AfterUpgrade.php
T
Yuri Kuznetsov 949dc96e7b diff imrovements
2019-12-11 13:52:07 +02:00

67 lines
2.6 KiB
PHP

<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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/.
************************************************************************/
class AfterUpgrade
{
public function run($container)
{
$entityManager = $container->get('entityManager');
$this->populateOpportunityContactId($entityManager);
}
protected function populateOpportunityContactId($entityManager)
{
$pdo = $entityManager->getPdo();
$sql = "
SELECT opportunity.id AS 'opportunityId', contact.id AS `contactId` FROM `opportunity`
JOIN contact_opportunity ON contact_opportunity.opportunity_id = opportunity.id AND contact_opportunity.deleted = 0
JOIN contact ON contact.id = contact_opportunity.contact_id AND contact.deleted = 0
WHERE
contact.id IN (
SELECT MIN(contact.id)
FROM `opportunity`
JOIN contact_opportunity ON contact_opportunity.opportunity_id = opportunity.id AND contact_opportunity.deleted = 0
JOIN contact ON contact.id = contact_opportunity.contact_id AND contact.deleted = 0
GROUP BY opportunity.id
) AND
opportunity.contact_id IS NULL AND
opportunity.deleted = 0
";
$sth = $pdo->prepare($sql);
$sth->execute();
while ($row = $sth->fetch()) {
$cId = $row['contactId'] ?? null;
$oId = $row['opportunityId'] ?? null;
if (!$cId || !$oId) continue;
$q = "
UPDATE `opportunity` SET contact_id = ".$pdo->quote($cId)." WHERE id = ".$pdo->quote($oId)."
";
$pdo->query($q);
}
}
}