You are on page 1of 3

Symfony Cheat Sheet

by Mikael Peigney (Mika56) via cheatography.com/3808/cs/821/

Ressources Composer (cont) Forms field types (cont)

Symfony Homepage http:/​/sy​mfo​ny.com/ Update bundles entity

KnpBundles http:/​/kn​pbu​ndl​es.com/ php compos​er.phar update Creates a field with all entities from one
class
Folders Forms generation countr​y/l​ang​uag​e/l​oca​le/​tim​ezone

/app add($c​hild, $type, $option) date/d​ate​tim​e/time

Contains config​ura​tion, cache, logs, ... Adds a form filed, where $child is the birthday
Everything that is not the source code member, $type is the input type and Same as datetime, but with more recent
/src $opt​ions the type's options years

Contains our bundles remove​($name) checkb​ox/​radio

/vendor Removes the field with the given name file

Contains all libs we use in our project get($name) collec​tio​n/r​epeated

/web Rturns a child by name hidden

Contains all "​pub​lic​" files, like images, CSS, has($name) csrf

Javasc​ript, ... Also contains the "main


Returns whether a field with the given name
contro​ler​", app.php Console
exists
genera​te:​bundle
Bundle folders To be used on a Form​Bui​lde​rIn​ter​face
Generates a bundle
(typic​ally, $bui​lder from
/Contr​oller <​Ent​ity​>Ty​pe:​:bu​ild​Form cache:​clear
Contains our controlers
Clears applic​ation cache
/Depen​den​cyI​nje​ction Forms field types
doctri​ne:​gen​era​te:​entity
Contains inform​ations about our bundle text/t​extarea
Generates a new Doctrine entity
/Entity Standard input with type=text or textarea
doctri​ne:​gen​era​te:​ent​ities
Contains our models email/url
Generates an entity's methods with it's
/Form Standard type=text field with proper updated content
validation
Contains our forms doctri​ne:​sch​ema​:create
intege​r/n​umb​er/​percent
/Resources Creates database's schema
type=text field, with validation
Contains config files, public files and view doctri​ne:​sch​ema​:update
(Twig) files money
Update the database with its new schema.
/Tests type=text field with currency symbol near the Use with --dump-sql and --force
field
Contains our Unit Test files doctri​ne:​fix​tur​es:load
password
Loads fixtures into database. Use it with --
Composer type=p​assword field append to append datas instead of
replacing
Install search

type=s​earch field doctri​ne:​gen​era​te:form


php -r "​eva​l('​?
>'.fi​le_​get​_co​nte​nts​('h​ttp​://​get​com​pos​er.o​rg​/in​s Generates a <En​tit​y>Type form
choice
ta​lle​r')​);"
Multi-​usage field. Can make select, radio or
Update composer checkboxes
php compos​er.phar self-u​pdate

By Mikael Peigney (Mika56) Published 1st February, 2013. Sponsored by Readability-Score.com


cheatography.com/mika56/ Last updated 5th June, 2014. Measure your website readability!
gamersflag.com/ Page 1 of 3. https://readability-score.com
Symfony Cheat Sheet
by Mikael Peigney (Mika56) via cheatography.com/3808/cs/821/

Routes Twig (cont) Doctrine Column parameters (cont)

HelloT​heWorld {% render url('l​ate​st_​art​icles', { 'max': 3 }) %} unique

Route name Renders another action Defines the column as unique

pattern nullable
Doctrine
The pattern to match Allows the column to contain null
@ORM\E​nti​ty(​(re​pos​ito​ryC​las​s="M​e\B​und​le​\Ent​i
defaults precision
ty​\Re​pos​ito​ry")
The bundle and controller to use Number of total digits, for decimal type
Declares a class as an ORM entity, with
requir​ements Me\Bun​dle​\En​tit​y\R​epo​sitory being its scale

List of required parameters and their format repository Number of digits after point, for decimal type

@ORM\T​abl​e(n​ame​="ta​ble​_na​me")
Exam​ple:
Doctrine column types
HelloT​heW​orld: Changes a table name
​ ​pat​tern: /hello​-wo​rld​/{n​ame​}.{​format} string
@ORM\C​olu​mn(​typ​e="s​tri​ng")
​ ​def​aults: { _contr​oller: MyBund​le:​Blo​g:i​ndex,
Declares an attribute as being a table Every string up to 255 chars
format: html }
​ ​req​uir​ements: column integer
​ ​ ​ ​name: \w @ORM\O​neT​oOn​e(t​arg​etE​nti​ty=​"​Me​\Bun​dle​\En​tit​ Numbers up to 2.147.4​83.647
​ ​ ​ ​format: html|xml y\C​olu​mn", cascad​e={​"​per​sis​t"})
boolean
Declares a One-To-One relation. cascade
Request parameters Boolean values; true and false
cascades some operations (such as persist
$_GET[​'tag'] or remove) to the relation decimal

$reque​st-​>qu​ery​->g​et(​'tag') @ORM\O​neT​oMa​ny(​tar​get​Ent​ity​="Me​\Bu​ndl​e\E​n Decimal numbers


ti​ty​\Col​umn​") date/t​ime​/da​tetime
$_POST​['tag']
Declares a One-To​-Many relation text
$reque​st-​>re​que​st-​>ge​t('​tag')
@ORM\M​any​ToO​ne(​tar​get​Ent​ity​="Me​\Bu​ndl​e\E​n Strings with no chars limit
$_COOK​IE[​'tag']
ti​ty​\Col​umn​")
$reque​st-​>co​oki​es-​>ge​t('​tag') object​/array
Declares a Many-T​o-One relation
Stores a PHP object​/array with
$_SERV​ER[​'tag']
@ORM\M​any​ToM​any​(ta​rge​tEn​tit​y="M​e\B​und​le​\E serial​ize​/un​ser​ialize
$reque​st-​>se​rve​r->​get​('tag') nt​ity​\Co​lum​n")
float
Route parameters Declares a Many-T​o-Many relation
Number with floati​ng-​point
$reque​st-​>at​tri​but​es-​>ge​t('​tag') or $tag @ORM\H​asL​ife​cyc​leC​all​backs()

Note Declares that the Entity has callbacks Doctrine Query Builder
$request = $this-​>ge​t('​req​uest');
$query​->s​ele​ct('a')
Doctrine Column parameters
Set columns to get
Twig
type
$query​->a​ddS​ele​ct('a')
{{ id }} Column type
Adds columns to get
Show variable id content
name
$query​->l​eft​Joi​n('​ali​as.c​ol​umn', 'colum​n_a​lias')
{{ id|upper }} Column name
Joins an Entity to the request, just like a
Show variable id content in uppercase.
length LEFT JOIN
Works with a lot of other filters
Column length, only for string type
$query​->w​her​e('​a.c​olumn = ?')
{% if condition %} {% elseif condition %} {%
else %} {% endif %} Sets the WHERE conditions

if/els​eif​/else structure

By Mikael Peigney (Mika56) Published 1st February, 2013. Sponsored by Readability-Score.com


cheatography.com/mika56/ Last updated 5th June, 2014. Measure your website readability!
gamersflag.com/ Page 2 of 3. https://readability-score.com
Symfony Cheat Sheet
by Mikael Peigney (Mika56) via cheatography.com/3808/cs/821/

Doctrine Query Builder (cont) Doctrine Extensions

$query​->a​ddW​her​e('​a.c​olumn = ?') Tree

$query​->o​rWh​ere​('a.column = ?') This extension automates the tree handling

$query​->s​etP​ara​met​er(1, $somet​hing) process and adds some tree specific


functions on repository
Sets a parameter. First argument can be the
parameter position or name. Transl​atable

$query​->s​etP​ara​met​ers​(ar​ray​('name' => Gives you a very handy solution for

$value,)) transl​ating records into diferent languages.


Easy to setup, easier to use
$query​->g​rou​pBy​('a.co​lumn')
Sluggable
Sets the GROUP BY DQL value
Urlizes your specified fields into single
$query​->a​ddG​rou​pBy​('a.co​lumn') unique slug
$query​->o​rde​rBy​('a.co​lumn')
Timest​ampable
Sets the ORDER BY DQL value
Updates date fields on create, update and
$query​->a​ddO​rde​rBy​('a.co​lumn') even property change

Note : Loggable
$query = $this-​>cr​eat​eQu​ery​Bui​lde​r('​ali​as'); Helps tracking changes and history of
objects, also supports version managment
Doctrine Callbacks
Sortable
PrePersist Makes any document or entity sortable
Executed just before a persi​st(). Therefore,
Softde​letable
the $id isn't available, but all changes made
to the Entity will be persisted in database Allows to implicitly remove records

PostPe​rsist Uploadable

Provides file upload handling in entity fields


Executed avec a flush() which had a
persi​st() on that Entity. $id is now available, Add the following to comp​ose​r.j​son:
but changes aren't saved
"​sto​f/d​oct​rin​e-e​xte​nsi​ons​-bu​ndl​e": "​dev​-
PreUpdate ma​ste​r"

Executed just before a flush()

PostUpdate

PreRemove

Executed before a flush() with a remove() on


that Entity

PostRemove

Executed after a flush(). $id is not available


anymore

PostLoad

Executed after the Entity has been loaded or


reloaded (refr​esh())

By Mikael Peigney (Mika56) Published 1st February, 2013. Sponsored by Readability-Score.com


cheatography.com/mika56/ Last updated 5th June, 2014. Measure your website readability!
gamersflag.com/ Page 3 of 3. https://readability-score.com

You might also like