Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML <?php /** <br/> * @Entity <br/> * @Table(name="my_model") <br/> */ class MyModel { /** <br/> * @Id @Column(name="id", type="integer") <br/> * @GeneratedValue(strategy="AUTO") <br/> */ private $id ; /** <br/> * @Column(name="title", type="string", length=255) <br/> */ private $title ; }
Copy Source | Copy HTML
- kernel. config : ~
- web . web : ~
- web . templating : ~
- doctrine. dbal :
- default_connection : default
- connections :
- default :
- driver : PDOMySql
- dbname : sfbox2
- user : root
- password : 123
- host : localhost
- event_manager_class : Doctrine \ Common \ EventManager
- configuration_class : Doctrine \ DBAL \ Configuration
- doctrine. orm :
- default_entity_manager : default
- cache_driver : array
- entity_managers :
- default :
- connection : default
Copy Source | Copy HTML
- <? php
- namespace Application \ ReadlaterBundle \ Entities;
- use Doctrine \ ORM \ EntityRepository;
- / ** <br/> * @Entity <br/> * @Table (name = "readlater_link") <br/> * /
- class Link
- {
- / ** <br/> * @Id @Column (name = "id", type = "integer") <br/> * @GeneratedValue (strategy = "AUTO") <br/> * /
- private $ id ;
- / ** <br/> * @Column (name = "url", type = "string", length = 255) <br/> * /
- private $ url ;
- / ** <br/> * @Column (name = "created_at", type = "datetime") <br/> * /
- private $ createdAt ;
- / ** <br/> * constructor <br/> * /
- public function __construct ()
- {
- $ this -> createdAt = new \ DateTime ();
- }
- / ** <br/> * to string <br/> * /
- public function __toString ()
- {
- return $ this -> getUrl ();
- }
- }
readlater/console doctrine:database:create
readlater/console doctrine:generate:entities
readlater/console doctrine:schema:create
Copy Source | Copy HTML
- <? php
- namespace Application \ ReadlaterBundle \ Entities;
- use Doctrine \ ORM \ EntityRepository;
- / ** <br/> * @Entity <br/> * @Table (name = "readlater_link") <br/> * /
- class Link
- {
- / ** <br/> * @Id @Column (name = "id", type = "integer") <br/> * @GeneratedValue (strategy = "AUTO") <br/> * /
- private $ id ;
- / ** <br/> * @Column (name = "url", type = "string", length = 255) <br/> * /
- private $ url ;
- / ** <br/> * @Column (name = "created_at", type = "datetime") <br/> * /
- private $ createdAt ;
- / ** <br/> * constructor <br/> * /
- public function __construct ()
- {
- $ this -> createdAt = new \ DateTime ();
- }
- / ** <br/> * to string <br/> * /
- public function __toString ()
- {
- return $ this -> getUrl ();
- }
- / ** <br/> * Get id <br/> * <br/> * @return integer $ id <br/> * /
- public function getId ()
- {
- return $ this -> id;
- }
- / ** <br/> * Set url <br/> * <br/> * @param string $ url <br/> * /
- public function setUrl ( $ url )
- {
- $ this -> url = $ url ;
- }
- / ** <br/> * Get url <br/> * <br/> * @return string $ url <br/> * /
- public function getUrl ()
- {
- return $ this -> url;
- }
- / ** <br/> * Set createdAt <br/> * <br/> * @param datetime $ createdAt <br/> * /
- public function setCreatedAt ( $ createdAt )
- {
- $ this -> createdAt = $ createdAt ;
- }
- / ** <br/> * Get createdAt <br/> * <br/> * @return datetime $ createdAt <br/> * /
- public function getCreatedAt ()
- {
- return $ this -> createdAt;
- }
- }
Copy Source | Copy HTML
- link_list :
- pattern : /
- defaults : { _bundle : ReadlaterBundle, _controller : Readlater, _action : index }
- add_link :
- pattern : / add
- defaults : { _bundle : ReadlaterBundle, _controller : Readlater, _action : add }
- read_link :
- pattern : / read /: id
- defaults : { _bundle : ReadlaterBundle, _controller : Readlater, _action : read }
Copy Source | Copy HTML
- <? php
- namespace Application \ ReadlaterBundle \ Controller;
- use Symfony \ Framework \ WebBundle \ Controller,
- Application \ ReadlaterBundle \ Entities \ Link,
- Doctrine \ ORM \ QueryBuilder;
- class ReadlaterController extends Controller
- {
- public function indexAction ()
- {
- $ em = $ this -> container-> getDoctrine_ORM_EntityManagerService ();
- $ links = $ em -> getRepository ( 'Application \ ReadlaterBundle \ Entities \ Link' ) -> findAll ();
- return $ this -> render ( 'ReadlaterBundle: Readlater: index' , array ( 'links' => $ links ));
- }
- public function addAction ()
- {
- $ request = $ this -> getRequest ();
- $ url = $ request -> request-> get ( 'link' );
- $ link = new Link ();
- $ link -> setUrl ( $ url );
- $ em = $ this -> container-> getDoctrine_ORM_EntityManagerService ();
- $ em -> persist ( $ link );
- $ em -> flush ();
- return $ this -> redirect ( $ this -> generateUrl ( 'link_list' ));
- }
- public function readAction ( $ id )
- {
- $ em = $ this -> container-> getDoctrine_ORM_EntityManagerService ();
- $ link = $ em -> find ( 'Application \\ ReadlaterBundle \\ Entities \\ Link' , $ id );
- $ em -> remove ( $ link );
- $ em -> flush ();
- return $ this -> redirect ( $ this -> generateUrl ( 'link_list' ));
- }
- }
$em = $this->container->getDoctrine_ORM_EntityManagerService();
so using the DI Container we access the EntityManager
$em->getRepository()
- so we get a repository of objects of a certain type, that is, some kind of * Table from Doctrine 1.x.
$em->persist($link);
- so we save the object
$em->flush()
so we complete the transaction, that is, we actually send the data to the database.
$em->remove($link)
- well, and so remove the object.
Copy Source | Copy HTML
- / ** <br/> * LinkRepository <br/> * /
- class LinkRepository extends EntityRepository
- {
- public function findAll ()
- {
- return $ this -> _ em-> createQuery ( 'SELECT l FROM Application \\ ReadlaterBundle \\ Entities \\ Link l ORDER BY l.createdAt DESC' ) -> getResult ();
- }
- }
Copy Source | Copy HTML
- / ** <br/> * @Entity (repositoryClass = "Application \ ReadlaterBundle \ Entities \ LinkRepository") <br/> * @Table (name = "readlater_link") <br/> * /
- class Link
- {
- // ...
- }
Source: https://habr.com/ru/post/91565/