Linking Models on Fly

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

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

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.

1 Like

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

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

How do i close the post?

Thanks