添加新关联时,Doctrine 2致命错误

时间:2018-02-15 13:37:33

标签: php doctrine-orm

我有两个对象类,Session和Speaker,具有多对多关联。当我想在会话中添加一个发言者时,我使用以下代码:

df = pd.read_csv(url.format(s=Cik), index_col=[1]).iloc[:, 1:].T
print (df)

indicator_id       Assets  DividendsCommonStockCash  EarningsPerShareDiluted  \
2011-06-30    186360000.0                       NaN                     0.15   
2011-09-30    182254000.0                       NaN                     0.23   
2012-03-31    184765000.0                       NaN                     0.18   
2012-06-30    203554000.0                       NaN                     0.38   
2012-09-30    196254000.0                       NaN                     0.24   
2012-12-31    193493000.0                       NaN                     0.31   
2013-03-31    194473000.0                       NaN                     0.29   
2013-06-30    221214000.0                       NaN                     0.33   
2013-09-30    220138000.0                       NaN                     0.28   
2013-12-31    215444000.0                       NaN                     0.11   
2014-03-31    228719000.0                       NaN                     0.26   
2014-06-30    241652000.0                       NaN                     0.20   
2014-09-30    247509000.0                       NaN                     0.22   
2014-12-31    233117000.0                       NaN                     0.12   
2015-03-31    236759000.0                       NaN                     0.15   
2015-06-30    250012000.0                       NaN                     0.20   
2015-09-30    255098000.0                       NaN                     0.24   
2015-12-31    232854000.0                       NaN                     0.25   
2016-03-31    236669000.0                       0.0                     0.20   
2016-06-30    257527000.0                       NaN                     0.27   
2016-09-30    257277000.0                       NaN                     0.29   
2016-12-31    256530000.0                       NaN                     0.24   
2017-03-31    265283000.0                       NaN                     0.19   
2017-06-30    285011000.0                       NaN                     0.26   
2017-09-30    303138000.0                       NaN                     0.28   

indicator_id  NetIncomeLoss  
2011-06-30        3839000.0  
2011-09-30        5626000.0  
2012-03-31        4567000.0  
2012-06-30        9297000.0  
2012-09-30        6007000.0  
2012-12-31        7578000.0  
2013-03-31        7140000.0  
2013-06-30       12119000.0  
2013-09-30       10522000.0  
2013-12-31        7766000.0  
2014-03-31        9822000.0  
2014-06-30       11363000.0  
2014-09-30       12440000.0  
2014-12-31       10533000.0  
2015-03-31        8399000.0  
2015-06-30       11130000.0  
2015-09-30       13251000.0  
2015-12-31       12948000.0  
2016-03-31       10806000.0  
2016-06-30       14341000.0  
2016-09-30       15682000.0  
2016-12-31       12547000.0  
2017-03-31       10217000.0  
2017-06-30       13794000.0  
2017-09-30       14717000.0  

这适用于现有会话。现在,当我实例化新会话然后尝试以相同方式添加扬声器时,我收到此错误:

109   $session->getSpeakers()->add($speaker);
110   $speaker->getSessions()->add($session);

我认为这是因为新会话还没有任何发言者因此在我呼叫Fatal error: Uncaught Error: Call to a member function add() on null in C:\###\api\sessions\update.php:109 Stack trace: #0 {main} thrown in C:\###\api\sessions\update.php on line 109 时返回null。

但是,我无法在Doctrine Docs中找到有关如何规避这一点的任何内容。我错过了某个地方的一步吗?

1 个答案:

答案 0 :(得分:2)

你在构造函数中有initialized your collection吗?

// missing parts skipped for brevity
public function __construct(...)
{
    // ...
    $this->speakers = new ArrayCollection();
    $this->sessions = new ArrayCollection();
}

无论如何,您的代码质量很差SOLID。你应该有一些叫做的方法:

addSpeaker(Speaker $speaker)
addSession(Session $session)

所以只有对象本身知道如何处理集合。目前,您在课堂外泄漏其内部详细信息(SRPlaw of Demeter违规),因为它会及时降低代码的可维护性。