Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Last active October 2, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinglozzer/56736aa1de81171dc79a to your computer and use it in GitHub Desktop.
Save kinglozzer/56736aa1de81171dc79a to your computer and use it in GitHub Desktop.
Injector:
ManyManyList:
class: StrangersClub\ManyManyList
<?php
namespace StrangersClub;
use DataList;
use ManyManyList as SilverStripeManyManyList;
class ManyManyList extends \ManyManyList {
/**
* @param mixed $item
* @param null $extraFields
* @throws \Exception
*/
public function add($item, $extraFields = array()) {
$foreignIDs = (array)$this->getForeignID();
$joinTable = $this->getJoinTable();
// Ensure we have an object to work with
if (is_numeric($item)) {
$item = DataList::create($this->dataClass)->byID($item);
}
if ($item instanceof $this->dataClass) {
$item->extend('onBeforeManyManyRelationAdd', $foreignIDs, $extraFields, $joinTable);
}
parent::add($item, $extraFields);
if ($item instanceof $this->dataClass) {
$item->extend('onAfterManyManyRelationAdd', $foreignIDs, $extraFields, $joinTable);
}
}
/**
* @param int $itemID
* @throws \Exception
*/
public function removeByID($itemID) {
parent::removeByID($itemID);
$item = DataList::create($this->dataClass)->byID($itemID);
// Just in case we're given an invalid ID
if (!$item instanceof $this->dataClass) {
return;
}
$foreignIDs = (array)$this->getForeignID();
$joinTable = $this->getJoinTable();
$item->extend('onAfterManyManyRelationRemove', $foreignIDs, $joinTable);
}
}
<?php
class MemberExtension extends Extension {
private static $belongs_many_many = array(
'BookedEvents' => 'Event'
);
/**
* @var array
*/
protected static $join_table_relation_mapping = array();
/**
* ManyManyList doesn't keep track of the relation name being edited, only the join tables/fields,
* so this is used to build a mapping from join table -> relation name
*
* @return array
*/
protected static function get_join_table_relation_mapping() {
if (empty(self::$join_table_relation_mapping)) {
$obj = singleton('Member');
$manyMany = $obj->manyMany();
foreach ($manyMany as $relation => $targetClass) {
list($dud, $dud, $dud, $dud, $joinTable) = $obj->manyManyComponent($relation);
self::$join_table_relation_mapping[$joinTable] = $relation;
}
}
return self::$join_table_relation_mapping;
}
/**
* @param array $foreignIDs
* @param array &$extraFields
* @param string $joinTable
*/
public function onBeforeManyManyRelationAdd(array $foreignIDs, &$extraFields, $joinTable) {
// Map the join table to a human-readable relation name
$tableRelationMapping = $this->get_join_table_relation_mapping();
if (!isset($tableRelationMapping[$joinTable])) {
return;
}
// If this is new event being added to/removed from the 'BookedEvents' relation...
if ($tableRelationMapping[$joinTable] === 'BookedEvents') {
// Ensure the payment date is set
if (!isset($extraFields['PaidDate']) || !$extraFields['PaidDate']) {
$extraFields['PaidDate'] = SS_Datetime::now()->value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment