TranslateBehavior.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @package Cake.Model.Behavior
  12. * @since CakePHP(tm) v 1.2.0.4525
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('ModelBehavior', 'Model');
  16. App::uses('I18n', 'I18n');
  17. App::uses('I18nModel', 'Model');
  18. /**
  19. * Translate behavior
  20. *
  21. * @package Cake.Model.Behavior
  22. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html
  23. */
  24. class TranslateBehavior extends ModelBehavior {
  25. /**
  26. * Used for runtime configuration of model
  27. *
  28. * @var array
  29. */
  30. public $runtime = array();
  31. /**
  32. * Stores the joinTable object for generating joins.
  33. *
  34. * @var object
  35. */
  36. protected $_joinTable;
  37. /**
  38. * Stores the runtime model for generating joins.
  39. *
  40. * @var Model
  41. */
  42. protected $_runtimeModel;
  43. /**
  44. * Callback
  45. *
  46. * $config for TranslateBehavior should be
  47. * array('fields' => array('field_one',
  48. * 'field_two' => 'FieldAssoc', 'field_three'))
  49. *
  50. * With above example only one permanent hasMany will be joined (for field_two
  51. * as FieldAssoc)
  52. *
  53. * $config could be empty - and translations configured dynamically by
  54. * bindTranslation() method
  55. *
  56. * @param Model $Model Model the behavior is being attached to.
  57. * @param array $config Array of configuration information.
  58. * @return mixed
  59. */
  60. public function setup(Model $Model, $config = array()) {
  61. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  62. if (!$db->connected) {
  63. trigger_error(
  64. __d('cake_dev', 'Datasource %s for TranslateBehavior of model %s is not connected', $Model->useDbConfig, $Model->alias),
  65. E_USER_ERROR
  66. );
  67. return false;
  68. }
  69. $this->settings[$Model->alias] = array();
  70. $this->runtime[$Model->alias] = array('fields' => array());
  71. $this->translateModel($Model);
  72. return $this->bindTranslation($Model, $config, false);
  73. }
  74. /**
  75. * Cleanup Callback unbinds bound translations and deletes setting information.
  76. *
  77. * @param Model $Model Model being detached.
  78. * @return void
  79. */
  80. public function cleanup(Model $Model) {
  81. $this->unbindTranslation($Model);
  82. unset($this->settings[$Model->alias]);
  83. unset($this->runtime[$Model->alias]);
  84. }
  85. /**
  86. * beforeFind Callback
  87. *
  88. * @param Model $Model Model find is being run on.
  89. * @param array $query Array of Query parameters.
  90. * @return array Modified query
  91. */
  92. public function beforeFind(Model $Model, $query) {
  93. $this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
  94. $locale = $this->_getLocale($Model);
  95. if (empty($locale)) {
  96. return $query;
  97. }
  98. $db = $Model->getDataSource();
  99. $RuntimeModel = $this->translateModel($Model);
  100. if (!empty($RuntimeModel->tablePrefix)) {
  101. $tablePrefix = $RuntimeModel->tablePrefix;
  102. } else {
  103. $tablePrefix = $db->config['prefix'];
  104. }
  105. $joinTable = new StdClass();
  106. $joinTable->tablePrefix = $tablePrefix;
  107. $joinTable->table = $RuntimeModel->table;
  108. $joinTable->schemaName = $RuntimeModel->getDataSource()->getSchemaName();
  109. $this->_joinTable = $joinTable;
  110. $this->_runtimeModel = $RuntimeModel;
  111. if (is_string($query['fields']) && "COUNT(*) AS {$db->name('count')}" == $query['fields']) {
  112. $query['fields'] = "COUNT(DISTINCT({$db->name($Model->escapeField())})) {$db->alias}count";
  113. $query['joins'][] = array(
  114. 'type' => 'INNER',
  115. 'alias' => $RuntimeModel->alias,
  116. 'table' => $joinTable,
  117. 'conditions' => array(
  118. $Model->escapeField() => $db->identifier($RuntimeModel->escapeField('foreign_key')),
  119. $RuntimeModel->escapeField('model') => $Model->name,
  120. $RuntimeModel->escapeField('locale') => $locale
  121. )
  122. );
  123. $conditionFields = $this->_checkConditions($Model, $query);
  124. foreach ($conditionFields as $field) {
  125. $query = $this->_addJoin($Model, $query, $field, $field, $locale);
  126. }
  127. unset($this->_joinTable, $this->_runtimeModel);
  128. return $query;
  129. }
  130. $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
  131. $addFields = array();
  132. if (empty($query['fields'])) {
  133. $addFields = $fields;
  134. } elseif (is_array($query['fields'])) {
  135. foreach ($fields as $key => $value) {
  136. $field = (is_numeric($key)) ? $value : $key;
  137. if (in_array($Model->escapeField('*'), $query['fields']) || in_array($Model->alias . '.' . $field, $query['fields']) || in_array($field, $query['fields'])) {
  138. $addFields[] = $field;
  139. }
  140. }
  141. }
  142. $this->runtime[$Model->alias]['virtualFields'] = $Model->virtualFields;
  143. if ($addFields) {
  144. foreach ($addFields as $_f => $field) {
  145. $aliasField = is_numeric($_f) ? $field : $_f;
  146. foreach (array($aliasField, $Model->alias . '.' . $aliasField) as $_field) {
  147. $key = array_search($_field, (array)$query['fields']);
  148. if ($key !== false) {
  149. unset($query['fields'][$key]);
  150. }
  151. }
  152. $query = $this->_addJoin($Model, $query, $field, $aliasField, $locale);
  153. }
  154. }
  155. $this->runtime[$Model->alias]['beforeFind'] = $addFields;
  156. unset($this->_joinTable, $this->_runtimeModel);
  157. return $query;
  158. }
  159. /**
  160. * Check a query's conditions for translated fields.
  161. * Return an array of translated fields found in the conditions.
  162. *
  163. * @param Model $Model The model being read.
  164. * @param array $query The query array.
  165. * @return array The list of translated fields that are in the conditions.
  166. */
  167. protected function _checkConditions(Model $Model, $query) {
  168. $conditionFields = array();
  169. if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions'])) ) {
  170. return $conditionFields;
  171. }
  172. foreach ($query['conditions'] as $col => $val) {
  173. foreach ($this->settings[$Model->alias] as $field => $assoc) {
  174. if (is_numeric($field)) {
  175. $field = $assoc;
  176. }
  177. if (strpos($col, $field) !== false) {
  178. $conditionFields[] = $field;
  179. }
  180. }
  181. }
  182. return $conditionFields;
  183. }
  184. /**
  185. * Appends a join for translated fields.
  186. *
  187. * @param Model $Model The model being worked on.
  188. * @param object $joinTable The jointable object.
  189. * @param array $query The query array to append a join to.
  190. * @param string $field The field name being joined.
  191. * @param string $aliasField The aliased field name being joined.
  192. * @param string|array $locale The locale(s) having joins added.
  193. * @return array The modfied query
  194. */
  195. protected function _addJoin(Model $Model, $query, $field, $aliasField, $locale) {
  196. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  197. $RuntimeModel = $this->_runtimeModel;
  198. $joinTable = $this->_joinTable;
  199. $aliasVirtual = "i18n_{$field}";
  200. $alias = "I18n__{$field}";
  201. if (is_array($locale)) {
  202. foreach ($locale as $_locale) {
  203. $aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
  204. $aliasLocale = "{$alias}__{$_locale}";
  205. $Model->virtualFields[$aliasVirtualLocale] = "{$aliasLocale}.content";
  206. if (!empty($query['fields']) && is_array($query['fields'])) {
  207. $query['fields'][] = $aliasVirtualLocale;
  208. }
  209. $query['joins'][] = array(
  210. 'type' => 'LEFT',
  211. 'alias' => $aliasLocale,
  212. 'table' => $joinTable,
  213. 'conditions' => array(
  214. $Model->escapeField() => $db->identifier("{$aliasLocale}.foreign_key"),
  215. "{$aliasLocale}.model" => $Model->name,
  216. "{$aliasLocale}.{$RuntimeModel->displayField}" => $aliasField,
  217. "{$aliasLocale}.locale" => $_locale
  218. )
  219. );
  220. }
  221. } else {
  222. $Model->virtualFields[$aliasVirtual] = "{$alias}.content";
  223. if (!empty($query['fields']) && is_array($query['fields'])) {
  224. $query['fields'][] = $aliasVirtual;
  225. }
  226. $query['joins'][] = array(
  227. 'type' => 'INNER',
  228. 'alias' => $alias,
  229. 'table' => $joinTable,
  230. 'conditions' => array(
  231. "{$Model->alias}.{$Model->primaryKey}" => $db->identifier("{$alias}.foreign_key"),
  232. "{$alias}.model" => $Model->name,
  233. "{$alias}.{$RuntimeModel->displayField}" => $aliasField,
  234. "{$alias}.locale" => $locale
  235. )
  236. );
  237. }
  238. return $query;
  239. }
  240. /**
  241. * afterFind Callback
  242. *
  243. * @param Model $Model Model find was run on
  244. * @param array $results Array of model results.
  245. * @param boolean $primary Did the find originate on $model.
  246. * @return array Modified results
  247. */
  248. public function afterFind(Model $Model, $results, $primary) {
  249. $Model->virtualFields = $this->runtime[$Model->alias]['virtualFields'];
  250. $this->runtime[$Model->alias]['virtualFields'] = $this->runtime[$Model->alias]['fields'] = array();
  251. $locale = $this->_getLocale($Model);
  252. if (empty($locale) || empty($results) || empty($this->runtime[$Model->alias]['beforeFind'])) {
  253. return $results;
  254. }
  255. $beforeFind = $this->runtime[$Model->alias]['beforeFind'];
  256. foreach ($results as $key => &$row) {
  257. $results[$key][$Model->alias]['locale'] = (is_array($locale)) ? current($locale) : $locale;
  258. foreach ($beforeFind as $_f => $field) {
  259. $aliasField = is_numeric($_f) ? $field : $_f;
  260. $aliasVirtual = "i18n_{$field}";
  261. if (is_array($locale)) {
  262. foreach ($locale as $_locale) {
  263. $aliasVirtualLocale = "{$aliasVirtual}_{$_locale}";
  264. if (!isset($row[$Model->alias][$aliasField]) && !empty($row[$Model->alias][$aliasVirtualLocale])) {
  265. $row[$Model->alias][$aliasField] = $row[$Model->alias][$aliasVirtualLocale];
  266. $row[$Model->alias]['locale'] = $_locale;
  267. }
  268. unset($row[$Model->alias][$aliasVirtualLocale]);
  269. }
  270. if (!isset($row[$Model->alias][$aliasField])) {
  271. $row[$Model->alias][$aliasField] = '';
  272. }
  273. } else {
  274. $value = '';
  275. if (!empty($row[$Model->alias][$aliasVirtual])) {
  276. $value = $row[$Model->alias][$aliasVirtual];
  277. }
  278. $row[$Model->alias][$aliasField] = $value;
  279. unset($row[$Model->alias][$aliasVirtual]);
  280. }
  281. }
  282. }
  283. return $results;
  284. }
  285. /**
  286. * beforeValidate Callback
  287. *
  288. * @param Model $Model Model invalidFields was called on.
  289. * @return boolean
  290. */
  291. public function beforeValidate(Model $Model) {
  292. unset($this->runtime[$Model->alias]['beforeSave']);
  293. $this->_setRuntimeData($Model);
  294. return true;
  295. }
  296. /**
  297. * beforeSave callback.
  298. *
  299. * Copies data into the runtime property when `$options['validate']` is
  300. * disabled. Or the runtime data hasn't been set yet.
  301. *
  302. * @param Model $Model Model save was called on.
  303. * @return boolean true.
  304. */
  305. public function beforeSave(Model $Model, $options = array()) {
  306. if (isset($options['validate']) && $options['validate'] == false) {
  307. unset($this->runtime[$Model->alias]['beforeSave']);
  308. }
  309. if (isset($this->runtime[$Model->alias]['beforeSave'])) {
  310. return true;
  311. }
  312. $this->_setRuntimeData($Model);
  313. return true;
  314. }
  315. /**
  316. * Sets the runtime data.
  317. *
  318. * Used from beforeValidate() and beforeSave() for compatibility issues,
  319. * and to allow translations to be persisted even when validation
  320. * is disabled.
  321. *
  322. * @param Model $Model
  323. * @return void
  324. */
  325. protected function _setRuntimeData(Model $Model) {
  326. $locale = $this->_getLocale($Model);
  327. if (empty($locale)) {
  328. return true;
  329. }
  330. $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
  331. $tempData = array();
  332. foreach ($fields as $key => $value) {
  333. $field = (is_numeric($key)) ? $value : $key;
  334. if (isset($Model->data[$Model->alias][$field])) {
  335. $tempData[$field] = $Model->data[$Model->alias][$field];
  336. if (is_array($Model->data[$Model->alias][$field])) {
  337. if (is_string($locale) && !empty($Model->data[$Model->alias][$field][$locale])) {
  338. $Model->data[$Model->alias][$field] = $Model->data[$Model->alias][$field][$locale];
  339. } else {
  340. $values = array_values($Model->data[$Model->alias][$field]);
  341. $Model->data[$Model->alias][$field] = $values[0];
  342. }
  343. }
  344. }
  345. }
  346. $this->runtime[$Model->alias]['beforeSave'] = $tempData;
  347. }
  348. /**
  349. * afterSave Callback
  350. *
  351. * @param Model $Model Model the callback is called on
  352. * @param boolean $created Whether or not the save created a record.
  353. * @return void
  354. */
  355. public function afterSave(Model $Model, $created) {
  356. if (!isset($this->runtime[$Model->alias]['beforeValidate']) && !isset($this->runtime[$Model->alias]['beforeSave'])) {
  357. return true;
  358. }
  359. $locale = $this->_getLocale($Model);
  360. if (isset($this->runtime[$Model->alias]['beforeValidate'])) {
  361. $tempData = $this->runtime[$Model->alias]['beforeValidate'];
  362. } else {
  363. $tempData = $this->runtime[$Model->alias]['beforeSave'];
  364. }
  365. unset($this->runtime[$Model->alias]['beforeValidate'], $this->runtime[$Model->alias]['beforeSave']);
  366. $conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
  367. $RuntimeModel = $this->translateModel($Model);
  368. $fields = array_merge($this->settings[$Model->alias], $this->runtime[$Model->alias]['fields']);
  369. if ($created) {
  370. // set each field value to an empty string
  371. foreach ($fields as $key => $field) {
  372. if (!is_numeric($key)) {
  373. $field = $key;
  374. }
  375. if (!isset($tempData[$field])) {
  376. $tempData[$field] = '';
  377. }
  378. }
  379. }
  380. foreach ($tempData as $field => $value) {
  381. unset($conditions['content']);
  382. $conditions['field'] = $field;
  383. if (is_array($value)) {
  384. $conditions['locale'] = array_keys($value);
  385. } else {
  386. $conditions['locale'] = $locale;
  387. if (is_array($locale)) {
  388. $value = array($locale[0] => $value);
  389. } else {
  390. $value = array($locale => $value);
  391. }
  392. }
  393. $translations = $RuntimeModel->find('list', array('conditions' => $conditions, 'fields' => array($RuntimeModel->alias . '.locale', $RuntimeModel->alias . '.id')));
  394. foreach ($value as $_locale => $_value) {
  395. $RuntimeModel->create();
  396. $conditions['locale'] = $_locale;
  397. $conditions['content'] = $_value;
  398. if (array_key_exists($_locale, $translations)) {
  399. $RuntimeModel->save(array($RuntimeModel->alias => array_merge($conditions, array('id' => $translations[$_locale]))));
  400. } else {
  401. $RuntimeModel->save(array($RuntimeModel->alias => $conditions));
  402. }
  403. }
  404. }
  405. }
  406. /**
  407. * afterDelete Callback
  408. *
  409. * @param Model $Model Model the callback was run on.
  410. * @return void
  411. */
  412. public function afterDelete(Model $Model) {
  413. $RuntimeModel = $this->translateModel($Model);
  414. $conditions = array('model' => $Model->alias, 'foreign_key' => $Model->id);
  415. $RuntimeModel->deleteAll($conditions);
  416. }
  417. /**
  418. * Get selected locale for model
  419. *
  420. * @param Model $Model Model the locale needs to be set/get on.
  421. * @return mixed string or false
  422. */
  423. protected function _getLocale(Model $Model) {
  424. if (!isset($Model->locale) || is_null($Model->locale)) {
  425. $I18n = I18n::getInstance();
  426. $I18n->l10n->get(Configure::read('Config.language'));
  427. $Model->locale = $I18n->l10n->locale;
  428. }
  429. return $Model->locale;
  430. }
  431. /**
  432. * Get instance of model for translations.
  433. *
  434. * If the model has a translateModel property set, this will be used as the class
  435. * name to find/use. If no translateModel property is found 'I18nModel' will be used.
  436. *
  437. * @param Model $Model Model to get a translatemodel for.
  438. * @return Model
  439. */
  440. public function translateModel(Model $Model) {
  441. if (!isset($this->runtime[$Model->alias]['model'])) {
  442. if (!isset($Model->translateModel) || empty($Model->translateModel)) {
  443. $className = 'I18nModel';
  444. } else {
  445. $className = $Model->translateModel;
  446. }
  447. $this->runtime[$Model->alias]['model'] = ClassRegistry::init($className, 'Model');
  448. }
  449. if (!empty($Model->translateTable) && $Model->translateTable !== $this->runtime[$Model->alias]['model']->useTable) {
  450. $this->runtime[$Model->alias]['model']->setSource($Model->translateTable);
  451. } elseif (empty($Model->translateTable) && empty($Model->translateModel)) {
  452. $this->runtime[$Model->alias]['model']->setSource('i18n');
  453. }
  454. return $this->runtime[$Model->alias]['model'];
  455. }
  456. /**
  457. * Bind translation for fields, optionally with hasMany association for
  458. * fake field.
  459. *
  460. * *Note* You should avoid binding translations that overlap existing model properties.
  461. * This can cause un-expected and un-desirable behavior.
  462. *
  463. * @param Model $Model instance of model
  464. * @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
  465. * @param boolean $reset Leave true to have the fields only modified for the next operation.
  466. * if false the field will be added for all future queries.
  467. * @return boolean
  468. * @throws CakeException when attempting to bind a translating called name. This is not allowed
  469. * as it shadows Model::$name.
  470. */
  471. public function bindTranslation(Model $Model, $fields, $reset = true) {
  472. if (is_string($fields)) {
  473. $fields = array($fields);
  474. }
  475. $associations = array();
  476. $RuntimeModel = $this->translateModel($Model);
  477. $default = array('className' => $RuntimeModel->alias, 'foreignKey' => 'foreign_key');
  478. foreach ($fields as $key => $value) {
  479. if (is_numeric($key)) {
  480. $field = $value;
  481. $association = null;
  482. } else {
  483. $field = $key;
  484. $association = $value;
  485. }
  486. if ($association === 'name') {
  487. throw new CakeException(
  488. __d('cake_dev', 'You cannot bind a translation named "name".')
  489. );
  490. }
  491. $this->_removeField($Model, $field);
  492. if (is_null($association)) {
  493. if ($reset) {
  494. $this->runtime[$Model->alias]['fields'][] = $field;
  495. } else {
  496. $this->settings[$Model->alias][] = $field;
  497. }
  498. } else {
  499. if ($reset) {
  500. $this->runtime[$Model->alias]['fields'][$field] = $association;
  501. } else {
  502. $this->settings[$Model->alias][$field] = $association;
  503. }
  504. foreach (array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany') as $type) {
  505. if (isset($Model->{$type}[$association]) || isset($Model->__backAssociation[$type][$association])) {
  506. trigger_error(
  507. __d('cake_dev', 'Association %s is already bound to model %s', $association, $Model->alias),
  508. E_USER_ERROR
  509. );
  510. return false;
  511. }
  512. }
  513. $associations[$association] = array_merge($default, array('conditions' => array(
  514. 'model' => $Model->alias,
  515. $RuntimeModel->displayField => $field
  516. )));
  517. }
  518. }
  519. if (!empty($associations)) {
  520. $Model->bindModel(array('hasMany' => $associations), $reset);
  521. }
  522. return true;
  523. }
  524. /**
  525. * Update runtime setting for a given field.
  526. *
  527. * @param string $field The field to update.
  528. */
  529. protected function _removeField(Model $Model, $field) {
  530. if (array_key_exists($field, $this->settings[$Model->alias])) {
  531. unset($this->settings[$Model->alias][$field]);
  532. } elseif (in_array($field, $this->settings[$Model->alias])) {
  533. $this->settings[$Model->alias] = array_merge(array_diff($this->settings[$Model->alias], array($field)));
  534. }
  535. if (array_key_exists($field, $this->runtime[$Model->alias]['fields'])) {
  536. unset($this->runtime[$Model->alias]['fields'][$field]);
  537. } elseif (in_array($field, $this->runtime[$Model->alias]['fields'])) {
  538. $this->runtime[$Model->alias]['fields'] = array_merge(array_diff($this->runtime[$Model->alias]['fields'], array($field)));
  539. }
  540. }
  541. /**
  542. * Unbind translation for fields, optionally unbinds hasMany association for
  543. * fake field
  544. *
  545. * @param Model $Model instance of model
  546. * @param string|array $fields string with field, or array(field1, field2=>AssocName, field3), or null for
  547. * unbind all original translations
  548. * @return boolean
  549. */
  550. public function unbindTranslation(Model $Model, $fields = null) {
  551. if (empty($fields) && empty($this->settings[$Model->alias])) {
  552. return false;
  553. }
  554. if (empty($fields)) {
  555. return $this->unbindTranslation($Model, $this->settings[$Model->alias]);
  556. }
  557. if (is_string($fields)) {
  558. $fields = array($fields);
  559. }
  560. $RuntimeModel = $this->translateModel($Model);
  561. $associations = array();
  562. foreach ($fields as $key => $value) {
  563. if (is_numeric($key)) {
  564. $field = $value;
  565. $association = null;
  566. } else {
  567. $field = $key;
  568. $association = $value;
  569. }
  570. $this->_removeField($Model, $field);
  571. if (!is_null($association) && (isset($Model->hasMany[$association]) || isset($Model->__backAssociation['hasMany'][$association]))) {
  572. $associations[] = $association;
  573. }
  574. }
  575. if (!empty($associations)) {
  576. $Model->unbindModel(array('hasMany' => $associations), false);
  577. }
  578. return true;
  579. }
  580. }