php - Symfony2, how to display data from entities with ManyToMany associations -


i have multiple entities associated manytomany , manytoone associations , have problem displaying data more 1 level deep in associations

for example. there 3 entities connected. customer->address->country. in twig can display:

{{ customer.name }} // outputs name {{ customer.address.postcode }} // outputs post code address entity 

but this:

{{ customer.address.country.isocode2 }} //should output iso code country entity 

outputs 500 internal server error:

method "isocode2" object "doctrine\orm\persistentcollection" not exist in appbundle:tables:customers.html.twig @ line 43 

more info customer entity, address mapping

/**  * @var \appbundle\entity\address  *  * @orm\manytoone(targetentity="appbundle\entity\address")  * @orm\joincolumns({  *   @orm\joincolumn(name="defaultaddress_id", referencedcolumnname="id")  * })  */ private $address; 

country mapping in address entity

/**  * @var \doctrine\common\collections\collection  *  * @orm\manytomany(targetentity="appbundle\entity\country", inversedby="address")  * @orm\jointable(name="address_country",  *   joincolumns={  *     @orm\joincolumn(name="address_id", referencedcolumnname="id")  *   },  *   inversejoincolumns={  *     @orm\joincolumn(name="country_id", referencedcolumnname="id")  *   }  * )  */ private $country; 

country entity:

    /**  * @var string  *  * @orm\column(name="name", type="string", length=45, nullable=true)  */ private $name;  /**  * @var string  *  * @orm\column(name="isocode2", type="string", length=45, nullable=true)  */ private $isocode2;  /**  * @var string  *  * @orm\column(name="isocode3", type="string", length=45, nullable=true)  */ private $isocode3;  /**  * @var integer  *  * @orm\column(name="id", type="integer")  * @orm\id  * @orm\generatedvalue(strategy="identity")  */ private $id;  /**  * @var \doctrine\common\collections\collection  *  * @orm\manytomany(targetentity="appbundle\entity\address", mappedby="country")  */ private $address;  /**  * constructor  */ public function __construct() {     $this->address = new \doctrine\common\collections\arraycollection(); } 

if try this

{{ customer.address.country }} 

i getting

contexterrorexception: catchable fatal error: object of class doctrine\orm\persistentcollection not converted string in app/cache/dev/twig/1d/7c/3eec624c629866dcd530ea084487b111c573dbcba579efa7a6b315c46c7a.php line 120 

with manytomany association, address can have multiple countries. correct logic?

if is, have iterate on countries of address:

{% country in customer.address.country %}     {{ country.isocode2 }} {% endfor %} 

if address have 1 country, must use manytoone association. can use syntax:

{{ customer.address.country.isocode2 }} 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -