On line guide/help
You are on page 5 of the online help. Navigate sequentially through the guide pages or search for a topic of interest to you.
Index Prev. NextMaster-Details report in Symfony
Build a master-details report using the Symfony framework
A Master/Details report is, for example, a sales order or an invoice. In this type of document, each report page contains data related to a single header or footer (1 master record row) and a series of detail data rows (detail record rows).
Using the library's data providers, you can define the data source to use as the master and details. The report will automatically loop through the possible values ββto generate the report using the data and will end when it has processed all of them.
In the specific case of Symfony, as an example, let's generate the sales order report, which you can also find in the reserved area. The example in the members' area uses PHP's standard MySQLi connector DataProviderMySQLi. Symfony interfaces with databases using the Doctrine ORM, so we'll use the DataProviderDoctine connector.
Creating a new Symfony Web Application with a custom controller button
Phase 1: Project Setup & MySQL Configuration
Step-by-Step Guide. As a prerequisite, before starting, ensure you have the required software installed on your system. These are essential for creating and managing a Symfony project.
- PHP (version 8.1 or later)
- Composer
- Symfony CLI
Open your terminal and run the following commands to create a new Symfony project using the webapp skeleton and navigate into the newly created directory.
symfony new my_symf_app --webapp
Change directory:
cd my_symf_app
Install the report library:
composer require alienproject/pdfreport
Install the necessary Doctrine and MySQL packages (Database Dependencies). These bundles provide the tools to interact with your database through Symfony's ORM.
composer require doctrine/orm symfony/framework-bundle
composer require symfony/orm-pack
Database Configuration : Modify the .env file in your project's root. Find the `DATABASE_URL` line and update it with your MySQL credentials (replace db_user:db_password). Replace also "db_name" with the existing database name. At the bottom of this page you will find the link to download the example SQL file needed to generate the tables and views with the test data in your MySQL database.
# .env
# Replace with your test MySQL credentials
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=8.0.32&charset=utf8mb4"
Phase 2: Controller and View Creation
Create ReportController : First, generate a new controller using the maker bundle. When prompted, name it ReportController.
php bin/console make:controller
Next, open the generated file src/Controller/ReportController.php and replace its content with the following code. This sets up the main page and the custom build method. Replace the code with the one below.
Class ReportController
Create the View. Create a new file at templates/report/index.html.twig. This template will render the main page with a button to trigger the build method.
View index.html.twig
Be sure to copy the order.xml template file in the public folder. The template file is uploaded to the public folder only because it's sample code. When developing a production application, template files should be managed more securely by placing them in a different folder or loading them into a Text field in the database.
XML order template file (order.xml)
Finally, start the Symfony development server. Access your application by navigating to /report on the provided local URL (eg. http://localhost/my_symf_app/public/report).
Optionally, edit my_symf_app/public/.htaccess if the requested page is not displayed, for example if "my_symf_app" is not located in the root of the web server but is in a subfolder
File public/.htaccess