An object containing other objects without a table

I have Entity (Invoice), which is intended solely for calculation purposes and does not have a table that links two other objects related by tables. (Although there are so many other parties 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 request 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(); 

Questions: # Doctrine is trying to execute a database query, how can I prevent this and find the appropriate objects? # How can I relate a date (which is in the Row entity and cannot be on the invoice) to the request?

Later this invoice will become part of another large facility for calculation / search.

thanks

+4
source share
2 answers

Short answer: you cannot

The long answer: you cannot, because an entity with @ORM annotations means that it is stored in the database - the query of this object refers to the query of the database table. Why not just create a table?!?!?

You need somewhere to keep the connection between file and row - the database table is the perfect place !!!!

Update

Just to clarify ... Entity is just a standard class - it has properties and methods ... like any other class. When you issue doctrine-based commands, it uses annotations inside entities to set up tables / columns / relationships, etc. if you delete the ones you can use but you like ... but you will need to fill in the values ​​to use them, and you won’t be able to use it in a Doctrine request, and this obviously will not be saved!

+6
source

You can use the object read-only. This content is supported by a view that you create manually in SQL.

PHP:

 /** @ORM\Entity(readOnly =true) */ class InvoiceView { ... 

SQL:

 CREATE VIEW invoice_view AS ( SELECT ... 
0
source

Source: https://habr.com/ru/post/1489734/


All Articles