Skip to content

Commit 10b7ab1

Browse files
authored
Fix title field name guessing (#835)
* fix title field collision * fix title field name guess * ii * stricter replace
1 parent 9650b49 commit 10b7ab1

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

src/Reference.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ protected function initTableAlias(): void
195195
$ourModel = $this->getOurModel();
196196

197197
$this->table_alias = $this->link;
198-
$this->table_alias = preg_replace('/_' . ($ourModel->id_field ?: 'id') . '/', '', $this->table_alias);
199-
$this->table_alias = preg_replace('/([a-zA-Z])[a-zA-Z]*[^a-zA-Z]*/', '\1', $this->table_alias);
198+
$this->table_alias = preg_replace('~_(' . preg_quote($ourModel->id_field, '~') . '|id)$~', '', $this->table_alias);
199+
$this->table_alias = preg_replace('~([a-z])[a-z]*[^a-z]*~i', '$1', $this->table_alias);
200200
if (isset($ourModel->table_alias)) {
201201
$this->table_alias = $ourModel->table_alias . '_' . $this->table_alias;
202202
}

src/Reference/HasOneSql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function addTitle(array $defaults = []): FieldSqlExpression
177177
{
178178
$ourModel = $this->getOurModel();
179179

180-
$fieldName = $defaults['field'] ?? preg_replace('/_' . $ourModel->id_field . '$/i', '', $this->link);
180+
$fieldName = $defaults['field'] ?? preg_replace('~_(' . preg_quote($ourModel->id_field, '~') . '|id)$~', '', $this->link);
181181

182182
if ($ourModel->hasField($fieldName)) {
183183
throw (new Exception('Field with this name already exists. Please set title field name manually addTitle([\'field\'=>\'field_name\'])'))

0 commit comments

Comments
 (0)