Goal:
My goal is to be able to call $entity->legalName()->first()
and get the entities legal name.
Models:
##### Entity #####
namespace Entity\Eloquent;
class Entity extends \Eloquent{
public static $key = 'uuid';
public function names(){
return $this->has_many(
'Entity\Eloquent\EntityName',
'entity_uuid'
);
}
public function legalName(){
return $this->has_one(
'Entity\Eloquent\EntityName',
'entity_name_id_legal_name'
);
}
}
##### EntityName #####
namespace Entity\Eloquent;
class EntityName extends \Eloquent{
/**
* NOTE: I've tried a few things, but
* currently, this model is empty.
* /
}
Attempted:
$entity = Entity::find($uuid);
$legalName = $entity->legalName()->get();
echo '<pre>LegalName:<br />',var_dump($legalName),'</pre>';
###########################################
## ERROR:
## Column not found: 1054 Unknown column 'entity_name_id_legal_name' in 'where clause'
## SQL: SELECT * FROM `m3sandbox_company_name` WHERE `company_name_id_legal_name` = ?
## Bindings: array ( 0 => 'a7ae2ea4-b342-11e2-9575-ba8c8f281c14')
###########################################
Also Attempted:
I also tried changing the Entity->legalName()
relationship method to:
public function legalName(){
return $this->has_one
(
'Entity\Eloquent\EntityName',
'id'
);
}
While the results were different, they were still incorrect:
$entity = Entity::find($uuid);
$legalName = $entity->legalName()->get();
echo '<pre>LegalName:<br />',var_dump($legalName),'</pre>';
###########################################
## prints: null
###########################################
Data Description:
An entity can have N names, so there is a one-to-many relationship between entity.uuid
(one) and entity_name.entity_uuid
(many).
An entity can only have ONE legal name, though, so there is a one-to-one relationship between entity.entity_name_id_legal_name
and entity_name.id
.
Data:
INSERT INTO `entity`
(`uuid`, `entity_name_id_legal_name`, `datetime_created`)
VALUES
('a7ae2ea4-b342-11e2-9575-ba8c8f281c14', 39657, '2011-06-29 02:12:06');
INSERT INTO `entity_name`
(`id`, `entity_uuid`, `value`, `datetime_created`)
VALUES
(516, 'a7ae2ea4-b342-11e2-9575-ba8c8f281c14', 'Bingo', '2011-06-29 02:12:06'),
(3965, 'a7ae2ea4-b342-11e2-9575-ba8c8f281c14', 'Bango', '2013-05-17 00:00:00'),
(39657, 'a7ae2ea4-b342-11e2-9575-ba8c8f281c14', 'Bongo', '2013-05-17 00:00:00');
Summary:
I need to get the entity-has-one-legalName relationship operational.
What am I missing?
Where am I going wrong?