0

在 Symfony2 中加载固定装置时,我遇到了一个非常奇怪的错误。我已经使用了netbeans调试器并检查了错误中提到的变量的值,这就是它应该是的!!!因为我真的不知道错误可能是什么,所以我发布了整个夹具类。输出的错误是:

PHP 致命错误:在第 77 行的 /home/victor/projects/elcuadre/src/ElCuadre/OffersBundle/DataFixtures/ORM/LoadOfferData.php 中的非对象上调用成员函数 setGeolocation()

代码是:

<?php

namespace ElCuadre\OffersBundle\DataFixtures\ORM;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\DataFixtures\AbstractFixture;
use ElCuadre\OffersBundle\Entity\GeoLocation;
use ElCuadre\OffersBundle\Entity\LatLng;
use ElCuadre\OffersBundle\Entity\Offer;
use ElCuadre\OffersBundle\Entity\Availability;
use ElCuadre\OffersBundle\Entity\Picture;
use ElCuadre\OffersBundle\Entity\Experience;

class LoadOfferData extends AbstractFixture implements FixtureInterface {

    public function buildLatLngs($points) {
        $result = new ArrayCollection();
        foreach ($points as $point) {
            $result[] = new LatLng($point["x"], $point["y"]);
        }
        return $result;
    }

    public function buildGeoLocation($centerLat, $centerLong, $mapType, $type, $zoom) {
        $geoloc = new GeoLocation();
        $geoloc->setCenterLat($centerLat);
        $geoloc->setCenterLong($centerLong);
        $geoloc->setMapType($mapType);
        $geoloc->setType($type);
        $geoloc->setZoom($zoom);
        return $geoloc;
    }

    public function loadGeoLocation(ObjectManager $manager, $centerLat, $centerLong, $mapType, $type, $zoom, $points) {
        $geoloc = $this->buildGeoLocation($centerLat, $centerLong, $mapType, $type, $zoom);
        $latlngs = $this->buildLatLngs($points);
        $geoloc->setLatLngs($latlngs);
        $manager->persist($geoloc);
        foreach ($latlngs as $latlng) {
            $latlng->setGeolocation($geoloc);
            $manager->persist($latlng);
        }
        return $geoloc;
    }

    public function buildExperience($title,$location,$type){
        $exp = new Experience();
        $exp->setTitle($title);
        $exp->setDescription($title);
        $exp->setLocation($location);
        $exp->setType($type);
    }

    public function load(ObjectManager $manager) {
        $geoloc = $this->loadGeoLocation($manager, 
                5.968144043645581, -62.53726971960451,
                'HYBRID', 'Marker', 14,
    array("x" => 5.968144043645581, "y" => -62.53726971960451));
        $pictures = new ArrayCollection();
        $pictures[] = Picture::newPicture('Salto Angel A', 'Salto Angel A', 
                'Salto Angel', "imgs/content/salto angel/A.jpg");
        $pictures[] = Picture::newPicture('Salto Angel 1', 'Salto Angel 1', 
                'Salto Angel', "imgs/content/salto angel/1.jpg");
        $pictures[] = Picture::newPicture('Salto Angel 2', 'Salto Angel 2', 
                'Salto Angel', "imgs/content/salto angel/2.jpg");
        $pictures[] = Picture::newPicture('Salto Angel 3', 'Salto Angel 3', 
                'Salto Angel', "imgs/content/salto angel/3.jpg");
        $pictures[] = Picture::newPicture('Salto Angel 4', 'Salto Angel 4', 
                'Salto Angel', "imgs/content/salto angel/4.jpg");
        $pictures[] = Picture::newPicture('Salto Angel 5', 'Salto Angel 5', 
                'Salto Angel', "imgs/content/salto angel/5.jpg");

        $exp = $this->buildExperience('Salto Angel 3 días y 2 noches',
                'Salto Angel - Bolivar - Venezuela','P');
        $exp->setGeolocation($geoloc);
        foreach ($pictures as $picture) {
            $picture->setExperience($exp);
            $manager->persist($picture);
        }

        $offer = new Offer();
        $offer->setTitle('Salto Angel 3 días y 2 noches');
        $offer->setDescription('Salto Angel 3 días y 2 noches');
        $offer->setPrice(1233.43);
        $offer->setDiscount(.12);
        $offer->setStart(new \DateTime('07-04-2012'));
        $offer->setEnd(new \DateTime('09-04-2012'));
        $offer->setDescription('3 dias y 2 noches para dos personas en habitacion Suite con desayuno incluido');
        $offer->setLocation('Caracas');
        $offer->setMaxPerUser(2);
        $offer->setIsFeatured(true);
        $availabilities = new ArrayCollection();
        $availabilities[] = new Availability(new \DateTime('07-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('08-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('09-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('10-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('11-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('12-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('13-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('14-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('15-04-2012'), 5);
        $availabilities[] = new Availability(new \DateTime('16-04-2012'), 5);
        foreach ($availabilities as $aval) {
            $offer->addAvailability($aval);
            $aval->setOffer($offer);
            $manager->persist($aval);
        }

        $manager->flush();
    }

}

?>

我在调试器中检查了变量的值,$geoloc它应该是(ElCuadre\OffersBundle\Entity\GeoLocation)。

那么,夹具包中是否存在错误?或者,我做错了吗?我能做些什么来解决这个错误?

4

1 回答 1

1

在 buildExperience 方法的最后,添加:

return $exp;
于 2012-08-17T06:21:34.200 回答