3

I've implemented the NotifyPropertyChanged to my entity Event. The Event entity contains a embeddable Address.

I also got a service which changes the Address (adds GeoLocation) and sets a geoCoded-Field to true in the Event.

The boolean field (geoCoded) is persisted as expected, but the changes made in the Address entity are lost.

I've tried to implement the NotifyPropertyChanged in the Address too, but it did not work. I've also tried to call $event->setAddress($address); after making the changes, but that did not help either.

class Event implements NotifyPropertyChanged
{
    /**
     * @var array
     */
    private $_listeners = [];

    /**
     * @var Address
     *
     * @ORM\Embedded(class="App\Entity\Address")
     */
    protected $address;

    /**
     * @var boolean
     *
     * @ORM\Column(type="boolean", nullable=true)
     */
    protected $geoCoded;

    /**
     * @param PropertyChangedListener $listener
     */
    public function addPropertyChangedListener(PropertyChangedListener $listener)
    {
        $this->_listeners[] = $listener;
    }

    /**
     * @param $propName
     * @param $oldValue
     * @param $newValue
     */
    protected function _onPropertyChanged($propName, $oldValue, $newValue)
    {
        if ($this->_listeners) {
            foreach ($this->_listeners as $listener) {
                $listener->propertyChanged($this, $propName, $oldValue, $newValue);
            }
        }
    }

    /**
     * Event constructor.
     */
    public function __construct()
    {
        $this->address = new Address();
    }

    /**
     * @return Address
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param Address $address
     */
    public function setAddress($address)
    {
        if ($address != $this->address) {
            $this->_onPropertyChanged('address', $this->address, $address);
            $this->address = $address;
        }
    }

    /**
     * @return bool
     */
    public function isGeoCoded(): ?bool
    {
        return $this->geoCoded;
    }

    /**
     * @param bool $geoCoded
     * @return Event
     */
    public function setGeoCoded(bool $geoCoded): Event
    {
        if ($geoCoded != $this->geoCoded) {
            $this->_onPropertyChanged('geoCoded', $this->geoCoded, $geoCoded);
            $this->geoCoded = $geoCoded;
        }

        return $this;
    }
}


class Address
{

    /**
     * @var string
     *
     * @ORM\Column(type="decimal", precision=18, scale=12, nullable=true)
     */
    protected $latitude;

    /**
     * @var string
     * 
     * @ORM\Column(type="decimal", precision=18, scale=12, nullable=true)
     */
    protected $longitude;

    /**
     * @return string
     */
    public function getLatitude()
    {
        return $this->latitude;
    }

    /**
     * @param string $latitude
     */
    public function setLatitude($latitude)
    {
        $this->latitude = $latitude;
    }

    /**
     * @return string
     */
    public function getLongitude()
    {
        return $this->longitude;
    }

    /**
     * @param string $longitude
     */
    public function setLongitude($longitude)
    {
        $this->longitude = $longitude;
    }
}

The important part of my service:

foreach ($events as $event) {
    $address = $event->getAddress();

    if (!$address->__toString()) {
        $this->skipItem($event);
        continue;
    }

    $result = $this->geoCoder->geocodeQuery(GeocodeQuery::create($address));

    if (!$result->count()) {
        $this->skipItem($event);
        continue;
    }

    $coordinates = $result->first()->getCoordinates();

    $address->setLatitude($coordinates->getLatitude());
    $address->setLongitude($coordinates->getLongitude());
    $address->setRadiusInM(0);
    $event->setGeoCoded(true);

    // Printing the changeset
}


I'm expecting the changeset to contain the address changes too, but the geoCoded field is the only one that changed in this changeset.

array:1 [
  "geoCoded" => array:2 [
    0 => null
    1 => true
  ]
]
Spomsoree
  • 123
  • 1
  • 8

0 Answers0