Sök…
Att förklara en Symfony-enhet som YAML
- AppBundle / enhet / Person.php
<?php
namespace AppBundle\Entity;
/**
* Person
*/
class Person
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var int
*/
private $age;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Person
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set age
*
* @param integer $age
*
* @return Person
*/
public function setAge($age)
{
$this->age = $age;
return $this;
}
/**
* Get age
*
* @return int
*/
public function getAge()
{
return $this->age;
}
}
- AppBundle / Resources / config / doktrin / Person.orm.yml
AppBundle\Entity\Person:
type: entity
repositoryClass: AppBundle\Repository\PersonRepository
table: persons
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 20
nullable: false
column: Name
unique: true
age:
type: integer
nullable: false
column: Age
unique: false
lifecycleCallbacks: { }
- Skapa tabellen
php bin/console doctrine:schema:update --force
Eller använd det rekommenderade sättet (förutsatt att du redan har doktrin-migrations-paket installerat i ditt projekt):
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Skapa enheten automatiskt från YAML
php bin / konsol doktrin: generera: enheter AppBundle
Förklara en Symfony-enhet med kommentarer
- AppBundle / enhet / Person.php
<?php
namespace AppBundle\Entity;
use DoctrineORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @ORM\Table(name="persons")
* @ORM\Entity(repositoryClass="AppBundle\Entity\PersonRepository")
* @UniqueEntity("name")
*/
class Person
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", name="name", length=20, nullable=false)
*/
protected $name;
/**
* @ORM\Column(type="integer", name="age", nullable=false)
*/
protected $age;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getAge()
{
return $this->age;
}
public function setAge($age)
{
$this->age = $age;
return $this;
}
}
- Generera enheten
php bin/console doctrine:generate:entities AppBundle/Person
- För att dumpa SQL-satserna på skärmen
php bin/console doctrine:schema:update --dump-sql
- Skapa tabellen
php bin/console doctrine:schema:update --force
Eller använd det rekommenderade sättet (förutsatt att du redan har doktrin-migrations-paket installerat i ditt projekt):
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow