DDC-1176: Error on proxy loading with foreign Key #1779

Closed
doctrinebot opened this Issue May 27, 2011 · 6 comments

2 participants

@doctrinebot

Jira issue originally created by user benoit:

Look at this thread:http://groups.google.com/group/doctrine-user/browse_thread/thread/2a03a6aea43428d6

This entity class configuration:

class IndividualsStatus
{
    /****
     * @var Individuals
     *
     * @Id
     * @ManyToOne(targetEntity="Individuals", inversedBy="diagnosers", cascade={"persist","refresh","remove"})
     * @JoinColumns({
     *   @JoinColumn(name="individuals_id", referencedColumnName="id")
     * })
     */
    private $individuals;

    /****
     * @var Diagnosers
     *
     * @Id
     * @ManyToOne(targetEntity="Diagnosers", inversedBy="individuals", cascade={"persist","refresh"})
     * @JoinColumns({
     *   @JoinColumn(name="diagnosers_id", referencedColumnName="id")
     * })
     */
    private $diagnosers;

    /****
    * @var smallint $status
    *
    * @Column(name="status_individuals", type="smallint")
    */
    private $statusIndividuals;

    [...]
}

class Individuals
{
    /****
     * @var Users
     *
     * @OneToOne(targetEntity="Users", cascade={"persist","detach","merge","refresh","remove"}, orphanRemoval=true)
     * @JoinColumns({
     *   @JoinColumn(name="users_id", referencedColumnName="id", unique=true)
     * })
     */
    private $users;

    /****
     * @var Advertisements
     *
     * @OneToMany(targetEntity="Advertisements", mappedBy="individuals")
     */
    private $advertisements;

    /****
     * @var IndividualsStatus
     *
     * @OneToMany(targetEntity="IndividualsStatus", mappedBy="individuals", cascade={"refresh", "remove", "persist"})
     * @joinColumns({
     *   @JoinColumn(name="individuals_id", referencedColumnName="id")
     * })
     */
    private $diagnosers;

  [...]
}

class Diagnosers
{
    /****
     * @var Users
     *
     * @OneToOne(targetEntity="Users", cascade={"persist","remove","detach","merge","refresh"})
     * @JoinColumns({
     *   @JoinColumn(name="users_id", referencedColumnName="id", unique=true)
     * })
     */
    private $users;

    /****
     * @var Pros
     *
     * @OneToOne(targetEntity="Pros", cascade={"persist","remove","detach","merge","refresh"})
     * @JoinColumns({
     *   @JoinColumn(name="pros_id", referencedColumnName="id", unique=true)
     * })
     */
    private $pros;

    /****
     * @var Reports
     *
     * @OneToMany(targetEntity="Reports", mappedBy="diagnosers", cascade={"refresh", "remove"})
     */
    private $reports;

    /****
     * @var IndividualsStatus
     *
     * @OneToMany(targetEntity="IndividualsStatus", mappedBy="diagnosers", cascade={"refresh", "remove", "persist"})
     * @joinColumns({
     *   @JoinColumn(name="diagnosers_id", referencedColumnName="id")
     * })
     */
    private $individuals;
}

Note that in individuals, the relation called "Diagnosers" refered to IndividualsStatus entity.
Note that in diagnosers, the relation called "Individuals" refered to IndividualsStatus entity.

When I execute this DQL request:
   $qb->select(array('i', 'iss', 'u')) 
           ->from('IndividualsStatus', 'iss') 
           ->join('iss.individuals', 'i') 
           ->join('i.users', 'u') 
           ->where('iss.diagnosers = ?1') 
           ->andWhere('iss.individuals = ?2') 
           ->setParameters(array(1 => $idDiag, 2 => $id)); 

I retrieve an IndividualsStatus entity:

object(stdClass)[149] 
  public '*_CLASS_*' => string 'IndividualsStatus' (length=37) 
  public 'individuals' => 
    object(stdClass)[148] 
      public '*_CLASS_*' => string 'Individuals' (length=31) 
      public 'reference' => string '451' (length=3) 
      public 'maxAdvertisements' => null 
      public 'role' => int 2 
      public 'id' => int 8 
      public 'users' => string 'Users' (length=25) 
      public 'advertisements' => string 'Array(0)' (length=8) 
      public 'diagnosers' => string 'Array(1)' (length=8) 
      public 'ownedReports' => string 'Array(0)' (length=8) 
      public 'reports' => string 'Array(0)' (length=8) 
      public '_em' => null 
  public 'diagnosers' => 
    object(stdClass)[154] 
      public '*_CLASS_*' => string 'Proxies 
\OpengroupeEntitiesDiagnosersProxy' (length=61) 
      public 'id' => string '19' (length=2) 
  public 'statusIndividuals' => int 1 
  public '_em' => null 

Now i want to access to the "Diagnosers" relation of the Individuals:
\Doctrine\Common\Util\Debug::dump($individualsStatus->getIndividuals()->getDiagnosers());

In my database, this individuals got 2 diagnosers attached, but only 1 is dumped.
For getting the 2, i have to select the Diagnosers relation into my Dql request:

   $qb->select(array('i', 'iss', 'u', 'd')) 
           ->from('IndividualsStatus', 'iss') 
           ->join('iss.individuals', 'i') 
           ->join('i.diagnosers', 'd') 
           ->join('i.users', 'u') 
           ->where('iss.diagnosers = ?1') 
           ->andWhere('iss.individuals = ?2') 
           ->setParameters(array(1 => $idDiag, 2 => $id)); 

Then if i do \Doctrine\Common\Util\Debug::dump($individualsStatus->getIndividuals()->getDiagnosers())
I have my 2 diagnosers display.

@doctrinebot

Comment created by benoit:

I test something more simple with the same entiies:

$diagnosers = $em->getRepository('Diagnosers')->find(19);
$listDiag = $diagnosers->getIndividuals();

In my database, the Diagnoser(19) got 20 individualsStatus(remember the relation called "Individuals" in "Diagnosers" return "IndividualsStatus" entities)
So this function return only 1 individualStatus(minor id in bdd).

I am completly blocked in my developpement. Did my entity annotation for the relation is bad ?

@doctrinebot

Comment created by @beberlei:

Formatting

@doctrinebot

Comment created by @beberlei:

This issue is related to foreign key identifiers, not using them will solve the problem. Your annotations are good.

However I fixed an issue of this kind some weeks ago. Are you using the Download of 2.1 Beta? Can you try to update to the latest version on Git?

@doctrinebot

Comment created by @beberlei:

Yes just seeing i fixed the bug AFTER releasing BEta 1. Please upgrade and verify!

@doctrinebot

Comment created by @beberlei:

Resolved

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 2.1 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment