What's the correct way to write Model-> find () in CakePhp for three tables?

can anyone tell me what is the best way to do something like, from db like

Table 1
-id
-data
-foreign_key_id_table2

Table 2
-id
-data
-foreign_key_id_table3

Table 3
-id
-data

      

I need to select all elements of table 1 related to all elements in table 2 of a given table 3 records something like

select table1.data from table1 where table1.foreign_key_id_table2 in (select table2.id from table2 where table2.foreign_key_id_table3 = X)

      

but i need to do it with cakephp and since i am new to this i cant figure out how

please, help:)

+1


a source to share


2 answers


Okay, I'll try to help "teach a man to fish."

Cake models represent database tables. Each model can have associations like belongsTo, hasMany, hasOne, etc. When you define an association, the cake recognizes it and gets all the data you need.

For example, let's say you have a Post model and a model comment. If you have established a relationship between the two (Post-hasMany-Comment and Comment-belongsTo-Post), the cake will work magic every time you do Post-> find () or Comment-> find ()

So what does this mean? If you do something like this:

$ data = $ this-> Post-> findByTitle ('your title title');

the $ data variable will contain not only your Post data, but all associated comments. You can always see what's there by callingdebug($data);

The deep cake layer will go and the corresponding data can be set using the recursive property / parameter or using the built-in Containable behavior.



Another benefit of defining associations between models (for the answer to your answer to Rob's answer) is that you don't have to put all of your models in the $ uses var. In fact, it would be wrong.

Believe it or not, once you've identified your associations correctly, this will work:

class PostsController extends AppController
{
    // not really needed, cake detects it automagically
    var $uses = array('Post');

    function index()
    {
        $comments = $this->Post->Comment->find('all');
    }
}

      

Sweet, isn't it?

In your case, it would be something like this:

$this->Table1->find
    (
        'all',
        array
        (
            'conditions' => array
            (
                'Table2.foreign_key_id_table3' => X
            )
        )
    );

      

There are many other great tips when you work with the built-in functions and cake classes, so I suggest you take some time to learn the cake properly, it will make your life easier.

+4


a source


I would recommend using Cake's method find()

for models. All you have to do is create the appropriate model associations and Cake will do the job for you. In your case it looks like Table1 model belongsTo

Table2 and Table2 belongsTo

Table3.

Table 2 would then have a relationship to hasOne

either hasMany

Table 1 and Table 3 would have the same relationship to Table 2.

To secure the real world, consider the ubiquitous blogging app. A User

may Posts

or Post

may not have many Comments

:

Database

users
 - id
 - data

posts
 - id
 - user_id
 - data

comments
 - id
 - post_id
 - data

User

Model



class User extends AppModel {
   $ hasMany = array ('Post');

   // ...
}

Post

Model

class Post extends AppModel {
   $ hasMany = array ('Comment');
   $ belongsTo = array ('User');

   // ...
}

Comment

Model

class Comment extends AppModel {
   $ belongsTo = array ('Post');

   // ...
}

Once you have the associations in place, simply call the find()

method you are interested in and Cake will return related entries of the other classes.

+3


a source







All Articles