# How to add a virtual field on belongsToMany-\>setForeignKey?

**URL:** <https://discourse.cakephp.org/t/how-to-add-a-virtual-field-on-belongstomany-setforeignkey/7841>\
**Category:** Need Help\
**Created:** [May 20, 2020, 9:31pm UTC](https://discourse.cakephp.org/t/how-to-add-a-virtual-field-on-belongstomany-setforeignkey/7841 "2020-05-20T21:31:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![celsowm](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@celsowm](https://discourse.cakephp.org/u/celsowm)\
**Post date:** [May 20, 2020, 9:31pm UTC](https://discourse.cakephp.org/t/how-to-add-a-virtual-field-on-belongstomany-setforeignkey/7841/1 "2020-05-20T21:31:41Z")

</div>

Hi !  
I’ve need to create on-the-fly a association between the same table, processo\_judicial, where another table contains reference to “both”.

My current code is:

```
$pjTable = TableRegistry::get('ProcessoJudicial');

$pjTable->belongsToMany('Originario',
[
'className'=>'ProcessoJudicial',
])
->setForeignKey('valor')
->setBindingKey('numero')
->setThrough('ProcessoJudicialOutroParametro')
->setConditions(['ProcessoJudicialOutroParametro.nome'=>'PROCESSO_ORIGINARIO']);

$pjs = $pjTable->find()->contain('Originario')
->where(['ProcessoJudicial.id IN'=>[134997,134998,135002,135041,135052]])
->limit(10);
debug($pjs->toArray());

```

The problem is that this table has values like:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/4faa5a0c3f9bf6165852de78bc13e4e891d73b3d.png)

In processo\_judicial I have the field “numero” without “non-digits chars”:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/417d54fd72c27f0aaa0152a08137f49ecf2b8e9e.png)

So I need someway to CakeORM change this SQL comparison using this:

```
WHERE ProcessoJudicialOutroParametro.valor 
in (c0, c1, c2)

```

to this:

```
WHERE (replace(replace(ProcessoJudicialOutroParametro.valor,'-',''),'.','')
in (c0, c1, c2)

```

How can I achive this?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 21, 2020, 12:25am UTC](https://discourse.cakephp.org/t/how-to-add-a-virtual-field-on-belongstomany-setforeignkey/7841/2 "2020-05-21T00:25:30Z")

</div>

As formulated, this is actually a SQL question, not a Cake question. You are looking for something that runs in the SQL query, not PHP code.

However! Do you have any control over the `processo_judicial_outro_parametro` table? If so, you might be able to create an additional column in there which has the number without the punctuation in it, which your query could look at.

Or, why does the `processo_judicial` table _not_ have the punctuation in it? Any chance that it could be added back in there?

---

<div class="post-metadata">

**Author:** ![celsowm](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@celsowm](https://discourse.cakephp.org/u/celsowm)\
**Post date:** [May 21, 2020, 12:54am UTC](https://discourse.cakephp.org/t/how-to-add-a-virtual-field-on-belongstomany-setforeignkey/7841/3 "2020-05-21T00:54:23Z")

</div>

I cannot change any table
