开发者

Doctrine does not return null for NULL values when you use get magic methods

开发者 https://www.devze.com 2023-04-12 00:05 出处:网络
I have a Symfony application configured with Doctrine, and I have designed a one-to-many relationship between two models: a Item belongs to a Customer, which is an alias for sfGuardUser.

I have a Symfony application configured with Doctrine, and I have designed a one-to-many relationship between two models: a Item belongs to a Customer, which is an alias for sfGuardUser.

Let's say that there are certain situations where an item does not have any customer. To test this I'm trying to make this comparison:

$customer = $this->getCustomer();
if ( $customer ) {
  return $customer->getNbInvoices();
}
else {
  return 'n/a';
}

However $this->getCustomer() does not return null or any other 'false' value to compare with, and the foreign key is set to NULL in the da开发者_高级运维tabase.

How can I compare an object that does not store an actual value in the database?


I think that $this->getCustomer() return a blank instance of customer doctrine_record. You can test if the customer has an ID, or you can use a method of doctrine_record class exists() :

if($customer->exists()){
 code...
}

http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_record.html#exists%28%29


how about

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号