2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Behavior for binding management.
|
|
|
|
*
|
|
|
|
* Behavior to simplify manipulating a model's bindings when doing a find operation
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5669
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-08 15:35:02 +00:00
|
|
|
* Behavior to allow for dynamic and atomic manipulation of a Model's associations
|
|
|
|
* used for a find call. Most useful for limiting the amount of associations and
|
|
|
|
* data returned.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2011-10-15 16:17:10 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class ContainableBehavior extends ModelBehavior {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Types of relationships available for models
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $types = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Runtime configuration for this behavior
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $runtime = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-02-04 05:00:59 +00:00
|
|
|
* Initiate behavior for the model using specified settings.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* Available settings:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* - recursive: (boolean, optional) set to true to allow containable to automatically
|
2009-02-04 05:00:59 +00:00
|
|
|
* determine the recursiveness level needed to fetch specified models,
|
|
|
|
* and set the model recursiveness to this level. setting it to false
|
|
|
|
* disables this feature. DEFAULTS TO: true
|
|
|
|
* - notices: (boolean, optional) issues E_NOTICES for bindings referenced in a
|
|
|
|
* containable call that are not valid. DEFAULTS TO: true
|
2008-05-30 11:40:08 +00:00
|
|
|
* - autoFields: (boolean, optional) auto-add needed fields to fetch requested
|
2009-02-04 05:00:59 +00:00
|
|
|
* bindings. DEFAULTS TO: true
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param Model $Model Model using the behavior
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $settings Settings to override for model.
|
2011-07-30 22:38:57 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function setup(Model $Model, $settings = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($this->settings[$Model->alias])) {
|
|
|
|
$this->settings[$Model->alias] = array('recursive' => true, 'notices' => true, 'autoFields' => true);
|
|
|
|
}
|
2008-07-30 20:34:01 +00:00
|
|
|
$this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Runs before a find() operation. Used to allow 'contain' setting
|
|
|
|
* as part of the find call, like this:
|
|
|
|
*
|
2009-10-06 01:01:31 +00:00
|
|
|
* `Model->find('all', array('contain' => array('Model1', 'Model2')));`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-10-06 01:01:31 +00:00
|
|
|
* {{{
|
2008-05-30 11:40:08 +00:00
|
|
|
* Model->find('all', array('contain' => array(
|
|
|
|
* 'Model1' => array('Model11', 'Model12'),
|
|
|
|
* 'Model2',
|
|
|
|
* 'Model3' => array(
|
|
|
|
* 'Model31' => 'Model311',
|
|
|
|
* 'Model32',
|
|
|
|
* 'Model33' => array('Model331', 'Model332')
|
|
|
|
* )));
|
2009-10-06 01:01:31 +00:00
|
|
|
* }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $Model Model using the behavior
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $query Query parameters as set by cake
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return array
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function beforeFind(Model $Model, $query) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$reset = (isset($query['reset']) ? $query['reset'] : true);
|
2010-12-12 20:48:36 +00:00
|
|
|
$noContain = (
|
|
|
|
(isset($this->runtime[$Model->alias]['contain']) && empty($this->runtime[$Model->alias]['contain'])) ||
|
|
|
|
(isset($query['contain']) && empty($query['contain']))
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$contain = array();
|
|
|
|
if (isset($this->runtime[$Model->alias]['contain'])) {
|
|
|
|
$contain = $this->runtime[$Model->alias]['contain'];
|
|
|
|
unset($this->runtime[$Model->alias]['contain']);
|
|
|
|
}
|
|
|
|
if (isset($query['contain'])) {
|
|
|
|
$contain = array_merge($contain, (array)$query['contain']);
|
|
|
|
}
|
2010-12-12 20:48:36 +00:00
|
|
|
if (
|
2011-08-16 03:55:08 +00:00
|
|
|
$noContain || !$contain || in_array($contain, array(null, false), true) ||
|
2010-12-12 20:48:36 +00:00
|
|
|
(isset($contain[0]) && $contain[0] === null)
|
|
|
|
) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($noContain) {
|
|
|
|
$query['recursive'] = -1;
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
if ((isset($contain[0]) && is_bool($contain[0])) || is_bool(end($contain))) {
|
|
|
|
$reset = is_bool(end($contain))
|
|
|
|
? array_pop($contain)
|
|
|
|
: array_shift($contain);
|
|
|
|
}
|
|
|
|
$containments = $this->containments($Model, $contain);
|
|
|
|
$map = $this->containmentsMap($containments);
|
|
|
|
|
|
|
|
$mandatory = array();
|
|
|
|
foreach ($containments['models'] as $name => $model) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$instance = $model['instance'];
|
2008-05-30 11:40:08 +00:00
|
|
|
$needed = $this->fieldDependencies($instance, $map, false);
|
|
|
|
if (!empty($needed)) {
|
|
|
|
$mandatory = array_merge($mandatory, $needed);
|
|
|
|
}
|
|
|
|
if ($contain) {
|
|
|
|
$backupBindings = array();
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($this->types as $relation) {
|
2010-03-14 19:34:57 +00:00
|
|
|
if (!empty($instance->__backAssociation[$relation])) {
|
|
|
|
$backupBindings[$relation] = $instance->__backAssociation[$relation];
|
|
|
|
} else {
|
|
|
|
$backupBindings[$relation] = $instance->{$relation};
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
foreach ($this->types as $type) {
|
|
|
|
$unbind = array();
|
|
|
|
foreach ($instance->{$type} as $assoc => $options) {
|
|
|
|
if (!isset($model['keep'][$assoc])) {
|
|
|
|
$unbind[] = $assoc;
|
|
|
|
}
|
2008-06-23 04:41:15 +00:00
|
|
|
}
|
2008-06-23 06:33:31 +00:00
|
|
|
if (!empty($unbind)) {
|
2008-06-23 04:41:15 +00:00
|
|
|
if (!$reset && empty($instance->__backOriginalAssociation)) {
|
|
|
|
$instance->__backOriginalAssociation = $backupBindings;
|
|
|
|
}
|
|
|
|
$instance->unbindModel(array($type => $unbind), $reset);
|
|
|
|
}
|
|
|
|
foreach ($instance->{$type} as $assoc => $options) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($model['keep'][$assoc]) && !empty($model['keep'][$assoc])) {
|
|
|
|
if (isset($model['keep'][$assoc]['fields'])) {
|
|
|
|
$model['keep'][$assoc]['fields'] = $this->fieldDependencies($containments['models'][$assoc]['instance'], $map, $model['keep'][$assoc]['fields']);
|
|
|
|
}
|
|
|
|
if (!$reset && empty($instance->__backOriginalAssociation)) {
|
|
|
|
$instance->__backOriginalAssociation = $backupBindings;
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif ($reset) {
|
2009-11-07 22:43:18 +00:00
|
|
|
$instance->__backAssociation[$type] = $backupBindings[$type];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$instance->{$type}[$assoc] = array_merge($instance->{$type}[$assoc], $model['keep'][$assoc]);
|
|
|
|
}
|
|
|
|
if (!$reset) {
|
|
|
|
$instance->__backInnerAssociation[] = $assoc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->settings[$Model->alias]['recursive']) {
|
|
|
|
$query['recursive'] = (isset($query['recursive'])) ? $query['recursive'] : $containments['depth'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$autoFields = ($this->settings[$Model->alias]['autoFields']
|
|
|
|
&& !in_array($Model->findQueryType, array('list', 'count'))
|
|
|
|
&& !empty($query['fields']));
|
2010-12-12 20:48:36 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$autoFields) {
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query['fields'] = (array)$query['fields'];
|
2010-04-06 01:43:16 +00:00
|
|
|
foreach (array('hasOne', 'belongsTo') as $type) {
|
|
|
|
if (!empty($Model->{$type})) {
|
|
|
|
foreach ($Model->{$type} as $assoc => $data) {
|
2010-04-20 02:38:18 +00:00
|
|
|
if ($Model->useDbConfig == $Model->{$assoc}->useDbConfig && !empty($data['fields'])) {
|
2010-04-06 01:43:16 +00:00
|
|
|
foreach ((array) $data['fields'] as $field) {
|
|
|
|
$query['fields'][] = (strpos($field, '.') === false ? $assoc . '.' : '') . $field;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-20 02:38:18 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($mandatory[$Model->alias])) {
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($mandatory[$Model->alias] as $field) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($field == '--primaryKey--') {
|
|
|
|
$field = $Model->primaryKey;
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (preg_match('/^.+\.\-\-[^-]+\-\-$/', $field)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
list($modelName, $field) = explode('.', $field);
|
2010-04-20 02:38:18 +00:00
|
|
|
if ($Model->useDbConfig == $Model->{$modelName}->useDbConfig) {
|
|
|
|
$field = $modelName . '.' . (
|
|
|
|
($field === '--primaryKey--') ? $Model->$modelName->primaryKey : $field
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$field = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($field !== null) {
|
|
|
|
$query['fields'][] = $field;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$query['fields'] = array_unique($query['fields']);
|
|
|
|
return $query;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Unbinds all relations from a model except the specified ones. Calling this function without
|
|
|
|
* parameters unbinds all related models.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $Model Model on which binding restriction is being applied
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2011-10-15 16:17:10 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#using-containable
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function contain(Model $Model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$contain = call_user_func_array('am', array_slice($args, 1));
|
|
|
|
$this->runtime[$Model->alias]['contain'] = $contain;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Permanently restore the original binding settings of given model, useful
|
|
|
|
* for restoring the bindings after using 'reset' => false as part of the
|
|
|
|
* contain call.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $Model Model on which to reset bindings
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function resetBindings(Model $Model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($Model->__backOriginalAssociation)) {
|
|
|
|
$Model->__backAssociation = $Model->__backOriginalAssociation;
|
|
|
|
unset($Model->__backOriginalAssociation);
|
|
|
|
}
|
|
|
|
$Model->resetAssociations();
|
|
|
|
if (!empty($Model->__backInnerAssociation)) {
|
|
|
|
$assocs = $Model->__backInnerAssociation;
|
2010-12-12 20:48:36 +00:00
|
|
|
$Model->__backInnerAssociation = array();
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($assocs as $currentModel) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->resetBindings($Model->$currentModel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Process containments for model.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $Model Model on which binding restriction is being applied
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $contain Parameters to use for restricting this model
|
|
|
|
* @param array $containments Current set of containments
|
2011-12-02 05:58:09 +00:00
|
|
|
* @param boolean $throwErrors Whether non-existent bindings show throw errors
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Containments
|
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function containments(Model $Model, $contain, $containments = array(), $throwErrors = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery');
|
|
|
|
$keep = array();
|
|
|
|
if ($throwErrors === null) {
|
|
|
|
$throwErrors = (empty($this->settings[$Model->alias]) ? true : $this->settings[$Model->alias]['notices']);
|
|
|
|
}
|
|
|
|
foreach ((array)$contain as $name => $children) {
|
|
|
|
if (is_numeric($name)) {
|
|
|
|
$name = $children;
|
|
|
|
$children = array();
|
|
|
|
}
|
|
|
|
if (preg_match('/(?<!\.)\(/', $name)) {
|
|
|
|
$name = str_replace('(', '.(', $name);
|
|
|
|
}
|
|
|
|
if (strpos($name, '.') !== false) {
|
|
|
|
$chain = explode('.', $name);
|
|
|
|
$name = array_shift($chain);
|
2009-11-19 22:13:35 +00:00
|
|
|
$children = array(implode('.', $chain) => $children);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$children = (array)$children;
|
|
|
|
foreach ($children as $key => $val) {
|
|
|
|
if (is_string($key) && is_string($val) && !in_array($key, $options, true)) {
|
|
|
|
$children[$key] = (array) $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = array_keys($children);
|
|
|
|
if ($keys && isset($children[0])) {
|
|
|
|
$keys = array_merge(array_values($children), $keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($keys as $i => $key) {
|
|
|
|
if (is_array($key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$optionKey = in_array($key, $options, true);
|
2008-08-11 16:18:19 +00:00
|
|
|
if (!$optionKey && is_string($key) && preg_match('/^[a-z(]/', $key) && (!isset($Model->{$key}) || !is_object($Model->{$key}))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$option = 'fields';
|
|
|
|
$val = array($key);
|
|
|
|
if ($key{0} == '(') {
|
|
|
|
$val = preg_split('/\s*,\s*/', substr(substr($key, 1), 0, -1));
|
|
|
|
} elseif (preg_match('/ASC|DESC$/', $key)) {
|
|
|
|
$option = 'order';
|
2011-12-16 06:52:07 +00:00
|
|
|
$val = $Model->{$name}->alias . '.' . $key;
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (preg_match('/[ =!]/', $key)) {
|
|
|
|
$option = 'conditions';
|
2011-12-16 06:52:07 +00:00
|
|
|
$val = $Model->{$name}->alias . '.' . $key;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-08-02 06:47:28 +00:00
|
|
|
$children[$option] = is_array($val) ? $val : array($val);
|
2008-05-30 11:40:08 +00:00
|
|
|
$newChildren = null;
|
|
|
|
if (!empty($name) && !empty($children[$key])) {
|
|
|
|
$newChildren = $children[$key];
|
|
|
|
}
|
|
|
|
unset($children[$key], $children[$i]);
|
|
|
|
$key = $option;
|
|
|
|
$optionKey = true;
|
|
|
|
if (!empty($newChildren)) {
|
|
|
|
$children = Set::merge($children, $newChildren);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($optionKey && isset($children[$key])) {
|
|
|
|
if (!empty($keep[$name][$key]) && is_array($keep[$name][$key])) {
|
|
|
|
$keep[$name][$key] = array_merge((isset($keep[$name][$key]) ? $keep[$name][$key] : array()), (array) $children[$key]);
|
|
|
|
} else {
|
|
|
|
$keep[$name][$key] = $children[$key];
|
|
|
|
}
|
|
|
|
unset($children[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($Model->{$name}) || !is_object($Model->{$name})) {
|
|
|
|
if ($throwErrors) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Model "%s" is not associated with model "%s"', $Model->alias, $name), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$containments = $this->containments($Model->{$name}, $children, $containments);
|
|
|
|
$depths[] = $containments['depth'] + 1;
|
|
|
|
if (!isset($keep[$name])) {
|
|
|
|
$keep[$name] = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($containments['models'][$Model->alias])) {
|
2011-12-01 07:21:31 +00:00
|
|
|
$containments['models'][$Model->alias] = array('keep' => array(), 'instance' => &$Model);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$containments['models'][$Model->alias]['keep'] = array_merge($containments['models'][$Model->alias]['keep'], $keep);
|
|
|
|
$containments['depth'] = empty($depths) ? 0 : max($depths);
|
|
|
|
return $containments;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Calculate needed fields to fetch the required bindings for the given model.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Model $Model Model
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $map Map of relations for given model
|
|
|
|
* @param mixed $fields If array, fields to initially load, if false use $Model as primary model
|
|
|
|
* @return array Fields
|
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function fieldDependencies(Model $Model, $map, $fields = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($fields === false) {
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($map as $parent => $children) {
|
|
|
|
foreach ($children as $type => $bindings) {
|
|
|
|
foreach ($bindings as $dependency) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($type == 'hasAndBelongsToMany') {
|
|
|
|
$fields[$parent][] = '--primaryKey--';
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif ($type == 'belongsTo') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$fields[$parent][] = $dependency . '.--primaryKey--';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
if (empty($map[$Model->alias])) {
|
|
|
|
return $fields;
|
|
|
|
}
|
2008-10-23 00:10:44 +00:00
|
|
|
foreach ($map[$Model->alias] as $type => $bindings) {
|
|
|
|
foreach ($bindings as $dependency) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$innerFields = array();
|
2008-10-23 00:10:44 +00:00
|
|
|
switch ($type) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'belongsTo':
|
|
|
|
$fields[] = $Model->{$type}[$dependency]['foreignKey'];
|
|
|
|
break;
|
|
|
|
case 'hasOne':
|
|
|
|
case 'hasMany':
|
|
|
|
$innerFields[] = $Model->$dependency->primaryKey;
|
|
|
|
$fields[] = $Model->primaryKey;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!empty($innerFields) && !empty($Model->{$type}[$dependency]['fields'])) {
|
|
|
|
$Model->{$type}[$dependency]['fields'] = array_unique(array_merge($Model->{$type}[$dependency]['fields'], $innerFields));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_unique($fields);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Build the map of containments
|
|
|
|
*
|
|
|
|
* @param array $containments Containments
|
|
|
|
* @return array Built containments
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function containmentsMap($containments) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$map = array();
|
|
|
|
foreach ($containments['models'] as $name => $model) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$instance = $model['instance'];
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($this->types as $type) {
|
|
|
|
foreach ($instance->{$type} as $assoc => $options) {
|
|
|
|
if (isset($model['keep'][$assoc])) {
|
|
|
|
$map[$name][$type] = isset($map[$name][$type]) ? array_merge($map[$name][$type], (array)$assoc) : (array)$assoc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
}
|