0
votes

Doctrine2 seems to be adding magic to its Proxy object for lazy loading. It's making my results incorrect, and I can't figure out what is causing it.

Here is my class model:

Class "RedProduct" inherits from abstract class "Product", which implements interface "BaseProduct"

abstract class Product holds the primary key:

abstract class Product implements BaseProduct {
   /** @Id @Column (type="integer", name="ID") @GeneratedValue */
    protected $id;

    public function getId() {    
        return $this->id;        
    }                            
}

I want RedProduct to prepend the letter 'R' to the id before returning it.

class RedProduct extends Product {
    public function getId() {
       return 'R' . $this->id;
    }
}

But in the proxy class, the getId() method (and ONLY the getId() method) has been modified to this:

public function getId()
{
    if ($this->__isInitialized__ === false) {
        return $this->_identifier["id"];
    }
    $this->__load();
    return parent::getId();
}

This means my object doesn't return the correct id when it's not initialized!

Is "getId" a reserved or magic method for Doctrine2? When I create other methods in both base class and inherited class, it doesn't have this effect on the Proxy.

1

1 Answers

-1
votes

If you check the getId() method on the proxy, you will see that it has this line:

return parent::getId();

Which means it will call the getId() function defined on your model class (RedProduct), since all of the proxies extend the corresponding model. The problem lies elsewhere.

I'm not sure what you are trying to achieve by modifying the returned id from the model, but Doctrine does not call your getters when persisting entities, it uses reflection, so if the problem is that your ids are incorrect in the database, this might be the cause.