Reputation: 1452
I want to model my database directly and not through Doctrine. What is the best way to define my entities?
I understand that If I create an entity from database, I can't make any changes to it because it will be rewrite whenever I change my database.
It is Ok to define like this
<?php
/* entity code generated */
class MyEntityBase
{
private $id;
private $title;
function getTitle()
{
return $this->title()
}
}
class MyEntity extends MyEntityBase
{
function getTitle()
{
return trim($this->title());
}
}
I choose to definie like this Hi. I choose in defining like this
class Article
{
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/** @Column(type="string") */
private $title;
function setTitle($title)
{
$this->title = $title;
}
function __construct() {
// $this->comments = new ArrayCollection();
}
}
$article = $em->find('Article', 1);
Upvotes: 0
Views: 457
Reputation: 14447
There is nothing wrong with defining your entities like this, though I would advice using the mapping feature doctrine uses because it makes working with relations a lot easier.
Whether you choose to use DocBlock annotations or annotate your models in YAML or XML doesn't make much difference. It's just important that you do annotate them do describe your models to Doctrine can do the smart stuff it's designed to do (ORM).
Also describing your database structure in your models doesn't require you to actually sync it with the database server, it's just a way for doctrine to know what properties and methods an Entity has and optionally you can also synchronize your models with your data or vice versa.
Upvotes: 2