Skip to content

Commit

Permalink
Merge pull request codeigniter4#7596 from kenjis/docs-db-session
Browse files Browse the repository at this point in the history
docs: improve "Configure DatabaseHandler" for Session
  • Loading branch information
kenjis committed Jun 20, 2023
2 parents 9d9f19e + 963526a commit 52a2372
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 23 additions & 2 deletions user_guide_src/source/libraries/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ However, there are some conditions that must be met:
Configure DatabaseHandler
-------------------------

Setting Table Name
^^^^^^^^^^^^^^^^^^

In order to use the 'DatabaseHandler' session driver, you must also create this
table that we already mentioned and then set it as your
``$savePath`` value.
Expand All @@ -567,6 +570,9 @@ you would do this:

.. literalinclude:: sessions/039.php

Creating Database Table
^^^^^^^^^^^^^^^^^^^^^^^

And then of course, create the database table ...

For MySQL::
Expand Down Expand Up @@ -594,6 +600,9 @@ For PostgreSQL::
and the session ID and a delimiter. It should be increased as needed, for example,
when using long session IDs.

Adding Primary Key
^^^^^^^^^^^^^^^^^^

You will also need to add a PRIMARY KEY **depending on your $matchIP
setting**. The examples below work both on MySQL and PostgreSQL::

Expand All @@ -606,11 +615,23 @@ setting**. The examples below work both on MySQL and PostgreSQL::
// To drop a previously created primary key (use when changing the setting)
ALTER TABLE ci_sessions DROP PRIMARY KEY;

You can choose the Database group to use by adding a new line to the
**app/Config/Session.php** file with the name of the group to use:
.. important:: If you don't add the correct primary key, the following error
may occur::

Uncaught mysqli_sql_exception: Duplicate entry 'ci_session:***' for key 'ci_sessions.PRIMARY'

Changing Database Group
^^^^^^^^^^^^^^^^^^^^^^^

The default database group is used by default.
You can change the database group to use by changing the ``$DBGroup`` property
in the **app/Config/Session.php** file to the name of the group to use:

.. literalinclude:: sessions/040.php

Setting Up Database Table with Command
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you'd rather not do all of this by hand, you can use the ``make:migration --session`` command
from the cli to generate a migration file for you::

Expand Down
2 changes: 0 additions & 2 deletions user_guide_src/source/libraries/sessions/040.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ class Session extends BaseConfig
{
// ...
public ?string $DBGroup = 'groupName';

// ...
}

0 comments on commit 52a2372

Please sign in to comment.