Question

I know some HTML, PHP, CSS and MySQL. Something that I haven't got to grasps with yet is frameworks. I am trying my best to read what they are and what they do, but for the life of me I cannot understand it.

Please could somebody explain frameworks and Doctrine 2 in a very simple way, as I don't know where to start with them, but notice that they are certainly required.

No correct solution

OTHER TIPS

I could tell you here what a framework is, but the answers to the question What is a software framework? already do it.

So, about Doctrine. It's an object relational mapper (ORM). It basically allows you to insert/update/select/delete an object in a relational database, or generate/update tables via classes.

Let's assume a simple Member table:

Simple table structure

Usually, you would write a query to, for example, insert something in a table. Such as:

INSERT Member VALUES ('Andy', 'andy@example.com', 30);

What an ORM allows you to do, is to insert a mapped object into the table. The values in the table will be seen just as you would normally see them by inserting them via query.

Let's look at a very simple example of Doctrine in the Symfony framework:

namespace ExampleProject\MemberBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
* Member
* @ORM\Table()
* @ORM\Entity
*/
class Member {
     /**
     * @var integer
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     private $id;

    /**
    * @var string
    * @ORM\Column(name="name", type="string", length=255)
    */
    private $name;

    /**
    * @var string
    * @ORM\Column(name="email", type="string", length=255)
    */
    private $email;

    /**
    * @var string
    * @ORM\Column(name="age", type="integer", length=3)
    */
    private $age;

    /* getters and setters here */
}

The above class is mapped (annotated with DocBlocks) and represents our Member table, describing which parts will be seen as columns in the database. Simply said: the mapped variables inside the class will be visible as columns in the database. And you also can tell by mapping these variables, which datatype you want the columns to have (string/integer etc).

Now in my code, I'm able to call the Doctrine entity manager, create a new Member object, initiate the properties and save it into the database, in a nice object-oriented syntax:

    $em = $this->getDoctrine()->getEntityManager();
    $member = new Member;
    $member->setId($id);
    $member->setName($name);
    $member->setEmail($email);
    $member->setAge($age);
    $em->persist($member);
    $em->flush();

As you can see, all we need to do is call to save the object in the database. In the background, the ORM also performs a INSERT query (similarly to the one I mentioned above). You even could enable a setting to see the actual query executed.

Now this may look quite unnecessary and lots of work. But it will save you a lot of time. It is more object oriented from your source code point of view, and you will be able to maintain your (medium/large) application much better than without using an ORM. Furthermore, if you currently have MySQL as a database, but you would like to change it to e.g., PostgreSQL, you can do so with minimal changes to your code as your ORM will take care of the underlying queries.

So in essence, the ORM is a database abstraction layer with object-oriented syntax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top