我正在使用 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();
,所以我想不知何故连接仍然以某种方式挂起,但我无法清楚地解释为什么会发生这种情况。