试图保存实体时出现分割错误



我使用Apigility来创建API和Doctrine作为ORM。以下代码在flush()时生成一个分段错误(我删除了一些验证):

class AdvertisersResource extends AbstractResourceListener 
{
    public function create($data)
    {
        $entityClass = $this->getEntityClass();
        $advertiserEntity = new $entityClass;
        $connection = $this->entityManager->getConnection();
        // rejecting if the posted data already contains an advertiser_id
        if (!empty($data->advertiser_id)) {
            return new ApiProblem(406, 'Advertiser ID cannot be provided at creation time.');
        }
        // generating the next advertiser_id
        $sql = "
        SELECT
            MAX(advertiser_id) + 1 as advertiser_id
        FROM
            advertisers
        ";
        $result = $connection->fetchAssoc($sql);
        if (empty($result)) {
            // error generating the new category_id
            return new ApiProblem(500, 'Database error.');
        }
        $advertiserEntity->setAdvertiserId((int)$result['advertiser_id']);
        // input data validation
        $advertisersRepository = $this->entityManager->getRepository('ioV1RestAdvertisersAdvertisersEntity');
        // advertiser name uniqueness
        $result = $advertisersRepository->findBy(array('advertiserName' => $advertiserEntity->getAdvertiserName()));
        if (!empty($result)) {
            return new ApiProblem(406, 'There is already an advertiser with this name.');
        }
        // validate geo_id
        $sql = "
        SELECT
            countryCode
        FROM
            countries (NOLOCK)
        WHERE
            countryCode = '{$data->geo_id}'
        ";
        $result = $connection->fetchAssoc($sql);
        if (empty($result)) {
            return new ApiProblem(406, 'Invalid geo ID: ' . $data->geo_id . ".");
        }
        $advertiserEntity->setGeoId($data->geo_id);
        $advertiserEntity->setAdvertiserName($data->advertiser_name);
        // ?????????????????????????????????????????
        // ????????  WHY IS THIS NECESSARY  ????????
        // without it -> segmentation fault
        $connection->close();
        // ?????????????????????????????????????????
        $this->entityManager->persist($advertiserEntity);
        $this->entityManager->flush();
        $categories = $advertiserEntity->getCategories();
        $advertiserEntity->setCategories(Util::extractCollection($categories));
        return $advertiserEntity;
    }
}

我只有在不使用$connection->close();时才得到分割错误,所以我想,不知怎的,连接仍然以某种方式挂起,但我无法得到一个清楚的解释,为什么会发生这种情况。

您使用的是Doctrine的最新版本吗?另外,请尝试升级PHP版本。

最新更新