doctrine $ entityManager-> getRepository

时间:2017-07-07 22:31:15

标签: php doctrine-orm orm

我是php和doctrine的新手。我正在尝试创建一个小项目,当我尝试使用findAll方法时遇到问题。执行时没有显示错误。

这是我的Canales.php类(在/src/model/dto文件夹中)

<?php

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 string
     *
     * @ORM\Column(name="URL", type="string", length=255, nullable=false)
     */
    private $url;

    /**
     * @var string
     *
     * @ORM\Column(name="imagen", type="string", length=255, nullable=false)
     */
    private $imagen;

    /**
     * @var boolean
     *
     * @ORM\Column(name="Ingles", type="boolean", nullable=false)
     */
    private $ingles;

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

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

    /**
     * Class Constructor
     * @param    $nombre   
     * @param    $url   
     * @param    $imagen   
     * @param    $ingles   
     * @param    $id   
     */
    public function __construct($nombre, $url, $imagen, $ingles, $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;
    }

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set ingles
     *
     * @param boolean $ingles
     * @return Canales
     */
    public function setIngles($ingles)
    {
        $this->ingles = $ingles;

        return $this;
    }

    /**
     * Get ingles
     *
     * @return boolean 
     */
    public function getIngles()
    {
        return $this->ingles;
    }

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

我的composer.json

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

我的DaoGenerico班

    require_once("bootstrap.php");
    function showAction(){
    $repository = $entityManager->getRepository('Canales');
    echo "..";
    $productos = $repository->findAll();
    echo "OK";
    if (!$productos) {
        throw $this->createNotFoundException(
            'No product found for id '.$id
        );
    }
    else{
        var_dump($productos);
    }
}

和我的prueba.php

<?php
include('./src/model/dao/daoGenerico.php');

showAction();
?>

正如我之前所说,当我在showAction中执行方法daoGenerico.php时,没有显示错误,但我认为问题在于使用getRepository('Canales')方法,也许Doctrine不会找到Canales,因为下一行echo ".."从未显示过;

任何建议或想法?我怎么能显示错误?

提前致谢

EDIT。

我也粘贴了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"), $isDevMode);
// or if you prefer yaml or XML
$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $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);

1 个答案:

答案 0 :(得分:1)

您错过了$entityManager您的功能。

请在model/dao;

中添加名称空间Canales.php