目标:
我的目标是能够调用$entity->legalName()->first()
并获取实体的合法名称。
楷模:
##### 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.
* /
}
尝试:
$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')
###########################################
还尝试过:
我还尝试将Entity->legalName()
关系方法更改为:
public function legalName(){
return $this->has_one
(
'Entity\Eloquent\EntityName',
'id'
);
}
虽然结果不同,但它们仍然不正确:
$entity = Entity::find($uuid);
$legalName = $entity->legalName()->get();
echo '<pre>LegalName:<br />',var_dump($legalName),'</pre>';
###########################################
## prints: null
###########################################
数据说明:一个实体可以有N个名字,所以(one)和(many)
之间是一对多的关系。但是,一个实体只能有一个法定名称,因此和
之间存在一对一的关系。entity.uuid
entity_name.entity_uuid
entity.entity_name_id_legal_name
entity_name.id
数据:
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');
摘要:
我需要使 entity-has-one-legalName 关系正常运行。
我错过了什么?
我哪里错了?