# Add Related Posts to a Symfony Blog

Yes. An agent adds a repository query that ranks posts by shared tags, a Twig block under each post, translations and a PHPUnit test, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-related-posts/symfony/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from the data model, because related posts are a query before they are a template. In a blog like symfony/demo, `composer.json` requires PHP 8.4, `symfony/framework-bundle` 8, `doctrine/orm` 3.5 and `twig/extra-bundle`, with PHPUnit 11.5, PHPStan at level 6 in `phpstan.dist.neon` and PHP-CS-Fixer in `require-dev`. `src/Entity/Post.php` has a `ManyToMany` to `Tag` through the `symfony_demo_post_tag` join table, ordered by name and capped at four tags by an `Assert\Count`, a `publishedAt` `DatePoint`, and `addTag`, `removeTag` and `getTags`. `src/Repository/PostRepository.php` has `findLatest`, which joins author and tags, filters `publishedAt <= :now` and takes an optional tag through `MEMBER OF`, and `findBySearchQuery`, which builds `LIKE` clauses per term; there is no method that relates one post to others.

The agent adds `findRelated(Post $post, int $limit)` to `PostRepository`: a query builder that joins `p.tags`, keeps posts other than the given one whose tags are in the post's own tag ids, filters `publishedAt <= :now` as `findLatest` does so a scheduled post never leaks, groups by post, orders by `COUNT(t.id)` descending and then `publishedAt` descending, and returns at most the limit. A post with no tags returns an empty list rather than falling back to recent posts, so the block says nothing when there is nothing related. The method gets the same docblock style and return type the file already uses, which PHPStan at level 6 checks.

On the page, `BlogController::postShow` renders `blog/post_show.html.twig` with only the post, so the agent passes `relatedPosts` from the repository alongside it. The template gains a section after the comments, built with the existing `blog/_post.html.twig` partial so each related entry shows the same title link, date, author and tags as the index, under a heading that uses a new `post.related_posts` key. That key is added to `translations/messages+intl-icu.en.xlf`, where `post.num_comments` and `post.no_comments` already live, and the agent runs `bin/console lint:translations` and `lint:xliff translations` as `lint.yaml` does, because a key present in English but missing from the other thirty files is reported there.

Tests follow `tests/Controller/BlogControllerTest.php`, a `WebTestCase` where `testIndex` counts `article.post` elements against `Paginator::PAGE_SIZE` and `testNewComment` logs in `jane_admin` from the fixtures. The agent adds a test that opens the first post from `/en/blog/`, asserts the related section lists at most the configured number of posts, none with the current slug, and each sharing a tag with it, using the fixtures in `src/DataFixtures/AppFixtures.php`, which give every post between two and four random tags. It runs `vendor/bin/phpunit`, `vendor/bin/phpstan analyze`, `vendor/bin/php-cs-fixer check --diff`, `bin/console lint:twig templates --env=prod` and `bin/console doctrine:schema:validate --skip-sync`, and starts `php -S localhost:8000 -t public` to load a post page and keep its HTML.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "Add related posts to this Symfony blog and open a pull request. Read composer.json, src/Entity/Post.php, src/Entity/Tag.php, src/Repository/PostRepository.php, src/Controller/BlogController.php, templates/blog/post_show.html.twig, templates/blog/_post.html.twig, translations/messages+intl-icu.en.xlf, src/DataFixtures/AppFixtures.php, tests/Controller/BlogControllerTest.php, phpstan.dist.neon and .github/workflows/lint.yaml first. Add findRelated(Post $post, int $limit = 3) to PostRepository: join tags, exclude the post itself, keep posts sharing at least one tag id with it, apply the same publishedAt filter findLatest uses, order by the count of shared tags and then publishedAt, and return at most $limit posts; a post with no tags returns an empty array. Pass the result to blog/post_show.html.twig from BlogController::postShow and render a section after the comments that reuses blog/_post.html.twig under a heading translated with a new post.related_posts key added to the English translation file. Add a test to BlogControllerTest that opens the first post, asserts at most three related posts, none with the current slug, and each sharing a tag with it. Run vendor/bin/phpunit, vendor/bin/phpstan analyze, vendor/bin/php-cs-fixer check --diff, bin/console lint:twig templates --env=prod, bin/console lint:xliff translations, bin/console lint:translations and bin/console doctrine:schema:validate --skip-sync, and paste the output in the pull request. Do not change the entities, the admin controllers or the search.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request with `findRelated` in `src/Repository/PostRepository.php`, one extra argument to `render` in `BlogController::postShow`, a related section in `templates/blog/post_show.html.twig` that includes `_post.html.twig`, the `post.related_posts` key in the English translation file, and a new test in `tests/Controller/BlogControllerTest.php`. The description pastes the PHPUnit summary, the PHPStan result, the PHP-CS-Fixer check and the Twig, XLIFF, translation and schema lints, and shows the related section as rendered for one post from the fixtures.

The repository's `tests.yaml` workflow runs PHPUnit on PHP 8.4 and 8.5 across Linux, macOS and Windows, and `lint.yaml` runs PHP-CS-Fixer, the console lints and PHPStan on the pull request, so every check the agent ran locally runs again on GitHub.

A reviewer checks that the query excludes the current post and unpublished posts, that the ordering puts posts sharing more tags first, that the section is absent for a post with no tags, and that the translation key follows the naming of its neighbours. No entity or migration changes, since the join table already holds everything the query needs.

## Run it on every blog feature ticket

A team that runs several Symfony sites on this layout starts one run per repository with `POST /o/{org}/sessions`, this prompt and the repository in `resources`. The agent reads each site's own entity and repository, so a blog that relates posts by category instead of tags gets a query on that relation, and each run opens its own pull request with its own lint output.

The key's `webhook_url` receives `run.completed` for each run it starts, with `data.session_url` for the pull request link.

## Why run it on Rush?

The query is the part that goes wrong quietly: a join that returns duplicates, an unpublished post that appears early, a sort that ignores how many tags overlap. In a Rush sandbox the agent runs the test suite against the fixtures, runs PHPStan at the project's level and every console lint the repository's workflow runs, and loads a post page in a real server, so the pull request shows the section rendered rather than described.

The same request works for every Symfony blog the team maintains, each run in its own sandbox, and the reviewer reads one query and one template against output they can see.

## Frequently asked questions

### How are related posts chosen?

By tags they share with the current post, through the existing `symfony_demo_post_tag` join table. Posts sharing more tags rank first, then newer posts, and only posts whose `publishedAt` has passed are considered, the same filter `findLatest` applies on the index.

### Does this need a migration?

No. `Post` already has the `ManyToMany` to `Tag`, so `findRelated` is a read query on tables that exist. The agent runs `doctrine:schema:validate --skip-sync` to show the mapping is unchanged.

### What happens for a post with no tags?

The query returns nothing and the template renders no section, because an empty block with a heading reads as a bug. A later change can fall back to the latest posts if the product wants a section on every page.

### Will the other languages break?

The new key goes into the English file, and `bin/console lint:translations` reports it as missing elsewhere so translators can add it. Until then Symfony falls back to the English text, and the run's output lists exactly which files lack the key.

## More Symfony jobs

- [Add Request ID Logging to a Symfony App](https://getrush.ai/run/add-request-id-logging/symfony/)
- [Add Security Headers to a Symfony App](https://getrush.ai/run/add-security-headers/symfony/)
- [Add an XML Sitemap to a Symfony App](https://getrush.ai/run/add-sitemap/symfony/)

## The same job on other stacks

- [Add Related Posts to a Wagtail Blog from a Linear Issue](https://getrush.ai/run/add-related-posts/wagtail/)
- [Add Related Posts to a Zola Blog](https://getrush.ai/run/add-related-posts/zola/)
- [Add Related Posts to an Astro Blog](https://getrush.ai/run/add-related-posts/astro/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
