9951 explained code solutions for 126 technologies


php-symfonyHow to create a model in PHP Symfony?


Creating a model in PHP Symfony is a simple process.

  1. Create a new class in the src/Entity directory:
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\MyModelRepository")
 */
class MyModel
{
    // ...
}
  1. Add the necessary fields and annotations to the class:
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\MyModelRepository")
 */
class MyModel
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    // ...
}
  1. Generate the getters and setters for the fields:
php bin/console make:entity --regenerate
  1. Generate the database table for the model:
php bin/console doctrine:schema:update --force
  1. Use the model in your application.

Helpful links

Edit this code on GitHub