1

リストメソッドを実行するために、Doctrine を使用して最初の DAO を実行しようとしています。私のエンティティクラスは /src/model/dto フォルダーの Canales.php です

namespace model\dto;

use Doctrine\ORM\Mapping as ORM;

/**
 * Canales
 *
 * @ORM\Table(name="Canales")
 * @ORM\Entity
 */
class Canales
{
    /**
     * @var string
     *
     * @ORM\Column(name="Nombre", type="string", length=255, nullable=false)
     */
    private $nombre;

    /**
     * @var integer
     *
     * @ORM\Column(name="Id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * Class Constructor
     * @param    $nombre   
     * @param    $id   
     */
    public function __construct($nombre, $id)
    {
        $this->nombre = $nombre;
        $this->url = $url;
        $this->imagen = $imagen;
        $this->ingles = $ingles;
        $this->id = $id;
    }



    /**
     * Set nombre
     *
     * @param string $nombre
     * @return Canales
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;

        return $this;
    }

    /**
     * Get nombre
     *
     * @return string 
     */
    public function getNombre()
    {
        return $this->nombre;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

このクラスは、これらの行を使用して生成されました

vendor/bin/doctrine orm:convert-mapping --from-database xml config/xml/

vendor/bin/doctrine orm:generate-entities --generate-annotations=true --regenerate-entities=true src/

私の Dao クラス (/src/model/dao フォルダー内)

class daoGenerico{
private $entityManager;

/**
 * Class Constructor
 * @param    $entityManager   
 */
public function __construct()
{
    require_once("bootstrap.php");
    $this->entityManager = $entityManager;
}


/**
 * @return mixed
 */
public function getEntityManager()
{
    return $this->entityManager;
}

function showAction(){
    $repository = $this->getEntityManager()->getRepository('model\dto\Canales');
        //->find($id);
    echo "..";
    $productos = $repository->findAll();
    echo "OK";
    if (!$productos) {
        throw $this->createNotFoundException(
            'No product found'
        );
    }
    else{
        var_dump($productos);
    }
}   

}

私のbootstrap.php

// bootstrap.php

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

require_once "vendor/autoload.php";

// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/model/dto"), $isDevMode);

// database configuration parameters
$conn = array(
    'driver'   => 'pdo_mysql',
    'host'     => 'localhost',
    'user'     => 'root',
    'password' => 'root',
    'dbname'   => 'yoga',
    'charset'  => 'UTF8',
);

// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);

そして私のtest.phpファイル

include_once('./src/model/dao/daoGenerico.php');

$daoGenerico = new daoGenerico();
$daoGenerico->showAction();

composer.json はこれです

{
   "require": {
        "doctrine/orm": "2.4.*"
    },
    "autoload": {
        "psr-0": {"": "src/"}
    }
}

実行すると、このエラーが発生しました

致命的なエラー:「クラス "model\dto\Canales" は有効なエンティティまたはマップされたスーパー クラスではありません」というメッセージを含むキャッチされない例外「Doctrine\ORM\Mapping\MappingException」。/Applications/MAMP/htdocs/yoga/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:336 内

私はstackoverflowで同様の質問をいくつか試しましたが、結果はありませんでした。アイデアやアドバイスはありますか?

4

1 に答える 1

1

問題を解決しました。この行のbootstrap.phpを変更する必要がありました

$config = Setup::createAnnotationMetadataConfiguration(array(DIR."/src‌​/model/dto"), $isDevMode, null, null, false); 
于 2017-07-20T09:12:23.470 に答える