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

Custom Foreign Key (Not Normalized Database Names) #282

Open pdrappo opened 5 years ago

pdrappo commented 5 years ago

Hi everyone, i looking for some help solving a problem using Spot2. I have working in a project who has a database already made. The fields of the tables are not normalized, so i'm not able or i can't figure it out how to make a relation between two tables. I want to connect People with Cities:

class Persona extends \Spot\Entity
{
    public static function fields()
    {
      return [
      ...
          "PERSONA_Id" => ["type" => "integer", "unsigned" => true, "primary" => true, "autoincrement" => true],
          "PERSONA_Nombre" => ["type" => "string", "length" => 255],
          "CODIGOPOSTALES_Id" => ["type" => "string", "length" => 50],
         ...
      ];
    }

    public static function relations(Mapper $mapper, Entity $entity)
    { 
      return [
        'localidad' => $mapper->belongsTo($entity, 'App\Entities\Sga\CodLoc', 'CODIGOPOSTALES_Id')
      ];
    }

}
class CodLoc extends \Spot\Entity
{
    public static function fields()
    {
      return [
          "id" => ["type" => "integer", "unsigned" => true, "primary" => true, "autoincrement" => true],
          "CodLoc" => ["type" => "string", "length" => 5],
          "DesLoc" => ["type" => "string", "length" => 250]
      ];
    }

}

When i fetch Persona with Persona_Id i want to get the correspondig City when CODIGOPOSTALES_Id = DesLoc"

How can i do this?? Thanks