How to bind a model in CakePHP for fields not named by convention?

I have two tables with a field usernamein both. How can I specify a field name for a local and external table?

I want CakePHP to do something like

ON (`T1`.`username` = `T2`.`username`)`

as a result. Without any changes, the tables will be joined with the following condition:

ON (`T1`.`id` = `T2`.`t1_id`)`

Setting a property is 'foreign_key' = 'username'not enough because it raises a request like this:

ON (`t1`.`id` = `t2`.`username`)`

I have two solutions. The first uses the join property and joins the table on the fly. In this case, I can set both the local and the external field. But if I need to join more tables with this manually linked one, I can no longer use contain. I need to write the following joins manually, even if these associations were set correctly. So I need to write long connection definitions every time, instead just use'contain' => array('T1', 'T2', 'T3')

"primary_key" . . , "" 'id'. - , , .

, , - , , CakePHP . , , - . , , 'foreign_key' .

+4
5

ForeignKey false

, , 'foreignKey' => false.

.. :

class Comment extends AppModel {
    public $belongsTo = array(
        'Profile' => array(
        )
    );
}

sql:

SELECT ... LEFT JOIN profiles on ON (Profile.id = Comment.profile_id)

, foreignKey :

class Comment extends AppModel {
    public $belongsTo = array(
        'Profile' => array(
            'foreignKey' => false
        )
    );
}

() sql:

SELECT ... LEFT JOIN profiles on ON ()

:

class Comment extends AppModel {
    public $belongsTo = array(
        'Profile' => array(
            'foreignKey' => false,
             'conditions' => array(
                 'Comment.username = Profile.username'
             ),
        )
    );
}

( , ), :

 SELECT ... LEFT JOIN profiles on ON (Comment.username = Profile.username)
+12

Update:

, foreignKey, ( :

'foreignKey' => false 'conditions' => 'Comment.username = User.username'

PS - , , , .


CakePHP: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany

class User extends AppModel {
    public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'user_id', // <---- THIS HERE you can define the foreign_key
            'conditions' => array('Comment.status' => '1'),
            'order' => 'Comment.created DESC',
            'limit' => '5',
            'dependent' => true
        )
    );
}

?:

, .

+6

, hasMany,

class User extends AppModel {
    public $hasMany = array(
        'Comment' => array(
            'finderQuery' => 'SELECT * FROM comments Comment 
                              LEFT JOIN users User 
                              ON Comment.username = User.username 
                              WHERE User.id = {__CakeID__}'
        )
    );
}

,

+3

I struggled with this exact problem for several days, and the solution I found (not explicitly stated above): hasManyshould have 'foreignKey' => false, but belongsToMUST also have 'foreignKey' => falseand have 'conditions' => 'Comment.username = User.username'. C conditionsdo not associate an array as a string. For example:

// Profile
public $hasMany = array(
    'Comment' => array(
        'foreignKey' => false,
    )
);

// Comment
public $belongsTo = array(
    'Profile' => array(
        'foreignKey' => false,
        'conditions' => array('Profile.username = Comment.username')
    )
);
+1
source
public $hasMany = array(
    'Comment' => array(
        'className' => 'T1',
        'foreignKey' => 'username',
        'dependent' => true
    )
);

Note that the dependent true value will delete all T1 when T2 is removed from the cake.

0
source

All Articles