Creare entitati - Tutorial Symfony
Postat la Thu 28 May 2015 in tutoriale, symfony 2
Entitatea - entity - e o clasa in care sunt descrise campurile unei tabele si include metode de setare si afisare campuri. Mai tarziu voi mentiona si includerea aici a regulilor de validare a datelor ce se introduc.
O entitate poate fi generata prin 2 metode: manual sau cu utilitarul app/console:
php app/console doctrine:generate:entity
The Entity shortcut name: TutorialTestBundle:Carti
Determine the format to use for the mapping information.
Configuration format (yml, xml, php, or annotation) [annotation]:
Instead of starting with a blank entity, you can add some fields now.
Note that the primary key will be added automatically (named id).
Available types: array, simple_array, json_array, object,
boolean, integer, smallint, bigint, string, text, datetime, datetimetz,
date, time, decimal, float, blob, guid.
New field name (press <return> to stop adding fields): titlu
Field type [string]:
Field length [255]:
New field name (press <return> to stop adding fields): descriere
Field type [string]: text
New field name (press <return> to stop adding fields): pagini
Field type [string]: integer
New field name (press <return> to stop adding fields):
Do you want to generate an empty repository class [no]?
You are going to generate a "TutorialTestBundle:Carti" Doctrine2 entity
using the "annotation" format.
Do you confirm generation [yes]?
Generating the entity code: OK
Ce produce urmatorul fisier Tutorial/TestBundle/Entity/Carti.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | <?php namespace Tutorial\TestBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Carti * * @ORM\Table() * @ORM\Entity */ class Carti { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="titlu", type="string", length=255) */ private $titlu; /** * @var string * * @ORM\Column(name="descriere", type="text") */ private $descriere; /** * @var integer * * @ORM\Column(name="pagini", type="integer") */ private $pagini; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set titlu * * @param string $titlu * @return Carti */ public function setTitlu($titlu) { $this->titlu = $titlu; return $this; } /** * Get titlu * * @return string */ public function getTitlu() { return $this->titlu; } /** * Set descriere * * @param string $descriere * @return Carti */ public function setDescriere($descriere) { $this->descriere = $descriere; return $this; } /** * Get descriere * * @return string */ public function getDescriere() { return $this->descriere; } /** * Set pagini * * @param integer $pagini * @return Carti */ public function setPagini($pagini) { $this->pagini = $pagini; return $this; } /** * Get pagini * * @return integer */ public function getPagini() { return $this->pagini; } } |
In acest exemplu am folosit modul de definire a metadatelor annotation similar cu phpdoc. Metodele de afisare a datelor si setare sunt descrise mai sus.
In cazul in care definim manual campurile, metodele de afisare a datelor si setare se genereaza cu comanda:
php app/console doctrine:generate:entities Tutorial/TestBundle/Entity/Carti
Pasul urmator este generarea tabelului aferent in baza de date:
php app/console doctrine:schema:update --force
Mai multe gasiti aici.
Anterior - Conectare la baza de date Urmator - Creare controller