How to save one Doctrine object in two database tables (necessary for MySQL optimization)

There is a table fileand a table in my database file_content. A table filestores file metadata, such as name, mimeand some others. file_contentstores blob with the contents of the file. I do not save blob in the same table as metadata for performance reasons only.

For the “version 2” of my project, I study Teaching (2.3). It seems to me that the "File" - it is one object with properties such as name, mime, extension, content, to be used as follows:

$file = new File();
$file->setName('hello.txt');
$file->setMime('text/plain');
$file->setContent('Hello world!')
$em->persist($file);
$em->flush();

Is this possible? It makes no sense for me to create two objects for something that is really just one entity. I could not find anything about this in the documentation, and I read a 2-year topic that this is not possible in Doctrine 2.1: Doctrine 2.1 - Map object for multiple tables

Someone tell me how to do it right? I am new to Doctrine and have played with it a bit to make sure this is the right choice for my project. Thank.

0
source share
3 answers

Do you have the opportunity to change the schema of your database? If so, I would consider combining this into a single table.

, , , "--" . , - :

class File {
    private $id;
    private $name;
    private $mime;
    private $content;
}

class Content {
    private $id;
    private $data;
    private $fileId;
}

Content- > fileId " " File- > id, , :

$file->getContent()->getData();
$file->getContent()->setData("something different");

: http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-one-unidirectional

0

, . .

FileMetadata, , mimetype .. FileContent, . , " " , , .

, , , setName :

  public function setName() {
    $this->getFileMetadata()->getName();
  }

:

  public function setName( $name ) {
    $this->getFileMetadata()->setName( $name );
  }

File FileMetadata FileContent setFilemetadata setFilecontent; .

File, , ( , ), - (, ) .

0

You can match both tables, resulting in Fileand FileContent, and add one-to-one relationships between them. Then add getter / setter methods to the contents of the file in the class File. In them you will need to call the appropriate methods in FileContent.

0
source

All Articles