As promised in my first post, I’m going to be looking at the reasons I chose to go with Django rather than Symfony2. Aside from wanting to learn a new language, there were a few things that jumped out at me when looking into Django.
One of the biggest was the dramatic amount of code necessary to do the same thing in Symfony2. Let’s look at a typical model for a blog. Here’s what it looks like in Django (I’ve added a small utility function via a manager):
models/blog.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class BlogManager(models.Manager): | |
def get_tags(self): | |
# get just the tags | |
tags = self.values('tags') | |
# combine all tags into a single list | |
all_tags = [] | |
for tag in tags: | |
all_tags += tag.values()[0].split(',') | |
# strip whitespace | |
stripped_tags = [tag.strip() for tag in all_tags] | |
return stripped_tags | |
class Blog(models.Model): | |
title = models.CharField(max_length=255) | |
slug = models.CharField(max_length=255) | |
author = models.CharField(max_length=100) | |
blog = models.TextField() | |
image = models.CharField(max_length=20) | |
tags = models.TextField() | |
created = models.DateTimeField('date created', auto_now_add=True) | |
updated = models.DateTimeField('last updated', auto_now_add=True, auto_now=True) | |
objects = BlogManager() | |
def __unicode__(self): | |
return self.title | |
class Meta: | |
app_label = 'app' |
Now here’s the same model in Symfony2 along with the utility function:
Entity/Blog.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace NewCoInc\AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use NewCoInc\AppBundle\Lib\FormattingLib; | |
/** | |
* Blog | |
* | |
* @ORM\Table(name="blog") | |
* @ORM\Entity(repositoryClass="NewCoInc\AppBundle\Entity\BlogRepository") | |
*/ | |
class Blog | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer", nullable=false) | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="IDENTITY") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="title", type="string", length=255, nullable=false) | |
*/ | |
private $title; | |
/** | |
* @ORM\Column(type="string") | |
*/ | |
private $slug; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="author", type="string", length=100, nullable=false) | |
*/ | |
private $author; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="blog", type="text", nullable=false) | |
*/ | |
private $blog; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="image", type="string", length=20, nullable=false) | |
*/ | |
private $image; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="tags", type="text", nullable=false) | |
*/ | |
private $tags; | |
/** | |
* @ORM\OneToMany(targetEntity="Comment", mappedBy="blog") | |
*/ | |
private $comments; | |
/** | |
* @var \DateTime | |
* | |
* @ORM\Column(name="created", type="datetime", nullable=false) | |
*/ | |
private $created; | |
/** | |
* @var \DateTime | |
* | |
* @ORM\Column(name="updated", type="datetime", nullable=false) | |
*/ | |
private $updated; | |
public function __construct() | |
{ | |
$this->comments = new ArrayCollection(); | |
$this->setCreated(new \DateTime()); | |
$this->setUpdated(new \DateTime()); | |
$this->formatter = new FormattingLib(); | |
} | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set title | |
* | |
* @param string $title | |
* @return Blog | |
*/ | |
public function setTitle($title) | |
{ | |
$this->title = $title; | |
$this->setSlug($this->title); | |
return $this; | |
} | |
/** | |
* Get title | |
* | |
* @return string | |
*/ | |
public function getTitle() | |
{ | |
return $this->title; | |
} | |
/** | |
* Set author | |
* | |
* @param string $author | |
* @return Blog | |
*/ | |
public function setAuthor($author) | |
{ | |
$this->author = $author; | |
return $this; | |
} | |
/** | |
* Get author | |
* | |
* @return string | |
*/ | |
public function getAuthor() | |
{ | |
return $this->author; | |
} | |
/** | |
* Set blog | |
* | |
* @param string $blog | |
* @return Blog | |
*/ | |
public function setBlog($blog) | |
{ | |
$this->blog = $blog; | |
return $this; | |
} | |
/** | |
* Get blog | |
* | |
* @return string | |
*/ | |
public function getBlog() | |
{ | |
return $this->blog; | |
} | |
/** | |
* Set image | |
* | |
* @param string $image | |
* @return Blog | |
*/ | |
public function setImage($image) | |
{ | |
$this->image = $image; | |
return $this; | |
} | |
/** | |
* Get image | |
* | |
* @return string | |
*/ | |
public function getImage() | |
{ | |
return $this->image; | |
} | |
/** | |
* Set tags | |
* | |
* @param string $tags | |
* @return Blog | |
*/ | |
public function setTags($tags) | |
{ | |
$this->tags = $tags; | |
return $this; | |
} | |
/** | |
* Get tags | |
* | |
* @return string | |
*/ | |
public function getTags() | |
{ | |
return $this->tags; | |
} | |
/** | |
* Set created | |
* | |
* @param \DateTime $created | |
* @return Blog | |
*/ | |
public function setCreated($created) | |
{ | |
$this->created = $created; | |
return $this; | |
} | |
/** | |
* Get created | |
* | |
* @return \DateTime | |
*/ | |
public function getCreated() | |
{ | |
return $this->created; | |
} | |
/** | |
* Set updated | |
* | |
* @param \DateTime $updated | |
* @return Blog | |
*/ | |
public function setUpdated($updated) | |
{ | |
$this->updated = $updated; | |
return $this; | |
} | |
/** | |
* Get updated | |
* | |
* @return \DateTime | |
*/ | |
public function getUpdated() | |
{ | |
return $this->updated; | |
} | |
/** | |
* Add comments | |
* | |
* @param \NewCoInc\AppBundle\Entity\Comment $comments | |
* @return Blog | |
*/ | |
public function addComment(\NewCoInc\AppBundle\Entity\Comment $comments) | |
{ | |
$this->comments[] = $comments; | |
return $this; | |
} | |
/** | |
* Remove comments | |
* | |
* @param \NewCoInc\AppBundle\Entity\Comment $comments | |
*/ | |
public function removeComment(\NewCoInc\AppBundle\Entity\Comment $comments) | |
{ | |
$this->comments->removeElement($comments); | |
} | |
/** | |
* Get comments | |
* | |
* @return \Doctrine\Common\Collections\Collection | |
*/ | |
public function getComments() | |
{ | |
return $this->comments; | |
} | |
/** | |
* Set slug | |
* | |
* @param string $slug | |
* @return Blog | |
*/ | |
public function setSlug($slug) | |
{ | |
$this->slug = $this->formatter->slugify($slug); | |
return $this; | |
} | |
/** | |
* Get slug | |
* | |
* @return string | |
*/ | |
public function getSlug() | |
{ | |
return $this->slug; | |
} | |
} |
Entity/BlogRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace NewCoInc\AppBundle\Entity; | |
use Doctrine\ORM\EntityRepository; | |
class BlogRepository extends EntityRepository | |
{ | |
public function getTags() | |
{ | |
$blogTags = $this->createQueryBuilder('b') | |
->select('b.tags') | |
->getQuery() | |
->getResult(); | |
$tags = array(); | |
foreach ($blogTags as $blogTag) | |
{ | |
$tags = array_merge(explode(",", $blogTag['tags']), $tags); | |
} | |
foreach ($tags as &$tag) | |
{ | |
$tag = trim($tag); | |
} | |
return $tags; | |
} | |
} |
Wow, that’s a TON of code. I will mention that Symfony2 does offer some tools to help generate this code, which are very helpful. But it feels to me like they’re trying to overcome the shortcomings of the language.