CakePHP Model BelongsTo
Can someone explain me what the fo开发者_高级运维llowing means ?
var $belongsTo = array(
'**EventOrganiser**' => array(
'className' => '**EventOrganiser**',
'foreignKey' => '**event_organiser_id**',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
i have marked the content with Content in the code. What does each of the EventOrganiser refer to ?
The association name:
$belongsTo = array( 'EventOrganiser' => array(
This is the name the association will be accessibly as from the model:
$this->Foo->EventOrganizer->... array( 'Foo' => array( ... ), 'EventOrganizer' => array( ... ) )
This is a completely freeform name, you can name it whatever you want. It will reflect in query results when searched from this model and when accessing the related model from this model.
The class name:
'className' => 'EventOrganiser',
That's the class name, the actual model name, that shall be used for the related model.
The foreign key:
'foreignKey' => 'event_organiser_id',
That's the foreign key column name in the database that shall be used for this association.
The latter two are not freeform, they need to be correct. Cake can usually guess them based on naming conventions of the association name.
精彩评论