2016-06-29 13 views
1

プロジェクトでDoctrineを使いたかったのですが、Entity Managerを使用できません。 私はentites、リポジトリ、設定ファイルとdbconnectを作成しましたが、正しく行われていないようです。 このコードを確認してください。たぶん私は本当に小さいものを見逃しているかもしれない。 (それはinit.phpにブートストラップされる)doctrineのエンティティマネージャを有効にできません

私のするDBConnectファイル:

<?php 
namespace Projekt\Config; 

use Doctrine\ORM\Tools\Setup; 
use Doctrine\ORM\EntityManager; 

$paths = array("Entity"); 
$isDevMode = false; 

// the connection configuration 
$dbParams = array(
'driver' => 'pdo_mysql', 
'user'  => 'root', 
'password' => '', 
'dbname' => 'projekt', 
); 

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false); 
$em = EntityManager::create($dbParams, $config); 

マイリポジトリ例:今すぐ

<?php 

namespace Projekt\Repository; 

use Doctrine\ORM\EntityRepository; 

/** 
* Message 
* 
* This class was generated by the Doctrine ORM. Add your own custom 
* repository methods below. 
*/ 
class Message extends EntityRepository 
{ 
    public function getMessage($id) 
    { 
     $message = $this->find($id); 
     return $message; 

    } 
    public function getAllMessages() 
    { 

    } 
    public function createMessage() 
    { 

    } 
    public function updateMessage() 
    { 

    } 
    public function deleteMessage() 
    { 

    } 
} 

私はデフォルトまたはカスタムリポジトリ方法Iにアクセスしようとしていますこのエラーが出る:EntityRepository.phpで

Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), 
called in F:\xampp\htdocs\mvc\app\Controllers\Messages.php 
on line 15 and defined in F:\xampp\htdocs\mvc\vendor\doctrine\orm\lib\Doctrine\ORM\EntityRepository.php on line 64 

ライン64は宣言する__construct関数であり、 EntityManagerのが、それが正しく動作していないようです:私が気づい

public function __construct($em, Mapping\ClassMetadata $class) 
{ 
    $this->_entityName = $class->name; 
    $this->_em   = $em; 
    $this->_class  = $class; 
} 

答えて

0

2つのこと:

  1. あなたのパスが相対的です。私は確かに私は常にEntityフォルダへの完全なパスを使用します。あなたはそれを簡単に達成するために__DIR__を使うことができます。あなたの名前空間に応じてそれは見えるはずです:

    $paths = array(__DIR__ . "/../Repository");

Doctrineはエンティティとリポジトリの場所を知る必要があります。あなたの名前空間に応じて、私はあなたのリポジトリファイルが "エンティティ"ではなく "リポジトリ"という名前のフォルダに存在すると思います。

  1. エンティティクラスを正しく定義しましたか?あなたのリポジトリクラスは私にはうまく見えますが、有効なEntityクラスを持っている場合にのみ機能します。

リポジトリの名前を「メッセージ」にしないでください。エンティティの名前は "Message"、リポジトリの名前は "MessageRepository"にする必要があります。

関連する問題