1

I'm using Apigility for creating an API and Doctrine as ORM. The following code generates a segmentation fault at flush() time (I removed some of the validation I made):

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;
    }
}

I'm getting the Segmentation fault only when not using $connection->close();, so I suppose that somehow the connections remains hanging somehow, but I wasn't able to get a clear explanation on why this happens.

1 Answers1

0

Are you using the latest Doctrine version? Also, try upgrading your PHP version.

Rob Allen
  • 12,643
  • 1
  • 40
  • 49