spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

[question] Problem with entity relations (probably my fault, I want help) #246

Closed luchothoma closed 6 years ago

luchothoma commented 6 years ago

This is my actual code: I have 2 clases LocalDeComida (like a restaurante) that has many Notificacion (notifications)

LocalDeComidaEntity.php `<?php namespace Entity; class LocalDeComida extends \Spot\Entity { protected static $mapper = 'Entity\Mapper\Reporte'; protected static $table = 'casa_de_comida';

public static function fields()
{
    return [    
        'id'            => ['type' => 'integer', 'primary' => true, 'autoincrement' => true],
        'nombre'        => ['type' => 'integer', 'required' => true],
        'maxMesas'        => ['type' => 'string', 'required' => true],
        'imagenURL'         => ['type' => 'string', 'required' => true]
    ];
}

public static function relations(Mapper $mapper, Entity $entity)
{
    return [
        'notifications' => $mapper->hasMany($entity, 'Entity\Notificacion', 'casadecomidaID'),
    ];
}

} ?>`

NotificacionEntity.php `<?php namespace Entity; class Notificacion extends \Spot\Entity { protected static $mapper = 'Entity\Mapper\Notificacion'; protected static $table = 'notificacion';

public static function fields()
{
    return [    
        'id'                => ['type' => 'integer', 'primary' => true, 'autoincrement' => true],
        'casadecomidaID'    => ['type' => 'integer', 'required' => true],
        'tipoNotificacion'  => ['type' => 'integer', 'required' => true],
        'atendido'          => ['type' => 'boolean', 'required' => true],
        'mesaNUM'           => ['type' => 'integer', 'required' => true],
        'data'              => ['type' => 'string', 'required' => true],
        'momentoRealizada'  => ['type' => 'datetime', 'required' => true]
    ];
}

public static function relations(Mapper $mapper, Entity $entity)
{
    return [
        'local' => $mapper->belongsTo($entity, 'Entity\LocalDeComida', 'id')
    ];
}

} // This is the 27 line of the file ?>`

And when I try to use it as here: $localDeComidaMapper->get(1)->notifications() Throws this exception Fatal error: Declaration of Entity\Notificacion::relations() must be compatible with Spot\EntityInterface::relations(Spot\MapperInterface $mapper, Spot\EntityInterface $entity) in NotificacionEntity.php on line 27

Can you give me some advice please ?

willemwollebrants commented 6 years ago

Your typehints are for the classes (Mapper, Entity), but they should be for the interfaces (MapperInterface, EntityInterface):

class Notificacion extends \Spot\Entity{
// ...
    public static function relations(MapperInterface $mapper, EntityInterface $entity){
// ...
     }
}
luchothoma commented 6 years ago

Really thanks to your hint !!!

Finally I have to do this: public static function relations(\Spot\MapperInterface $mapper, \Spot\EntityInterface $entity)

I suppose that If i put use \Spot; at the beginning of file, I will write exactly what you answer to me, so I should use it like that ?

Another thing, I am new to Spot but I like it, but this "obvious" problem can be "fix" it, if in the documentation where like you explane and not as it is now. [http://phpdatamapper.com/docs/relations/](Spot Doc Relations)

willemwollebrants commented 6 years ago

If you put

use Spot\MapperInterface;
use Spot\EntityInterface;

at the top of the file, you don't need the full classname. The PHP documentation has excellent info on namespaces

I've sent a PR for the docs :)