Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity containing other entities without having a table

I have an Entity ( Invoice ) which is purely for calculation purposes and has no table, that associates with two other entities having relations by tables. (Although there are so many other entities involved ).

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

    /**
     * @ORM\ManyToOne(targetEntity="File")
     * @ORM\JoinColumn(name="file_id", referencedColumnName="file_id")
     */
    protected $file;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="date")
     */
    private $date;

}

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

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


}


class Invoice
{
    /**
     * @ORM\Id
     * @ORM\Column(name="invoice_id", type="integer")
     * @ORM\GeneratedValue
     */
    protected $id = null;

    /**
     * @ORM\OneToMany(targetEntity="Row", mappedBy="row_id")
     */
    protected $row;

    /**
     * @ORM\OneToMany(targetEntity="File", mappedBy="file_id")
     */
    protected $file;

}

I want to be able to query for Invoices :

$sDate = //Some date
$this->getEntityManager()
     ->createQuery("SELECT Invoice, Row, File
                        FROM
                            ReportsEntitiesBundle:Invoice Invoice
                        LEFT JOIN
                            Row.row Row
                        LEFT JOIN
                            Row.file File
                    WHERE date=:date"
                  )
 ->setParaMeter(':date', $sDate)
 ->setFirstResult($iPage*$iLimit)
 ->setMaxResults($iLimit)
 ->getResult();

The questions : # Doctrine tries to query the database, how can I prevent that and have it find the relevant entities? # How can I relate the date ( which is in Row entity and cannot be in Invoice ) to the query?

Later this Invoice will become a part of another big entity for calculating/search purposes.

Thank you

like image 441
Arsham Avatar asked Jul 04 '13 14:07

Arsham


1 Answers

Short Answer: You can't

Long Answer : You can't because an entity with @ORM annotations means its persisted to a database - querying that entity relates to querying a database table. Why not just create the table ?!?!?

You need somewhere to persist the association between file and row - a database table is a perfect place !!!!

Update

Just to clarify ... an Entity is just a standard class - it has properties and methods ... just like any other class - When you issue doctrine based commands it uses the annotations within the entities to configure the tables / columns / relationships etc if remove those you can use it however you like ... but you will need to populate the values to use it and you wont be able to use it in a Doctrine query and it obviously wont be persisted !

like image 82
Manse Avatar answered Oct 23 '22 05:10

Manse