简体   繁体   中英

Symfony/Doctrine 2 ManyToOne relationship, ORM does not retain set value for column, error ensues

I'm creating a ManyToOne relationship in table B, to one entry in table A.

I am using a particular key_id parameter, which is set in the controller, before being flushed to the db. My problem is that the SQL created by the ORM nulls the value for this key, for some reason. Before the ManyToOne relationship the same controller had no issues setting and persisting the key_id value.

Here is my annotation on Table B

    /**
     * @ORM\Column(type="string", length=63)
     * @Filter\Trim()
     * @Filter\StripNewlines()
     */
    private table_b_id

    /**
     * @ORM\ManyToOne(targetEntity="TableA", inversedBy="table_b_entries", fetch="EAGER")
     * @ORM\JoinColumn(name="table_b_id", referencedColumnName="table_a_id")
     */
    private $reference_to_table_a;

And Table A

    /**
     * Constructor
     */
    function __construct()
    {
        $this->table_b_entries = new ArrayCollection();
    }

    /**
     * @ORM\OneToMany(targetEntity="TableB", mappedBy="table_b_id")
     */
    private $table_b_entries;

The error that I get is:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'table_b_id' cannot be null

Although I DO set it in the controller before flushing to db, and I have verified the object that I flush that it contains the value, however the SQL does NOT contain this value anymore... I'm not sure where or why it gets lost...

Update

The error described here is because I was not setting the reference object, but trying to set the reference column id manually on Table B. I Should have first fetched the Table A, and set that object on Table B, through the reference setter...

The initial question was answered by: Doctrine 2 category_id always NULL when set ManyToOne relationship

Now I'm dealing with another error:

ContextErrorException: Notice: Undefined index: table_a_id in /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 628

You don't need two fields for ManyToOne relation. This is sufficient:

class B
{
    /**
     * @ORM\ManyToOne(targetEntity="A", inversedBy="entitiesB")
     * @ORM\JoinColumn(name="name_of_column_on_table_b", referencedColumnName="column_from_table_a_to_be_referenced")
     */
    private $entityA;

    // ...
}

class A
{
    /**
     * @ORM\OneToMany(targetEntity="B", mappedBy="entityA")
     */
    private $entitiesB;

    // ...
}

Doctrine will automatically create field called entityA_id on your B table, index it and create foreign key between the two.

When you have instance of entity B , you can retrieve it's A record relation Id by just calling:

$b->getEntityA()->getId()

People commonly think that this will trigger another query, but it will not, it will just read entityA_id from B record, for exaplanation see here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM