Creare entitati - Tutorial Symfony
Postat la 28.05.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:
:linenos:
<?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