CakePHP 实体包含没有外键
CakePHP Entity contain without foreign key
我有一个实体别墅,我希望这个实体包含其他具有相同 'complex' (Varchar(255)
) 的别墅。
class VillasTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('villas');
$this->displayField('name');
$this->primaryKey('id');
$this->hasMany('Complexs', [
'className' => 'Villas',
'foreignKey' => false,
'propertyName' => 'complexs',
'conditions' => ['Complexs.complex' => 'Villas.complex']
]);
}
}
?>
不知道有没有可能。我不想在每个需要这些实体的函数中添加一个查找。我还想在使用这个新字段的实体中创建一个函数。
``
尽管使用 VARCHAR(255)
作为外键可能效率很低 and/or 将需要大量索引,但我想通常一个选项定义另一个 table 在这里会派上用场,类似于 targetForeignKey
用于 belongsToMany
关联。您可能想要 suggest that as an enhancement.
结果格式化程序
目前这似乎无法使用关联开箱即用,因此您可能必须 select 并自己注入关联记录,例如在结果格式化程序中。
$Villas
->find()
->formatResults(function($results) {
/* @var $results \Cake\Datasource\ResultSetInterface|\Cake\Collection\Collection */
// extract the custom foreign keys from the results
$keys = array_unique($results->extract('complex')->toArray());
// find the associated rows using the extracted foreign keys
$villas = \Cake\ORM\TableRegistry::get('Villas')
->find()
->where(['complex IN' => $keys])
->all();
// inject the associated records into the results
return $results->map(function ($row) use ($villas) {
if (isset($row['complex'])) {
// filters the associated records based on the custom foreign keys
// and adds them to a property on the row/entity
$row['complexs'] = $villas->filter(function ($value, $key) use ($row) {
return
$value['complex'] === $row['complex'] &&
$value['id'] !== $row['id'];
})
->toArray(false);
}
return $row;
});
});
这将在之后使用自定义外键获取关联的行,并注入结果,这样您最终会得到实体上的关联记录。
另见 Cookbook > Database Access & ORM > Query Builder > Adding Calculated Fields
可能还有其他选择,例如使用收集必要密钥的自定义预加载程序,结合使用正确密钥将结果拼接在一起的自定义关联 class,请参阅
我有一个实体别墅,我希望这个实体包含其他具有相同 'complex' (Varchar(255)
) 的别墅。
class VillasTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('villas');
$this->displayField('name');
$this->primaryKey('id');
$this->hasMany('Complexs', [
'className' => 'Villas',
'foreignKey' => false,
'propertyName' => 'complexs',
'conditions' => ['Complexs.complex' => 'Villas.complex']
]);
}
}
?>
不知道有没有可能。我不想在每个需要这些实体的函数中添加一个查找。我还想在使用这个新字段的实体中创建一个函数。 ``
尽管使用 VARCHAR(255)
作为外键可能效率很低 and/or 将需要大量索引,但我想通常一个选项定义另一个 table 在这里会派上用场,类似于 targetForeignKey
用于 belongsToMany
关联。您可能想要 suggest that as an enhancement.
结果格式化程序
目前这似乎无法使用关联开箱即用,因此您可能必须 select 并自己注入关联记录,例如在结果格式化程序中。
$Villas
->find()
->formatResults(function($results) {
/* @var $results \Cake\Datasource\ResultSetInterface|\Cake\Collection\Collection */
// extract the custom foreign keys from the results
$keys = array_unique($results->extract('complex')->toArray());
// find the associated rows using the extracted foreign keys
$villas = \Cake\ORM\TableRegistry::get('Villas')
->find()
->where(['complex IN' => $keys])
->all();
// inject the associated records into the results
return $results->map(function ($row) use ($villas) {
if (isset($row['complex'])) {
// filters the associated records based on the custom foreign keys
// and adds them to a property on the row/entity
$row['complexs'] = $villas->filter(function ($value, $key) use ($row) {
return
$value['complex'] === $row['complex'] &&
$value['id'] !== $row['id'];
})
->toArray(false);
}
return $row;
});
});
这将在之后使用自定义外键获取关联的行,并注入结果,这样您最终会得到实体上的关联记录。
另见 Cookbook > Database Access & ORM > Query Builder > Adding Calculated Fields
可能还有其他选择,例如使用收集必要密钥的自定义预加载程序,结合使用正确密钥将结果拼接在一起的自定义关联 class,请参阅