2017 © Pedro Peláez
 

symfony-bundle doctrine_typescript_bundle

convert doctrine entities into typescript classes

image

kif/doctrine_typescript_bundle

convert doctrine entities into typescript classes

  • Thursday, November 6, 2014
  • by Kifah
  • Repository
  • 0 Watchers
  • 4 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

KifDoctrineToTypescriptBundle

Main Concept

This is a command line tool for converting doctrine entities into Typescript language files. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript, and heavily uses OOP-Terminolgy and structuring., (*1)

A typical Scenario, is if you have a frontEnd based on Typscript/Javascript, and a backend based on symfony/php, and you want to have a unified DTO/MODEL representation between both. So if you structure something using doctrine, you want it autoamtically generated in your javascript part of the project, without needed to write the code for it., (*2)

Installation

``` bash $ php composer.phar require kif/doctrine_typescript_bundle 'dev-master', (*3)



* activate the bundle in your <code>app/AppKernel.php</code> file <br> ``` php <?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Kif\DoctrineToTypescriptBundle\KifDoctrineToTypescriptBundle(), ); }

Usage

Standard Usage

  • in your symfony folder use the command

``` bash $ php app/console kif:doctrine:typescript:generate destination_folder, (*4)


<code>destination_folder</code> must be a writable folder. This would generate a folder <code>/models/</code> in the given destination folder, with Typescript files containing all the models represented in your symfony project as doctrine entities so let us imagine we have a single Doctrine Entity in our Project: ``` php <?php // src/Acme/UserBundle/Entity/Contact.php namespace Acme\UserBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="contacts") */ class Contact { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string * * @ORM\Column(name="nick_name", type="string", length=255, nullable=true) */ private $nickName; }

Now if we run the command, (*5)

``` bash $ php app/console kif:doctrine:typescript:generate src/typscript, (*6)


we will find the following file gets generated ``` bash $ src/typscript/models/AcmeUserBundleEntity/Contact.ts

with the content, (*7)

``` typescript, (*8)

module AcmeUserBundleEntity {, (*9)

export class Contact {, (*10)

private _id:number ;, (*11)

get id(){, (*12)

return this._id;, (*13)

}, (*14)

set id(_id:number){, (*15)

this._id=_id;, (*16)

}, (*17)

private _nickName:string ;, (*18)

get nickName(){, (*19)

return this._nickName;, (*20)

}, (*21)

set nickName(_nickName:string){, (*22)

this._nickName=_nickName;, (*23)

}, (*24)



Now you can have access to the generated model easily in your Typescript code ``` typescript ///<reference path="models/AcmeUserBundleEntity/Contact"/> var contact = new AcmeUserBundleEntity.Contact(); contact.nickName = "myNickname"; alert(contact.nickName);

Usage with option --exposed-only

Important: For this to work correctly, you need to have the JMS Serialzer Bundle installed correctly., (*25)

There are cases, where you want to generate only Entites and fields, that are exposed by the JMS serializer, because you want to keep the rest private or hidden from other parts of your project., (*26)

Let us revist the doctrine entity from earlier., (*27)

``` php <?php // src/Acme/UserBundle/Entity/Contact.php //... use JMS\Serializer\Annotation\ExclusionPolicy; use JMS\Serializer\Annotation\Expose;, (*28)

  • @ORM\Table(name="contacts"), (*29)

    • @ExclusionPolicy("all") */ class Contact { //...

    /**, (*30)

    • @Expose */ private $nickName;

}, (*31)


Notice the annotation <code>@ExclusionPolicy("all")</code> that excludes all fields, and the annotation <code>@Expose</code> right over the field <code>$nickName</code>. This would generate the following Typescript file. ``` typescript module AcmeUserBundleEntity { private _nickName:string ; get nickName(){ return this._nickName; } set nickName(_nickName:string){ this._nickName=_nickName; }

Notice how only the field $nickName was generated ,while the rest was ignored. (hidden), (*32)

Also note that only entites where @ExclusionPolicy is set as (none or all) are generated. So entites without this annotation get automatically recognized as @ExclusionPolicy(all) and get completey ignored by the generator, (*33)

Todo

  • set a --single-file option to a generate all models in a single .ts/js file. This should stop the bundle-to-folder mechanism. The name of the generated file would be simply "models.ts"
  • when using --exposed-only option be able to disable the setters when the field is @readOnly

The Versions

06/11 2014

dev-master

9999999-dev https://github.com/Kifah/KifDoctrineToTypescriptBundle

convert doctrine entities into typescript classes

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 cli doctrine typescript