# Linking Models on Fly

**URL:** <https://discourse.cakephp.org/t/linking-models-on-fly/2922>\
**Category:** Need Help\
**Created:** [July 23, 2017, 7:55pm UTC](https://discourse.cakephp.org/t/linking-models-on-fly/2922 "2017-07-23T19:55:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![suarezl.1w](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@suarezl.1w](https://discourse.cakephp.org/u/suarezl.1w)\
**Post date:** [July 23, 2017, 7:55pm UTC](https://discourse.cakephp.org/t/linking-models-on-fly/2922/1 "2017-07-23T19:55:12Z")

</div>

Hello, I am creating two models on the fly, these models are related by a field, I am trying to relate them but it seems that I was not taking the parameters that I am passing to the moment of creating the model, look;

`  
$mainDboObject = new Model();  
$mainDboObject-\>name = ‘MainTable’;  
$mainDboObject-\>alias = ‘MainTable’;  
$mainDboObject-\>useDbConfig = ‘db1’;  
$mainDboObject-\>useTable = $value;  
$mainDboObject-\>primaryKey = ‘id’;

```
            $DiscDboObject = new Model();
            $DiscDboObject->name = 'DiscTable';
            $DiscDboObject->alias = 'DiscTable';
            $DiscDboObject->useDbConfig = 'db1';
            $DiscDboObject->useTable = 'C1_' . $value;
            $DiscDboObject->primaryKey = 'id';

```

`  
THEN;

> $mainDboObject-\>hasOne = array(  
> ‘DiscTable’ =\> array(  
> ‘className’ =\> ‘DiscTable’,  
> ‘foreignKey’ =\> ‘INDICE’,  
> )  
> );

$mainDboObject-\>Behaviors-\>attach(‘Containable’);  
$mainDboObject-\>find(‘first’, array(  
‘contain’ =\> array(  
‘DiscTable’ =\> array(  
‘fields’ =\> array(  
‘DiscTable.\*’  
)  
)  
)  
));

I GET THIS: Error: Table disc\_tables for model DiscTable was not found in datasource default.

Somebody?? I’m using cakephp 2.9

---

<div class="post-metadata">

**Author:** ![xaphalanx](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xaphalanx/32/575_2.png) [@xaphalanx](https://discourse.cakephp.org/u/xaphalanx)\
**Post date:** [July 24, 2017, 1:17pm UTC](https://discourse.cakephp.org/t/linking-models-on-fly/2922/2 "2017-07-24T13:17:25Z")

</div>

Here’s the documentation on how to link a model on the fly.

[https://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly](https://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly)

Read that first.

In Cake2.x you get a model from the registry: ClassRegistry::init(‘YourModel’); Doing that. I would not try newing one up on my own.

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [July 24, 2017, 1:19pm UTC](https://discourse.cakephp.org/t/linking-models-on-fly/2922/3 "2017-07-24T13:19:10Z")

</div>

The main question is WHY you want to create models on the fly?

---

<div class="post-metadata">

**Author:** ![suarezl.1w](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@suarezl.1w](https://discourse.cakephp.org/u/suarezl.1w)\
**Post date:** [July 24, 2017, 2:10pm UTC](https://discourse.cakephp.org/t/linking-models-on-fly/2922/4 "2017-07-24T14:10:27Z")

</div>

Thanks a lot!

Solve using the option joins the set of options at the time of the query and it was not necessary to make the relation explicitly in the model.

Regards

---

<div class="post-metadata">

**Author:** ![suarezl.1w](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@suarezl.1w](https://discourse.cakephp.org/u/suarezl.1w)\
**Post date:** [July 24, 2017, 2:16pm UTC](https://discourse.cakephp.org/t/linking-models-on-fly/2922/5 "2017-07-24T14:16:58Z")

</div>

How do i close the post?

Thanks
