CSM tutorial issues with tags

Hi,
Using PHP v7.3.7, running under Cygwin/Windows, with the WAMPStack bundle (Apache, MySql, PHP), and using the latest and greatest CakePHP.

I’m working through the CMS tutorial at CMS Tutorial - Tags and Users - 4.x

Under the code block to add the tag control to the web page, the tutorial says “You should now create a couple new articles that have tags…” Sure, great. Except… the tags control is a read-only select list, which I can’t modify. Here’s the generated HTML:

<select name="tags[_ids][]" multiple="multiple" id="tags-ids"></select>

So being the person I am, I opened the MySQL console and manually typed in SQL statements to add three new tags, and to link them to article 1:

mysql> select * from tags;
+----+-----------+---------------------+---------------------+
| id | title     | created             | modified            |
+----+-----------+---------------------+---------------------+
|  1 | tag one   | 2021-07-03 23:38:11 | 2021-07-03 23:38:11 |
|  2 | tag two   | 2021-07-03 23:38:16 | 2021-07-03 23:38:16 |
|  3 | tag three | 2021-07-03 23:38:21 | 2021-07-03 23:38:21 |
+----+-----------+---------------------+---------------------+
3 rows in set (0.00 sec)

mysql> select * from articles_tags;
+------------+--------+
| article_id | tag_id |
+------------+--------+
|          1 |      1 |
|          1 |      2 |
|          1 |      3 |
+------------+--------+
3 rows in set (0.00 sec)

Cool, now when I open the first article they show up. But they also show up in the second article:

Uh, pretty sure that’s not supposed to happen :slight_smile:

So, it looks like there are two problems to deal with right now. First, as the tutorial sits, it is not possible to add tags to an article except by manual injection.

Second, there is a problem with the associations between the Articles table and the Tags table.

Extract from ArticlesTable.php:

class ArticlesTable extends Table
{
    public function initialize(array $config): void
    {
        $this->addBehavior('Timestamp');
        $this->belongsToMany('Tags'); // Sets up one-to-many
    }

Extract from TagsTable.php:

class TagsTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('tags');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsToMany('Articles', [
            'foreignKey' => 'tag_id',
            'targetForeignKey' => 'article_id',
            'joinTable' => 'articles_tags',
        ]);
    }

Per the tutorial I set up ArticlesTable manually, and TagsTable using the bake all tags command.

Any ideas why the tags show up on both articles? Is there a problem with the associations?

not sure, if I get you right.

you are in the “edit” action. there you have a list of all existing tags which you can associate to the current article.

if you go to the “view” action you should see which tags are associated to the article.

No, that’s not the problem. The problem is, there’s no way to add tags through the application. The tutorial instructs you to create a couple of articles that have tags. As there are no predefined tags, that implies that you should be able to add new tags when adding and editing articles.

From your comment, I now realize that the list of tags should be predefined, and you are supposed to select existing tags to apply them to articles.

I think the solution is to create SQL to insert the predefined tags (funny, cats, gifs) into the tags table, and to clarify how the page is intended to work. I’m going to proceed with the rest of the tutorial with the predefined tags. If that works, I’ll raise a PR to update the tutorial.

Is there no Tags controller with add and edit and delete functionality?

Ah, yes, of course. There is, but I’m completely new to CakePHP so it didn’t occur to me to visit localhost:8765/tags to create tags.

And I completely missed the line in the tutorial that told me to go to /tags/add to create a few tags. Doh!

1 Like