Quantcast
Channel: Joomla 4 - Check if Alias is Unique - Joomla Stack Exchange
Viewing all articles
Browse latest Browse all 2

Joomla 4 - Check if Alias is Unique

$
0
0

I'm trying to develop a Joomla 4 component and I almost completed it, but I have issues to save an item when the Alias is not Unique.

In the database, I've set up this:

CREATE UNIQUE INDEX aliasindex ON rwl35_rwlevents (alias);In the Model (administrator/components/com_rwlevents/src/Model), I have this:

protected function prepareTable($table){    $date = Factory::getDate()->toSql();    $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);    $table->check();    $table->generateAlias();}

Then, in the EventTable.php file, (/administrator/components/com_rwlevents/src/Table)

I have this code:

public function check(){    try    {        parent::check();    }    catch (\Exception $e)    {        $this->setError($e->getMessage());        return false;    }    // Check the publish down date is not earlier than publish up.    if ($this->publish_down > $this->_db->getNullDate() && $this->publish_down < $this->publish_up) {        $this->setError(Text::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));        return false;    }    // Set publish_up, publish_down to null if not set    if (!$this->publish_up) {        $this->publish_up = null;    }    if (!$this->publish_down) {        $this->publish_down = null;    }    $this->alias = trim($this->alias);    if (empty($this->alias))    {        $this->alias = $this->name;    }    $this->alias = JFilterOutput::stringURLSafe($this->alias);    return true;}

Then, In the same file, I have a function to generate the Alias:

public function generateAlias(){    if (empty($this->alias))    {        $this->alias = $this->name;    }    $this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);    if (trim(str_replace('-', '', $this->alias)) == '')    {        $this->alias = Factory::getDate()->format('Y-m-d-H-i-s');    }    return $this->alias;}

But in the Admin, when I try to save an item with an Alias that already exists in the Database, I have:

Duplicate entry 'concert-au-3-arena' for key 'aliasindex'

How can I code this to say: "if this Alias already exists, then, do this..."?


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>