简体   繁体   中英

doctrine2 attribute doesn't exist

this is my entity:

/**
* @ORM\Table(name="Animal")
* @ORM\HasLifecycleCallbacks 
*/
class Animal {

    /**
    * @var integer $id
    * 
    * @ORM\Column(name="id", type="integer", nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="IDENTITY")     
    */
    private $id;

    /**
    * @var localizedcontent $lctitle
    *
    * @ORM\ManyToOne(targetEntity="localizedcontent",fetch="EAGER", cascade={"persist"})
    * @ORM\JoinColumn(name="lcTitle", referencedColumnName="pkId", nullable=false)
    */
    private $lctitle;

    /**
    * @var localizedcontent $lcdescription
    *
    * @ORM\ManyToOne(targetEntity="localizedcontent",fetch="EAGER", cascade={"persist"})
    * @ORM\JoinColumn(name="lcDescription", referencedColumnName="pkId", nullable=false)
    */
    private $lcdescription;

    /**
    * @ORM\PostLoad
    */
    public function postLoad(){
      $lct = $this->lctitle;
      $lcd = $this->lcdescription;          
    }

This is my dql:

SELECT a,lct FROM Animal JOIN e.lctitle lct WHERE a.id=:id

When i'm starting xdebug, it tells me that lcdescription is a proxy object and lctitle doesn't exists. I don't know why. I think the postLoad event is too early because the localizedcontent isn't loaded at this moment, right? Is there an other listener for reading the value of lctitle in relation to the Animal Object?

Thanks

Doctrine always returns proxies. These classes inherit from the entity-classes. It might help if you declare your relations protected instead of private.

/**
* @var localizedcontent $lctitle
*
* @ORM\ManyToOne(targetEntity="localizedcontent",fetch="EAGER", cascade={"persist"})
* @ORM\JoinColumn(name="lcTitle", referencedColumnName="pkId", nullable=false)
*/
protected $lctitle;

or you could write a getter and call this one in your post-load function

public function getLctitle() {
    return $this->lctitle;
}

public function getLcdescription() {
    return $this->lcdescription;
}

/**
* @ORM\PostLoad
*/
public function postLoad(){
  $lct = $this->getLctitle();
  $lcd = $this->getLcdescription();          
}

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