I'm working on an application, which tracks expenses. I have users. Each user can create his categories for the expenses (like Food, Bills, Transport, Drinks, Clothes) and then create expenses - each expense has one category (OneToMany relationship). For example the expense Banana is in the category Food for the user Faery. I also have months for each user. For example for March 2013 user Faery has a budget of 500, for March 2013 user SuperCoolUser has a budget of 900 and so on.
And here are my entities:
User.php
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="category")
*/
protected $categories;
/**
* @ORM\OneToMany(targetEntity="Expense", mappedBy="expense")
*/
protected $expenses;
public function __construct()
{
parent::__construct();
$this->categories = new ArrayCollection();
$this->expenses = new ArrayCollection();
}
// ...
}
Category.php
/**
* @ORM\Table(name="category")
* @UniqueEntity(fields={"name", "user"})
*/
class Category
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Expense", mappedBy="expense")
*/
protected $expenses;
public function __construct()
{
$this->expenses = new ArrayCollection();
}
/**
* @ORM\Column(type="string", length=100)
protected $name;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="fos_user")
* @ORM\JoinColumn(name="fos_user_id", referencedColumnName="id")
*/
protected $user;
// ...
}
Expense.php
/**
* @ORM\Table(name="expense")
*/
class Expense
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="fos_user")
* @ORM\JoinColumn(name="fos_user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="category")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* @ORM\Column(type="string", length=100)
*/
protected $product;
/**
* @ORM\Column(type="string")
*/
protected $description;
/**
* @ORM\Column(type="float")
*/
protected $price;
/**
* @ORM\Column(type="date")
*/
protected $date;
// ...
}
Month.php
/**
* @ORM\Table(name="month")
*/
class Month
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="fos_user")
* @ORM\JoinColumn(name="fos_user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\Column(type="float")
*/
protected $budget;
/...
}
All of this looked fine to me, but I noticed that category, month, expense, all of these has a connection to User. I made this for the queries to be more convinient, but isn't it becoming too duplicated?
I have really little expirience with databases and with programming, but I want the application to become nice and the code to be of good quality.
That's why I'm asking if this code is too problematic and connected and duplicated, or it's allowable. And it it's not please give me some advices or ideas how to implement this better. Thank you very much in advance!
P.S. I'm not sure if this question is for here or for StackOverflow, but it's working that way and I'm asking for a better solution, so I decided to post it here. Sorry a lot if it's not the right place.