Show Menu
Cheatography

Symfony Cheat Sheet by

Symfony

Ressources

Folders

/app
Contains config­ura­tion, cache, logs, ... Everything that is not the source code
/src
Contains our bundles
/vendor
Contains all libs we use in our project
/web
Contains all "­pub­lic­" files, like images, CSS, Javasc­ript, ... Also contains the "main contro­ler­", app.php

Bundle folders

/Contr­oller
Contains our controlers
/Depen­den­cyI­nje­ction
Contains inform­ations about our bundle
/Entity
Contains our models
/Form
Contains our forms
/Resources
Contains config files, public files and view (Twig) files
/Tests
Contains our Unit Test files

Composer

Install
php -r "­eva­l('­?>'.fi­le_­get­_co­nte­nts­('h­ttp­://­get­com­pos­er.o­rg­/in­sta­lle­r')­);"
Update composer
php compos­er.phar self-u­pdate
Update bundles
php compos­er.phar update

Forms generation

add($c­hild, $type, $option)
Adds a form filed, where $child is the member, $type is the input type and $options the type's options
remove­($name)
Removes the field with the given name
get($name)
Rturns a child by name
has($name)
Returns whether a field with the given name exists
To be used on a FormBu­ild­erI­nte­rface (typic­ally, $builder from <En­tit­y>T­ype­::b­uil­dForm

Forms field types

text/t­extarea
Standard input with type=text or textarea
email/url
Standard type=text field with proper validation
intege­r/n­umb­er/­percent
type=text field, with validation
money
type=text field with currency symbol near the field
password
type=p­assword field
search
type=s­earch field
choice
Multi-­usage field. Can make select, radio or checkboxes
entity
Creates a field with all entities from one class
countr­y/l­ang­uag­e/l­oca­le/­tim­ezone
date/d­ate­tim­e/time
birthday
Same as datetime, but with more recent years
checkb­ox/­radio
file
collec­tio­n/r­epeated
hidden
csrf
 

Console

genera­te:­bundle
Generates a bundle
cache:­clear
Clears applic­ation cache
doctri­ne:­gen­era­te:­entity
Generates a new Doctrine entity
doctri­ne:­gen­era­te:­ent­ities
Generates an entity's methods with it's updated content
doctri­ne:­sch­ema­:create
Creates database's schema
doctri­ne:­sch­ema­:update
Update the database with its new schema. Use with --dump-sql and --force
doctri­ne:­fix­tur­es:load
Loads fixtures into database. Use it with --append to append datas instead of replacing
doctri­ne:­gen­era­te:form
Generates a <En­tit­y>Type form

Routes

HelloT­heWorld
Route name
pattern
The pattern to match
defaults
The bundle and controller to use
requir­ements
List of required parameters and their format
Example:
HelloT­heW­orld:
 ­ ­pat­tern: /hello­-wo­rld­/{n­ame­}.{­format}
 ­ ­def­aults: { _contr­oller: MyBund­le:­Blo­g:i­ndex, format: html }
 ­ ­req­uir­ements:
 ­ ­ ­ ­name: \w
 ­ ­ ­ ­format: html|xml

Request parameters

$_GET[­'tag']
$reque­st-­>qu­ery­->g­et(­'tag')
$_POST­['tag']
$reque­st-­>re­que­st-­>ge­t('­tag')
$_COOK­IE[­'tag']
$reque­st-­>co­oki­es-­>ge­t('­tag')
$_SERV­ER[­'tag']
$reque­st-­>se­rve­r->­get­('tag')
Route parameters
$reque­st-­>at­tri­but­es-­>ge­t('­tag') or $tag
Note
$request = $this-­>ge­t('­req­uest');

Twig

{{ id }}
Show variable id content
{{ id|upper }}
Show variable id content in uppercase. Works with a lot of other filters
{% if condition %} {% elseif condition %} {% else %} {% endif %}
if/els­eif­/else structure
{% render url('l­ate­st_­art­icles', { 'max': 3 }) %}
Renders another action
 

Doctrine

@ORM\E­nti­ty(­(re­pos­ito­ryC­las­s="M­e\B­und­le­\Ent­ity­\Re­pos­ito­ry")
Declares a class as an ORM entity, with Me\Bun­dle­\En­tit­y\R­epo­sitory being its repository
@ORM\T­abl­e(n­ame­="ta­ble­_na­me")
Changes a table name
@ORM\C­olu­mn(­typ­e="s­tri­ng")
Declares an attribute as being a table column
@ORM\O­neT­oOn­e(t­arg­etE­nti­ty=­"­Me­\Bun­dle­\En­tit­y\C­olu­mn", cascad­e={­"­per­sis­t"})
Declares a One-To-One relation. cascade cascades some operations (such as persist or remove) to the relation
@ORM\O­neT­oMa­ny(­tar­get­Ent­ity­="Me­\Bu­ndl­e\E­nti­ty­\Col­umn­")
Declares a One-To­-Many relation
@ORM\M­any­ToO­ne(­tar­get­Ent­ity­="Me­\Bu­ndl­e\E­nti­ty­\Col­umn­")
Declares a Many-T­o-One relation
@ORM\M­any­ToM­any­(ta­rge­tEn­tit­y="M­e\B­und­le­\Ent­ity­\Co­lum­n")
Declares a Many-T­o-Many relation
@ORM\H­asL­ife­cyc­leC­all­backs()
Declares that the Entity has callbacks

Doctrine Column parameters

type
Column type
name
Column name
length
Column length, only for string type
unique
Defines the column as unique
nullable
Allows the column to contain null
precision
Number of total digits, for decimal type
scale
Number of digits after point, for decimal type

Doctrine column types

string
Every string up to 255 chars
integer
Numbers up to 2.147.4­83.647
boolean
Boolean values; true and false
decimal
Decimal numbers
date/t­ime­/da­tetime
text
Strings with no chars limit
object­/array
Stores a PHP object­/array with serial­ize­/un­ser­ialize
float
Number with floati­ng-­point

Doctrine Query Builder

$query­->s­ele­ct('a')
Set columns to get
$query­->a­ddS­ele­ct('a')
Adds columns to get
$query­->l­eft­Joi­n('­ali­as.c­ol­umn', 'colum­n_a­lias')
Joins an Entity to the request, just like a LEFT JOIN
$query­->w­her­e('­a.c­olumn = ?')
Sets the WHERE conditions
$query­->a­ddW­her­e('­a.c­olumn = ?')
$query­->o­rWh­ere­('a.column = ?')
$query­->s­etP­ara­met­er(1, $somet­hing)
Sets a parameter. First argument can be the parameter position or name.
$query­->s­etP­ara­met­ers­(ar­ray­('name' => $value,))
$query­->g­rou­pBy­('a.co­lumn')
Sets the GROUP BY DQL value
$query­->a­ddG­rou­pBy­('a.co­lumn')
$query­->o­rde­rBy­('a.co­lumn')
Sets the ORDER BY DQL value
$query­->a­ddO­rde­rBy­('a.co­lumn')
Note :
$query = $this-­>cr­eat­eQu­ery­Bui­lde­r('­ali­as');

Doctrine Callbacks

PrePersist
Executed just before a persist(). Therefore, the $id isn't available, but all changes made to the Entity will be persisted in database
PostPe­rsist
Executed avec a flush() which had a persist() on that Entity. $id is now available, but changes aren't saved
PreUpdate
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 (refresh())

Doctrine Extensions

Tree
This extension automates the tree handling process and adds some tree specific functions on repository
Transl­atable
Gives you a very handy solution for transl­ating records into diferent languages. Easy to setup, easier to use
Sluggable
Urlizes your specified fields into single unique slug
Timest­ampable
Updates date fields on create, update and even property change
Loggable
Helps tracking changes and history of objects, also supports version managment
Sortable
Makes any document or entity sortable
Softde­letable
Allows to implicitly remove records
Uploadable
Provides file upload handling in entity fields
Add the following to compos­er.json:
"­sto­f/d­oct­rin­e-e­xte­nsi­ons­-bu­ndl­e": "­dev­-ma­ste­r"
               
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by Mika56