send value to a trait from use of a class

0

I have a class Printer that uses a Trait called TagTrait . I want to send a value from the class that uses that trait so that the trait defines a relation of a table in doctrine according to what interests me.

Example:

class Printer 
{
    use TagTrait; // aqui hacer algo para enviar el texto "printer"
    // .. varios metodos

}


use Doctrine\ORM\Mapping as ORM;

trait TagTrait
{

       /**
       * @ORM\ManyToMany(targetEntity="Application\Sonata\ClassificationBundle\Entity\Tag", cascade={"persist"})
       * @ORM\JoinTable(
       *      name= "Parametro_enviado_desde_clase",
       *      joinColumns={@ORM\JoinColumn(name=Parametro_enviado_desde_clase."_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
       * )
       */
   protected $tags;


    // .. varios metodos
}

The idea is that the trait defines the property $tags as a manytomany relation on another entity, but that the intermediate table has the name that I sent it from the trait.

If possible, this general type trait could be defined and I would only have to make a use indicating the parameter so that the doctrine will carry out its operations on the tables that interest me.

Does anyone know if something like this can be done?

The only solution I find is to remove all the code from the trait and put it in the entity printer, since I can not pass that parameter ...

    
asked by Jakala 05.10.2016 в 12:39
source

1 answer

0

I respond to myself. In the google group link they have given me the answer I was looking for ... The only thing what you have to do is remove the name field in the definition of the relationship in the trait:

use Doctrine \ ORM \ Mapping as ORM;

trait TagTrait
{

       /**
       * @ORM\ManyToMany(targetEntity="Application\Sonata\ClassificationBundle\Entity\Tag", cascade={"persist"})
       * @ORM\JoinTable(
       *      name= "Parametro_enviado_desde_clase",
       *      joinColumns={@ORM\JoinColumn(name=Parametro_enviado_desde_clase."_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
       * )
       */
   protected $tags;


    // .. varios metodos
}

With this, doctrine tries to create the printer_tag table:

CREATE TABLE printer_tag (printer_id INT NOT NULL, tag_id INT NOT NULL, INDEX IDX_FE129BD746EC494A (printer_id), INDEX IDX_FE129BD7BAD26311 (tag_id), PRIMARY KEY(printer_id, tag_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE printer_tag ADD CONSTRAINT FK_FE129BD746EC494A FOREIGN KEY (printer_id) REFERENCES printer (id);
ALTER TABLE printer_tag ADD CONSTRAINT FK_FE129BD7BAD26311 FOREIGN KEY (tag_id) REFERENCES classification__tag (id);

If the relationship was with another entity, eg Collection, doctrine tries to create the printer_collection table:

  

CREATE TABLE printer_collection (printer_id INT NOT NULL, tag_id INT   NOT NULL, INDEX IDX_8963B5A046EC494A (printer_id), INDEX   IDX_8963B5A0BAD26311 (tag_id), PRIMARY KEY (printer_id, tag_id))   DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;   ALTER TABLE printer_collection ADD CONSTRAINT FK_8963B5A046EC494A   FOREIGN KEY (printer_id) REFERENCES printer (id); ALTER TABLE   printer_collection ADD CONSTRAINT FK_8963B5A0BAD26311 FOREIGN KEY   (tag_id) REFERENCES classification__collection (id);

That is, omitting the name of doctrine, we try to create a table with the name of the class, _ and the name of the related entity:)

    
answered by 11.10.2016 в 12:06