开发者

Not able to call functions on extracted entities with Doctrine 2

I'm new to PHP and also with Doctrine. (Worked before with Hibernate ORM implementation).

My problem is that after I fetch a record from my database by the entityManager, I can't access the object methods at all. Below are some code snippets:

Entity manager creation:

    $classLoader = new \Doctrine\Common\ClassLoader('entities');
    $classLoader->register();

    $config = new Configuration();
    $cache = new ArrayCache();
    $config->setMetadataCacheImpl($cache);

    $driverImpl = $config->newDefaultAnnotationDriver('entities');
    $driverImpl->getAllClassNames(); 
    $config->setMetadataDriverImpl($driverImpl);
    $config->setQueryCacheImpl($cache);
    $config->setProxyDir('proxies');
    $config->setProxyNamespace('proxies\namespaces');

    $config->setAutoGenerateProxyClasses(true);
    $em = EntityManager::create(getConnOptions(), $config);

it works fine!

Here is my Entity class :

namespace entities\positions;
/**
 * Positions
 *
 * @Table(name="positions")
 * @Entity
 */

class Positions
{
 /**
     * @var bigint $id
     *
     * @Column(name="id", type="bigint", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
private $id;

/**
 * @var string $notes
 *
 * @Column(name="notes", type="string", length=255, nullable=true)
 */
private $notes;

/**
 * @var integer $number
 *
 * @Column(name="number", type="integer", nullable=true)
 */
public $number;

/**
 * @var 开发者_如何学CVolumes
 *
 * @ManyToOne(targetEntity="Volumes")
 * @JoinColumns({
 *   @JoinColumn(name="volume_id", referencedColumnName="id")
 * })
 */
private $volume;

public function getNumber() {

    return $this->number;

}

}

and here is the code that generates error:


$found = $this->em->find('Positions', 1);
echo $found->getNumber();

the error that I get is the following:

Fatal error: Call to undefined method Positions::getNumber() in /var/www/php-test/business/Test.php on line 30

Can you suggest me how to fix it? Thanks.

N.B. It gives me the same error if I try to call : $found->number, that I have made public for this reason.


The problem is due to the fact that I was declared the namespace in entities. This was the reason for what I got this error. If you have entities under entities/ directory scattered in it's own directory, you need to put this paths in the driver creation array configuration :

$driverImpl = $config->newDefaultAnnotationDriver(array("entities", "entities/dir1", "entities/dir2"));

That does the trick.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜