我結束瞭解除綁定並重新綁定模型來解決我的問題,下面的代碼是內beforeFind()
// If this function is being called with ContainableBehavior being used, then the associations of User
// have already been adjusted to perform the query correctly. However, after find() functions are executed,
// models, and their associated models have their associations reset to defaults.
// Because we execute TenantsUser->find by calling the function below, TenantsUser will reset the associations
// within the User model thereby destroying all the work that ContainableBehavior did.
// For this reason, before calling TenantsUser->find(), we unbind the User Model from the TenantsUser Model, which means
// that the Users associations are NOT reset.
$this->TenantsUser->unbindModel(
array('belongsTo' => array('User')),
false // We have to force the unbind to remain, otherwise the associations will be reset.
);
$user_ids = $this->TenantsUser->getUserIdsForTenant($this->getCurrentTenantId(), $tenants_users_statuses);
// Rebind the model.
$this->TenantsUser->bindModel(
array('belongsTo' => array('User'))
);