Numerous CakePHP Nested Connections

I have an application in which some of the models are associated with hasMany / belongs to associations. For example, A hasMany B, B hasMany C, C hasMany D and D hasMany E. In addition, E belongs to D, D belongs to C, C belongs to B, and B belongs to A. Using Containable behavior was great for managing the amount of information returned with each request, but I seem to have a problem trying to get data from table A when using a condition that includes table D. For example, here is an example of my "A" model:

class A extends AppModel {
    var $name = 'A';

    var $hasMany = array(
        'B' => array('dependent' => true)
    );

    function findDependentOnE($condition) {
        return $this->find('all', array(
            'contain' => array(
                'B' => array(
                    'C' => array(
                        'D' => array(
                            'E' => array(
                                'conditions' => array(
                                    'E.myfield' => $some_value
                                )
                            )
                        )
                    )
                )
            )
        ));
    }
}

This still returns me all the entries in "A", and if this is related to the entries in "E", they do not satisfy the condition, then I just get the following:

Array(
    [0] => array(
        [A] => array(
            [field1] => // stuff
            [field2] => // more stuff
            // ...etc
        ),
        [B] => array(
            [field1] => // stuff
            [field2] => // more stuff
            // ...etc
        ),
        [C] => array(
            [field1] => // stuff
            [field2] => // more stuff
            // ...etc
        ),
        [D] => array(
            [field1] => // stuff
            [field2] => // more stuff
            // ...etc
        ),
        [E] => array( 
            // empty if 'E.myfield' != $some_value'
        )
    ),
    [1] => array( // ...etc )
)

If 'E.myfield'!= $some_value, , .

, ...

, , -/CakePHP-y :

SELECT * 
FROM A INNER JOIN
        (B INNER JOIN 
            (C INNER JOIN 
                (D INNER JOIN 
                    E ON D.id=E.d_id) 
                ON C.id=D.c_id) 
            ON B.id=C.b_id) 
        ON A.id=B.a_id 
    WHERE E.myfield = $some_value
+3
1

- , Containable contain Model::find. Model::find :

A; B, A; C, B; D, C; , E, D, E .

D, C, B, A. SQL, , contain .

CakePHP , , hasOne A E. , , . - (: untested):

$this->bindModel(array('hasOne'=>array(
    'B'=>array(
        'foreignKey' => false,
        'conditions' => array('A.id = B.a_id')
    ),
    'C'=>array(
        'foreignKey' => false,
        'conditions' => array('B.id = C.b_id')
    ),
    'D'=>array(
        'foreignKey' => false,
        'conditions' => array('C.id = D.c_id')
    ),
    'E'=>array(
        'foreignKey' => false,
        'conditions' => array('D.id = E.d_id')
    )
)));

$this->find('all', array(
    'conditions' => array( 'E.my_field' => $some_value )
));

E.my_value Model::find Set::extract :

$results = $this->find('all', array(
    'contain' => array(
        'B' => array(
            'C' => array(
                'D' => array(
                    'E' => array()
                )
            )
        )
    )
));
return Set::extract("/A/B/C/D/E[my_field={$some_value}]/../../../../", $results);

Set::extract, , .

EDIT. , Set::extract, . PHP.

+2

Source: https://habr.com/ru/post/1738495/


All Articles