尝试保存实体时出现分段错误

时间:2014-07-15 13:58:28

标签: doctrine segmentation-fault persist connection-close apigility

我使用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('io\V1\Rest\Advertisers\AdvertisersEntity');

        // 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();的情况下才得到分段错误,所以我想某种联系仍然以某种方式悬挂,但我无法清楚地解释为什么会发生这种情况

1 个答案:

答案 0 :(得分:0)

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