T
T
templton19822016-08-29 22:49:23
symfony
templton1982, 2016-08-29 22:49:23

The repository stopped working, the entity constructor is not called?

Dear Toasters!
Help to solve a problem - the third day I fight! Started learning symfony. The task is as follows:
Entity 1 - sad (id, title)
Entity 2 - me(id, val)
Relationship: for each entity 1 there are many entities 2.
I do this:
Entity 1 - sad

<?php
namespace Stocker\StockBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="Stocker\StockBundle\Entity\Repository\SadRepository")
 * @ORM\Table(name="sad")
 */
class Sad
{
    public function __construct()
    {
        $this->mes=new ArrayCollection();
    }
    

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $title;
    
    /**
     * @ORM\OneToMany(targetEntity="Me", mappedBy="Sad")
     */
    private $mes;

Essence 2 - me
<?php
namespace Stocker\StockBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Stocker\StockBundle\Entity\Repository\Sad1Repository")
 * @ORM\Table(name="me")
 */
class Me
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $val;
    
    /**
     * @ORM\ManyToOne(targetEntity="Sad", inversedBy="mes")
     * @ORM\JoinColumn(name="sad_id", referencedColumnName="id")
     */
    private $sad;

sadRepository:
<?php

namespace Stocker\StockBundle\Entity\Repository;

/**
 * SadRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SadRepository extends \Doctrine\ORM\EntityRepository
{
    public function getSad(){
        $qb=$this->createQueryBuilder('f')
                ->select('f');
        return $qb->getQuery()->getResult();
    }
}

Next:
php app/console doctrine:generate:entities Stocker\StockBundle Create getters ,
seters
php app/console doctrine:schema:update --force Create
tables in the database
from the parent table in a drop-down list. That is, the connections are created correctly.
Next, in the controller, I call the desired repository. In reply:
array:2 [▼
  0 => Sad {#458 ▼
    #id: 1
    #title: "asdf"
    -mes: PersistentCollection {#475 ▼
      -snapshot: []
      -owner: Sad {#458}
      -association: array:15 [ …15]
      -em: EntityManager {#106 …11}
      -backRefFieldName: "Sad"
      -typeClass: ClassMetadata {#456 …}
      -isDirty: false
      #collection: ArrayCollection {#484 ▼
       <b> -elements: []</b>
      }
     <b> #initialized: false</b>
    }
  }
  1 => Sad {#482 ▶}
]

Problem: Collection of objects is not created.
Prehistory. Initially, I did everything the same way, everything worked, everything is fine. I started to fence many-to-many, create an intermediate table, etc. (Now I have already understood that the ORM itself will create intermediate tables). As a result of these operations, this algorithm stopped working, referring to #initialized: false
If you put die in the constructor of Entity 1, then nothing happens. That is, obviously the entity constructor has ceased to be called.
I tried to write everything in a new way, delete tables from the database, create new entities, delete and create old ones. In short, no matter how I try to repeat the algorithm, the collections are still not filled.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question