creation_date datetime NOT NULL,
parent_group int(10) DEFAULT NULL,
PRIMARY KEY (id_group),
- UNIQUE KEY `name` (group_name),
FOREIGN KEY (parent_group) REFERENCES galette_groups (id_group) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP TABLE IF EXISTS galette_groups CASCADE;
CREATE TABLE galette_groups (
id_group integer DEFAULT nextval('galette_groups_id_seq'::text) NOT NULL,
- group_name character varying(250) NOT NULL CONSTRAINT name UNIQUE,
+ group_name character varying(250) NOT NULL,
creation_date timestamp NOT NULL,
parent_group integer DEFAULT NULL REFERENCES galette_groups(id_group) ON DELETE RESTRICT ON UPDATE CASCADE,
PRIMARY KEY (id_group)
ALTER TABLE galette_searches DROP INDEX form;
ALTER TABLE galette_searches DROP COLUMN parameters_sum;
+-- drop groups unique name constraint
+ALTER TABLE galette_groups DROP INDEX `name`;
+
UPDATE galette_database SET version = 0.960;
\ No newline at end of file
DROP INDEX galette_searches_idx;
ALTER TABLE galette_searches DROP COLUMN parameters_sum;
+-- drop groups unique name constraint
+ALTER TABLE galette_groups DROP CONSTRAINT name;
+
UPDATE galette_database SET version = 0.960;
\ No newline at end of file
namespace Galette\Entity;
+use Galette\Repository\Groups;
use Throwable;
use Galette\Core\Login;
use Analog\Analog;
{
global $zdb, $hist;
+ $parent_group = null;
+ if ($this->parent_group) {
+ $parent_group = $this->parent_group->getId();
+ }
+ if (!Groups::isUnique($zdb, $this->getName(), $parent_group, $this->getId())) {
+ Analog::log(
+ 'Group name is not unique at requested level',
+ Analog::WARNING
+ );
+ throw new \RuntimeException(
+ _T("The group name you have requested already exists in the database.")
+ );
+ }
+
try {
$values = array(
self::PK => $this->id,
);
if ($this->parent_group) {
- $values['parent_group'] = $this->parent_group->getId();
+ $values['parent_group'] = $parent_group;
}
if (!isset($this->id) || $this->id == '') {
/**
* Check if groupname is unique
*
- * @param Db $zdb Database instance
- * @param string $name Requested name
+ * @param Db $zdb Database instance
+ * @param string $name Requested name
+ * @param int|null $parent Parent groupe (defaults to null)
+ * @param int|null $current Current ID to be excluded (defaults to null)
*
* @return boolean
*/
- public static function isUnique(Db $zdb, $name)
+ public static function isUnique(Db $zdb, string $name, int $parent = null, int $current = null)
{
try {
$select = $zdb->select(Group::TABLE);
- $select->columns(
- array('group_name')
- )->where(array('group_name' => $name));
+ $select->columns(['group_name'])
+ ->where(['group_name' => $name]);
+
+ if ($parent === null) {
+ $select->where('parent_group IS NULL');
+ } else {
+ $select->where(['parent_group' => $parent]);
+ }
+
+ if ($current !== null) {
+ $select->where->notEqualTo(Group::PK, $current);
+ }
+
$results = $zdb->execute($select);
return !($results->count() > 0);
} catch (Throwable $e) {
$this->boolean($group->store())->isTrue();
$group_id = $group->getId();
+ //update without changes should be ok
+ $group = new \Galette\Entity\Group($group_id);
+ $this->boolean($group->store())->isTrue();
+
//Adding another group with same name throws an exception
$group = new \Galette\Entity\Group();
$group->setLogin($this->login);
$group->setName('A group');
$this->boolean($group->store())->isFalse();
}
- )->hasMessage('Duplicate entry');
+ )->hasMessage('The group name you have requested already exists in the database.');
+
+ //update with changes should be ok
+ $group = new \Galette\Entity\Group($group_id);
+ $group->setName('A group - edited');
+ $this->boolean($group->store())->isTrue();
+
+ //
+ $group = new \Galette\Entity\Group();
+ $group->setName('Unique one');
+ $this->boolean($group->store())->isTrue();
+ //editing using an existing name is not ok
+ $this
+ ->exception(
+ function () use ($group) {
+ $group->setName('A group - edited');
+ $this->boolean($group->store())->isFalse();
+ }
+ )->hasMessage('The group name you have requested already exists in the database.');
}
/**
$children_list = $groups->getList(true, $europe);
$this->array($children_list)->hasSize(4);
}
+
+ /**
+ * Test group name unicity
+ *
+ * @return void
+ */
+ public function testUnicity()
+ {
+ $group = new \Galette\Entity\Group();
+ $group->setLogin($this->login);
+ $unique_name = 'One group to rule them all';
+ $group->setName($unique_name);
+ $this->boolean($group->store())->isTrue();
+ $group_id = $group->getId();
+
+ $select = $this->zdb->select(\Galette\Entity\Group::TABLE);
+ $select->where(['group_name' => 'Europe']);
+ $result = $this->zdb->execute($select)->current();
+ $europe = $result->{\Galette\Entity\Group::PK};
+
+ $select = $this->zdb->select(\Galette\Entity\Group::TABLE);
+ $select->where(['group_name' => 'France']);
+ $result = $this->zdb->execute($select)->current();
+ $france = $result->{\Galette\Entity\Group::PK};
+
+ //name already exists - not unique
+ $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, $unique_name))->isFalse();
+ //name does not exist on another level - unique
+ $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, $unique_name, $europe))->isTrue();
+ //name is the current one - unique
+ $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, $unique_name, null, $group_id))->isTrue();
+
+ //tests on another level
+ $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, 'Nord', $france))->isFalse();
+ $this->boolean(\Galette\Repository\Groups::isUnique($this->zdb, 'Creuse', $france))->isTrue();
+ }
}