简体   繁体   中英

OneToMany Relationship with Symfony2 and Doctrine

I keep getting a null result when I try to retrieve the many side of my bi-directional relation.

In My Tool Entity, I have

public function __construct()
{
    $this->instances = new ArrayCollection;
}


/**
 * @var integer
 */
private $toolId;


/* @var ArrayCollection things
*
* @ORM\OneToMany(targetEntity="Instance", mappedBy="tool")
*/
private $instances;




public function getInstances(){
    print "In the get";
    var_dump($this->instances);
    return $this->instances;
}

Then on the other side, I have

class Instance {
/**
 * @var integer
 */
private $instanceId;

/**
 * @var string
 */
private $serialnumber;

/**
 * @var boolean
 */
private $inServiceFlag = '1';

 /**
 * @ORM\ManyToOne(targetEntity="Tool", inversedBy="instances")
 * @ORM\JoinColumn(name="tool_id", referencedColumnName="tool_id")
 */
private $tool;

When I view an instance, I can see the tool, but when I view the tool I can see an array of the instances. I always get a null value.

What am I missing?

Are you sure that your @JoinColumn annotation is correct?

In the annotation, I see referencedColumnName="tool_id" , but on your Tool entity, I see private $toolId;

Everything else looks correct to me, according to the docs (assuming these entities are in the same namespace, of course)

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