Php 当您使用get magic方法时,条令不会为null值返回null

Php 当您使用get magic方法时,条令不会为null值返回null,php,null,compare,symfony-1.4,doctrine-1.2,Php,Null,Compare,Symfony 1.4,Doctrine 1.2,我有一个配置了条令的Symfony应用程序,并且我设计了两个模型之间的一对多关系:项目属于客户,它是sfGuardUser的别名 假设在某些情况下,商品没有任何客户。为了验证这一点,我尝试进行以下比较: $customer = $this->getCustomer(); if ( $customer ) { return $customer->getNbInvoices(); } else { return 'n/a'; } 但是,$this->getCustomer()不

我有一个配置了条令的Symfony应用程序,并且我设计了两个模型之间的一对多关系:
项目属于
客户
,它是
sfGuardUser
的别名

假设在某些情况下,商品没有任何客户。为了验证这一点,我尝试进行以下比较:

$customer = $this->getCustomer();
if ( $customer ) {
  return $customer->getNbInvoices();
}
else {
  return 'n/a';
}
但是,
$this->getCustomer()
不返回要比较的
null
或任何其他“false”值,并且外键在数据库中设置为null


如何比较数据库中未存储实际值的对象?

我认为$this->getCustomer()返回customer记录的空白实例。您可以测试客户是否有ID,也可以使用doctrine\u record class exists()的方法:

怎么样

if ($this->relatedExists('Customer') {
   return $this['Customer']->getNbInvoices();
} else {
  return 'n/a';
}
if ($this->relatedExists('Customer') {
   return $this['Customer']->getNbInvoices();
} else {
  return 'n/a';
}