简体   繁体   中英

Symfony 5 referencing a field within a referenced table

The question hard to formulate in just one sentence when I don't have the exact terms ready for use, but I'm basically working on a Symfony 5 project that involves a MySQL database. I use Twig to allow for communication between my PHP controllers and my HTML interface. Until now, I've been doing just fine using simple references to entity fields in Twig, such as:

myEntity.someField

To get the value I needed. However, I currently need to reference a "nested" field like so:

myEntity1.myEntity2Field.someField

The "nesting" making a world of a difference between the two. I am now getting an error when trying to do this ( Impossible to access an attribute ("someField") on a string variable ("<value from entity2 field>") ), probably because my database is not organized correctly yet, from what I understand. Hopefully you could understand my difficulty. So, how can I tweak my database to allow this sort of double-referencing to take place?

Note : myEntity2Field refers to the name of a field from Entity2 that should serve as a reference to the entire Entity2 table, from which someField can then be extracted.

the error you are getting is because you are trying to access a value from a string or integer, not an Object.

myEntity.someField is the same as $myEntity->getSomeField() if you have a getter Method or $myEntity->someField if you are accessing the property directly.

If someField value is not an Object you can only access the value but if its an Object you will be able to access the other object properties and methods in this way.

To do so the best way is to use association(relation) between your entities spically if you have the association already in the database and if you build your relations right in your database so you jaut have to refernce this relation or association between your entities and doctrine will do the rest for you. as an example:

    <?php
/** @Entity */
class User
{
    // ...

    /**
     * @ManyToOne(targetEntity="Address")
     * @JoinColumn(name="address_id", referencedColumnName="id")
     */
    private $address;
}

/** @Entity */
class Address
{
    private $street;
    
    public function getStreet(){
     ....
    }

}

this example is a ManyToOne relation between the user and the addresse entity with the getters methos in the user entitiy you will be able to use the nesting when you access the user:

$user->getAddress()->getStreet()

as an example and this will work in twig also:

{{user.address.street}}

https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/association-mapping.html#many-to-one-unidirectional

the other way you can get the object inside your calss and map it with methods so you can access it as you want.

i hope i will be able to help you with this info.

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