1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-01 05:55:02 +03:00
ifhub.club/engine/classes/ManyToManyRelation.class.php

60 lines
1.7 KiB
PHP
Raw Normal View History

<?php
/**
* Класс. представляющий собой обёертку для связей MANY_TO_MANY.
* Позволяет оперровать коллекцией загруженных по связи эдементов через имя связи
* Например, $oTopic->Tags->add($oTag) или $oTopic->Tags->delete($oTag->getId()) при
* наличии настроенной MANY_TO_MANY связи 'tags'
*/
class LS_ManyToManyRelation
{
// Ссылка на $oEntityORM->aRelationsData[<relation_name>],
// где relation_name - имя сязи, которую представляет объект
protected $_aCollection = array();
2011-05-31 16:04:05 +03:00
protected $bUpdated = false;
2011-05-31 16:04:05 +03:00
public function __construct($aCollection)
{
2011-05-31 16:04:05 +03:00
$this->_aCollection = $aCollection;
}
/**
* Добавление объекта в коллекцию
* @param <type> $oEntity
*/
public function add($oEntity)
{
2011-05-31 16:04:05 +03:00
$this->bUpdated = true;
$this->_aCollection[$oEntity->_getPrimaryKeyValue()] = $oEntity;
}
/**
* Удаление объекта из коллекции по его id или массиву id
* @param <type> $iId
*/
public function delete($iId)
{
2011-05-31 16:04:05 +03:00
$this->bUpdated = true;
if (is_array($iId)) {
foreach ($iId as $id) {
if (isset($this->_aCollection[$id])) {
unset($this->_aCollection[$id]);
}
}
} elseif (isset($this->_aCollection[$iId])) {
unset($this->_aCollection[$iId]);
}
}
2011-05-31 16:04:05 +03:00
public function getCollection()
{
return $this->_aCollection;
}
public function isUpdated()
{
return $this->bUpdated;
}
}
?>