Doctrine arraycollection保持为null

时间:2018-01-02 22:52:57

标签: php doctrine-orm doctrine

嘿我只是尝试了php的教义而且我的$ matches arraycollection遇到了问题,当我在__construct中设置它有效但后来它只是null而我似乎无法弄清楚为什么,其他arraycollections工作完美

<?php
// src/Player.php
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @Entity @Table(name="teams")
 **/
class Team
{
    /**
     * @Id @Column(type="integer") @GeneratedValue
     **/
    protected $id;
    /**
     * @Column(type="string")
     **/
    protected $name;
    /**
     * @OneToMany(targetEntity="Player", mappedBy="team")
     **/
    protected $players;
    /**
     * @OneToMany(targetEntity="Goal",mappedBy="againstTeam")
     **/
    protected $goalsAgainst;
    /**
     * @Column(type="string")
     **/
    protected $color;
    /**
     * ManyToMany(targetEntity="Match", mappedBy="teams")
     **/
    protected $matches;

    public function __construct()
    {
        $this->matches = new ArrayCollection();
        $this->players = new ArrayCollection();
        $this->goalsAgainst = new ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getMatches() {
        return $this->matches;
    }

    public function addMatch(Match $match) {
        $this->matches->add($match);       
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setColor($color) {
        $this->color = $color;
    }

    public function addGoalsAgainst(Goal $goal) {
        $this->goalsAgainst->add($goal);
        $goal->setAgainstTeam($this);
    }

    public function getJson() {
        return array(
            'Id'=>$this->id,
            'Name'=>$this->name,
            'Players'=>null,
            'GoalsAgainst'=>null,
            'Color'=>$this->color,
            'Matches'=>null
        );
    }
}
?>

如果我向addMatches(匹配$匹配)添加nullcheck,如

public function addMatch(Match $match) {
    if ($this->matches == null) {
        $this->matches = new ArrayCollection();
    }
    $this->matches->add($match);
}

然后添加了匹配,但如果我在下一行使用$ team-&gt; getMatches(),则$ matches再次为null

0 个答案:

没有答案