如何在Symfony中获取Object的属性

时间:2012-06-02 11:28:46

标签: php symfony doctrine-orm

嘿我如何从整个对象中获取任何特定属性。 我有这个查询

$portfolios = $this->getDoctrine()
    ->getRepository('MunichInnovationGroupBundle:PmPortfolios')
    ->findBy(array('user' => '1'));
foreach ($portfolios as $portfolio){
 if($portfolio.isDefault == true){
   $default_portfolio = $portfolio;
 }
  echo $portfolio.name;
}

完整的对象看起来像这样

MunichInnovationGroup\Bundle\Entity\PmPortfolios Object
(
[id:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => 991654b4-aa73-11e1-bdce-4a7b883b8e17
[portfolioName:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => Umair Portfolio 1
[description:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => Thsi is the description for Umairs portfolio 1
[permalink:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => premalink
[sharingCode:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => asdbnvg123dg
[shared:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => 
[sharedPortfolioCalls:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => 
[isDefault:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => 1
[user:MunichInnovationGroup\Bundle\Entity\PmPortfolios:private] => Proxies\MunichInnovationGroupBundleEntityUmUsersProxy Object

如何获取isDefault值?

提前致谢

1 个答案:

答案 0 :(得分:1)

如果您使用所有生成的set / get方法正确设置了实体,并且isDefault是私有属性(从var_dump看起来如此),您只需使用

if($portfolio->getIsDefault())

为了更好的方法命名,我会在实体中编写一个方法:

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

然后使用 if($portfolio->isDefault())