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
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?