Doctrine 2 查询返回的对象没有属性或方法

Posted

技术标签:

【中文标题】Doctrine 2 查询返回的对象没有属性或方法【英文标题】:Doctrine 2 query returned object has no property or methods 【发布时间】:2011-07-08 00:52:02 【问题描述】:

我正在使用 Doctrine 2 和 Zend Framework 1.11。我已经建立了我的 Doctrine 集成,并且它似乎可以在其中工作,我能够获取并实例化一个实体管理器来使用。但是,我对控制器类中以下行的行为感到困惑:

$transfercurrency = $this->entityManager->getRepository('Gesmoney\Entity\Country')->findBy(array('countrycode' => $transfercountry));

当我执行 var_dump($transfercurrency) 时,我得到一个具有一大堆属性的对象,事实上,它对我来说看起来不正确。我试图把它贴在馅饼上,但它不会让我,因为它超过 100kb。因此,我只粘贴了大约四分之一的enter link description here。同样使用 Netbeans,返回的对象似乎没有属性或方法,因此当我调用代码完成时,我什么也得不到。当我执行 var_dump($transfercurrency[0]->id) 时,出现以下错误;

注意:未定义的属性: Gesmoney\Entity\Country::$property in /shared/www/dev.gesmoneylatest.com/library/Gesmoney/Entity/Country.php 第 55 行 NULL

这是一个相当长的帖子,但我希望有人能回答我的问题。谢谢。

控制器类

<?php

class Systemadmin_ExchangerateController extends Zend_Controller_Action

/**
 * @var Bisna\Application\Container\DoctrineContainer
 */
protected $doctrine;

/**
 * @var Doctrine\ORM\EntityManager
 */
protected $entityManager;

public function init()

    $this->doctrine = Zend_Registry::get('doctrine');
    $this->entityManager = $this->doctrine->getEntityManager();


public function indexAction()

    // action body



public function getexchangerateAction($transfercountry = 'GB') 

    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    $transfercurrency = $this->entityManager->getRepository('Gesmoney\Entity      \Country')->findBy(array('countrycode' => $transfercountry));
    var_dump($transfercurrency);



实体

<?php

 namespace Gesmoney\Entity;
 /**
 * @Entity @Table(name="countries")
 */
 class Country 
/**
 * @Id @Column(type="integer", length=3, nullable=false) 
 * @GeneratedValue(strategy="IDENTITY")
 * @var integer
 * 
 */
private $id;

/**
 * @Column(type="string", length=25)
 * @var string
 */
private $countryname;

/**
 * @Column(type="datetime")
 * @var string
 */
private $lastupdate;

/**
 * @Column(type="string", length=2)
 * @var string
 */
private $countrycode;

/**
 * @Column(type="string", length=20)
 * @var string
 */
private $countrycurrency;

/**
 * @Column(type="string", length=3)
 * @var string
 */
private $currencycode;


/**
 * @param \Doctrine\Common\Collections\Collection $property
 * @OneToMany(targetEntity="Region", mappedBy="country", cascade="persist", "remove")
 */
private $region;


public function __get($property) 
    return $this->property;


public function __set($property, $value) 
    $this->property = $value;



Application.ini 摘录

;; added for Doctrine2 Integration
pluginPaths.Bisna_Application_Resource = "Bisna/Application/Resource"

; ------------------------------------------------------------------------------
; Doctrine Cache Configuration
; ------------------------------------------------------------------------------

; Points to default cache instance to be used. Optional is only one cache is defined
resources.doctrine.cache.defaultCacheInstance = default

; Cache Instance configuration for "default" cache
resources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\ArrayCache"
resources.doctrine.cache.instances.default.namespace    = "Application_"

; ------------------------------------------------------------------------------
; Doctrine DBAL Configuration
; ------------------------------------------------------------------------------

; Points to default connection to be used. Optional if only one connection is defined
resources.doctrine.dbal.defaultConnection = default

; Database configuration
;resources.doctrine.dbal.connections.default.parameters.wrapperClass = ""
resources.doctrine.dbal.connections.default.parameters.driver   = "pdo_mysql"
resources.doctrine.dbal.connections.default.parameters.dbname   = "zzzzz"
resources.doctrine.dbal.connections.default.parameters.host = "localhost"
resources.doctrine.dbal.connections.default.parameters.port = zzzz
resources.doctrine.dbal.connections.default.parameters.user = "root"
resources.doctrine.dbal.connections.default.parameters.password = ""


; ------------------------------------------------------------------------------
; Doctrine ORM Configuration
; ------------------------------------------------------------------------------

; Points to default EntityManager to be used. Optional if only one EntityManager is defined
resources.doctrine.orm.defaultEntityManager = default

; EntityManager configuration for "default" manager
resources.doctrine.orm.entityManagers.default.connection     = default
resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = true
resources.doctrine.orm.entityManagers.default.proxy.namespace           = "Gesmoney\Entity\Proxy"
resources.doctrine.orm.entityManagers.default.proxy.dir                 = APPLICATION_PATH "/../library/Gesmoney/Entity/Proxy"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.adapterClass          = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingNamespace      = "Gesmoney\Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingDirs[]         = APPLICATION_PATH "/../library/Gesmoney/Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderCache = default

【问题讨论】:

【参考方案1】:

您忘记了__get__set 属性上的$

public function __get($property) 
    return $this->$property;


public function __set($property, $value) 
    $this->$property = $value;

【讨论】:

以上是关于Doctrine 2 查询返回的对象没有属性或方法的主要内容,如果未能解决你的问题,请参考以下文章

Doctrine queryBuilder:返回对象而不是数组

使用 Native SQL 的 Doctrine 查询总是返回空数组

更新或插入方法中的 Doctrine 2 DBAL 表达式

通过findOneBy方法获取的Doctrine实体的行为与通常类似,但会触发致命错误

如何在 Doctrine2 的查询结果中获取 Collection

Hibernate框架学习——查询优化