Asked  2 Years ago    Answers:  5   Viewed   69 times

I have this situation:

Abstract Class:

abstract class AbstractBase
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @var integer
     */
    protected $id;

    /**
     * @ORMColumn(type="datetime", name="updated_at")
     * @var DateTime $updatedAt
     */
    protected $updatedAt;

    /**
     * @ORMPreUpdate
     */
    public function setUpdatedAt()
    {
        die('THIS POINT IS NEVER REACHED');
        $this->updatedAt = new DateTime();
    }
}

Concrete Class:

/**
 * @ORMEntity(repositoryClass="EntityRepositoryUserRepository")
 * @ORMTable(name="users")
 * @ORMHasLifecycleCallbacks
 */
class User extends AbstractBase
{
    // some fields, relations and setters/getters defined here, these all work as expected.
}

Then i call it in my controller like this:

$user = $this->em->find('EntityUser', 1);
// i call some setters here like $user->setName('asd');
$this->em->flush();
die('end');

Everything works as expected, so the id field from the abstract class gets created for the User entity, i can access it etc. The problem is, that the line "die('THIS POINT IS NEVER REACHED')" is never reached. (Note the @ORMPreUpdate) This means that lifecycleCallbacks are not called on inherited objects. Is this a bug, or is there a reason for this?

 Answers

2

Your abstract base class has to be anotated as Mapped Superclasses and include the HasLifecycleCallbacks-Annotation.

Further Information: Inheritance Mapping in the Doctrine Documentation.

/**
 * @ORMMappedSuperclass
 * @ORMHasLifecycleCallbacks
 */
abstract class AbstractBase
{
    [...]

    /**
     * @ORMPreUpdate
     */
    public function setUpdatedAt()
    {
        $this->updatedAt = new DateTime();
    }
}

/**
 * @ORMEntity(repositoryClass="EntityRepositoryUserRepository")
 * @ORMTable(name="users")
 */
class User extends AbstractBase
{
    // some fields, relations and setters/getters defined here, these all work as expected.
}
Thursday, October 6, 2022
 
joulss
 
3

See Serializing Entities in doctrine manual: (Everything you save in a session is serialized and deserialized.)

Serializing entities can be problematic and is not really recommended, at least not as long as an entity instance still holds references to proxy objects or is still managed by an EntityManager.

There is a technical limitation that avoid private properties from being serialized when an entity is proxied (lazy-loaded entities are proxied).

This means that you have to avoid using private properties for the entities you want to serialize (use protected entities instead).

Also, if a lazy-loaded entity is not loaded at serialization time, it won't be loadable after de-serialization. So you have to make sure the entity is fully loaded before serializing it.

Sunday, November 20, 2022
 
4

This bug is fixed in Doctrine 2.4

https://github.com/doctrine/doctrine2/issues/2934

Wednesday, October 19, 2022
 
coco
 
2

The syntax for the declaration of metaclasses has changed in Python 3. Instead of the __metaclass__ field, Python 3 uses a keyword argument in the base-class list:

import abc

class AbstractBase(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def must_implement_this_method(self):
        raise NotImplementedError()

Calling d = ConcreteClass() will raise an exception now, because a metaclass derived from ABCMeta can not be instantiated unless all of its abstract methods and properties are overridden (For more information see @abc.abstractmethod):

TypeError: Can't instantiate abstract class ConcreteClass with abstract methods
must_implement_this_method

Hope this helps :)

Monday, October 24, 2022
5

When a virtual function returns a pointer or reference to a class, a class which inherits from the base class and overrides the function is allowed to change the return type to a pointer or reference to a class which is derived from the original return type.

You can't return base by value as it is abstract so you can't actually create one by itself.

http://en.wikipedia.org/wiki/Covariant_return_type

When using virtual functions and base classes, you usually have to use dynamic allocation to create your objects. I suggest you look into smart pointers to help manage the memory.

Sunday, October 2, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages