That's a tough one! :-)

I cannot give you a straight answer for that one because I never
needed that but I can try to give you some clues. Please note that
these are just ideas and they might not even be possible.

Also, please check that Cake cannot supply to DB connections to the
same Model before getting in this trouble (AFAIK, 1 connection per
Model...but I might be wrong...) :-)

There's a class on the Cake API named "DboMysql" that provides a
connection with a MySQL DB. I think that somewhere "under the hood"
there should be an instance of this class on the Model. You can
override Cake's native Model and thus create ANOTHER MySQL connection
(look in the manual for the procedure to do so...). By then you can
have TWO DB connections on the same Model. This solves one part of the
problem.

The other is the relationship between the tables. I'm pretty sure that
this is the easy part. By messing with the Model's vars and by
creating a couple of methods to encapsulate the database switching you
can "emulate" Cake's 'belongsTo' behaviour for certain.

One extra tip: you can use Model's method execute() to force the DB
change:

//...on the Model

function aMethod($param)
{
  $this->execute("USE B");
 ...
}

function anotherMethod($param)
{
 $this->execute("USE A");
...
}

I hope I have pointed some direction for your problem's solution.

Good luck and don't forget to report back here if you find a
solution... :-)
You might save someone else a lot of work... :-)

> Let's say I have a MySQL database named "A" and another one named "B".
> Within "A", I have a table named A1. Within "B" I have a table
> named B1. Let us now say that A.A1 has a foreign key in B.B1.
>
> So, I'm trying to write a model for A.A1 with a foreign key to B.B1.
> Here's where I get stuck:
>
> class A1 extends AppModel
> {
>   var $name = 'A1';
>   var $useDbConfig = 'dbConfig_A';
>
>   var $belongsTo = array('B1' => array('className' => 'B1'));
>
> }
>
> I think the above is more or less correct, but as you can see B1 will be
> searched within database "A", not database "B". And that's the problem I
> was describing.
>
> Even though I could specify a database connection in a model, here I
> have a situation where I need both connections because the tables are in
> different databases (although both are under the same instance of the
> MySQL server).
>
> Hope there's a simple answer to this problem. Thanks for all your help.

> Best regards,
>   Sergei
>
>
>
> > Good luck.
>
> > On Apr 8, 2:53 pm, "[EMAIL PROTECTED]"
> > <[EMAIL PROTECTED]> wrote:
> > > Hi everybody,
>
> > > I'm considering converting my application into CakePHP.
>
> > > Currently I have 3 databases within my application and the first one
> > > has several tables that are referenced by tables in the other two
> > > databases.
>
> > > Problem 1. I have tables that are named the same in at least two
> > > databases. Can models, controllers and views be isolated to a folder
> > > that would correspond to a specific database? Cake Views are already
> > > in a folder by the controller name but models and controllers exist in
> > > the same directories.
>
> > > Problem 2. I don't know how to describe a model that has a
> > > relationship with a table from a different database.
>
> > > What's the best way to handle this situation in Cake?
>
> --
>
> Sergei Gerasenko
> IT Department
> PublicSchoolWORKS
> Phone: (513)-631-6111


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to