php - Getting strange data after adding a foreign constraint in symfony 3.3 -


i have 2 simple entities in app:product , category related manytoone relation:

    <?php      namespace appbundle\entity;      use doctrine\orm\mapping orm;      /**      * product      *      * @orm\table(name="product")      * @orm\entity      */     class product     {         /**          * @var int          *          * @orm\column(name="id", type="integer")          * @orm\id          * @orm\generatedvalue(strategy="auto")          */         private $id;          /**          * @var string          *          * @orm\column(name="name", type="string", length=255)          */         private $name;          /**          * @var string          *          * @orm\column(name="description", type="text")          */         private $description;          /**          * @orm\manytoone(targetentity="category", inversedby="products")          * @orm\koincolumn(ondelete="cascade")          */         private $category;          /**          * id          *          * @return int          */         public function getid()         {             return $this->id;         }          /**          * set name          *          * @param string $name          *          * @return product          */         public function setname($name)         {             $this->name = $name;              return $this;         }          /**          * name          *          * @return string          */         public function getname()         {             return $this->name;         }          /**          * set description          *          * @param string $description          *          * @return product          */         public function setdescription($description)         {             $this->description = $description;              return $this;         }          /**          * description          *          * @return string          */         public function getdescription()         {             return $this->description;         }          /**          * @return int          */         public function getcategory()         {             return $this->category;         }          /**          * @param int $category          */         public function setcategory($category)         {             $this->category = $category;         }     }  /**  * category  *  * @orm\table(name="category")  * @orm\entity  */ class category {     /**      * @var int      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      *      * @orm\column(name="name", type="string", length=255)      */     private $name;       /**      * id      *      * @return int      */     public function getid()     {         return $this->id;     }      /**      * set name      *      * @param string $name      *      * @return category      */     public function setname($name)     {         $this->name = $name;          return $this;     }      /**      * name      *      * @return string      */     public function getname()     {         return $this->name;     } } 

the relation working fine.but there problem.when try fetch objects db

public function indexaction(){         $products = $this->getdoctrine()             ->getrepository('appbundle:product')             ->findall();          echo "<pre>";         print_r($products);die;     } 

it returns tone of code, like:

array (     [0] => appbundle\entity\product object         (             [id:appbundle\entity\product:private] => 1             [name:appbundle\entity\product:private] => samsung galaxy s5             [description:appbundle\entity\product:private] => stylish , elegant!             [category:appbundle\entity\product:private] => proxies\__cg__\appbundle\entity\category object                 (                     [__initializer__] => closure object                         (                             [static] => array                                 (                                     [entitypersister] => doctrine\orm\persisters\entity\basicentitypersister object                                         (                                             [class:protected] => doctrine\orm\mapping\classmetadata object                                                 (                                                     [name] => appbundle\entity\category                                                     [namespace] => appbundle\entity                                                     [rootentityname] => appbundle\entity\category                                                     [customgeneratordefinition] =>                                                      [customrepositoryclassname] =>                                                      [ismappedsuperclass] =>                                                      [isembeddedclass] =>                                                      [parentclasses] => array                                                         (                                                         )                                                      [subclasses] => array                                                         (                                                         )                                                      [embeddedclasses] => array                                                         (                                                         )                                                      [namedqueries] => array                                                         (                                                         )                                                      [namednativequeries] => array                                                         (                                                         )                                                      [sqlresultsetmappings] => array                                                         (                                                         ) 

and lot more, unpossible present here entirely. appreciate if suggests me how rid of unnecessary data , recieve object's properties. in advance.

that happens because doctrine default loads associations proxies references real entities. when try fetch association, doctrine fetches data database , hydrates it. commonly called lazy loading , useful performance improving.

to solve have set eager fetch mode on association:

/**  * @orm\manytoone(targetentity="category", inversedby="products", fetch="eager")  */ private $category; 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -