我正在尝试插入一个mysql数据库。我正在使用doctrine和标准PHP(没有框架)。我有3个表,包含3个PHP类
<?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 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;
...
}
<?php
namespace model\dto;
use Doctrine\ORM\Mapping as ORM;
/**
* Playlist
*
* @ORM\Table(name="playlist", indexes={@ORM\Index(name="idCanal", columns={"idCanal"})})
* @ORM\Entity
*/
class Playlist
{
/**
* @var string
*
* @ORM\Column(name="idYoutube", type="string", length=34, nullable=false)
*/
private $idYoutube;
/**
* @var string
*
* @ORM\Column(name="descripcion", type="string", length=255, nullable=false)
*/
private $descripcion;
/**
* @var boolean
*
* @ORM\Column(name="ingles", type="boolean", nullable=false)
*/
private $ingles;
/**
* @var string
*
* @ORM\Column(name="titulo", type="string", length=255, nullable=false)
*/
private $titulo;
/**
* @var string
*
* @ORM\Column(name="imagen", type="string", length=255, nullable=false)
*/
private $imagen;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Canales
*
* @ORM\ManyToOne(targetEntity="Canales")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="idCanal", referencedColumnName="Id")
* })
*/
private $idcanal;
...
}
<?php
namespace model\dto;
use Doctrine\ORM\Mapping as ORM;
/**
* Videos
*
* @ORM\Table(name="videos", indexes={@ORM\Index(name="idPlaylist", columns={"idPlaylist"})})
* @ORM\Entity
*/
class Videos
{
/**
* @var string
*
* @ORM\Column(name="idYoutube", type="string", length=34, nullable=false)
*/
private $idYoutube;
/**
* @var string
*
* @ORM\Column(name="titulo", type="string", length=255, nullable=false)
*/
private $titulo;
/**
* @var string
*
* @ORM\Column(name="imagen", type="string", length=255, nullable=false)
*/
private $imagen;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Playlist
*
* @ORM\ManyToOne(targetEntity="Playlist")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="idPlaylist", referencedColumnName="idPlaylist")
* })
*/
private $idplaylist;
...
}
另外,我有插入和查询的这个dao
<?php
abstract class daoGenerico{
private $entityManager;
/**
* Class Constructor
* @param $entityManager
*/
public function __construct()
{
require("bootstrap.php");
$this->entityManager = $entityManager;
}
/**
* @return mixed
*/
public function getEntityManager()
{
return $this->entityManager;
}
function listAll($nombre){
$repositorio = 'model\dto\\'.$nombre;
$repository = $this->getEntityManager()->getRepository($repositorio);
$entityList = $repository->findAll();
if (!$entityList) {
throw $this->createNotFoundException(
'No product found'
);
}
return $entityList;
}
function findByIdYoutube($nombre, $entity){
$existe = true;
$repositorio = 'model\dto\\'.$nombre;
$repository = $this->getEntityManager()->getRepository($repositorio);
$entity = $repository->findOneBy(array('idYoutube' => $entity->getIdYoutube()));
return $entity;
}
function insert($entity){
$entityM= $this->getEntityManager();
$entityM->merge($entity);
$entityM->flush();
}
}
?>
我的test.php就是这个
<?php
function getPlaylists($idCanal) {
$key = 'XXXX';
$channel = $idCanal; //ejemplo: UCL-aihy3UD61TmvCOL9szUw
$url = "https://www.googleapis.com/youtube/v3/playlists?key=$key&channelId=$channel&part=snippet,id&order=date&maxResults=50";
$json = file_get_contents($url);
$obj = json_decode($json, true);
//var_dump($obj);
return $obj;
}
function getVideosByPlaylists($idPlaylist) {
$key = 'XXXXX';
$url = "https://www.googleapis.com/youtube/v3/playlistItems?key=$key&playlistId=$idPlaylist&part=snippet,id&order=date&maxResults=50";
$json = file_get_contents($url);
$obj = json_decode($json, true);
//var_dump($obj);
return $obj;
}
include_once('./src/model/dao/daoGenerico.php');
include_once('./src/model/dao/daoCanal.php');
include_once('./src/model/dao/daoPlaylist.php');
include_once('./src/model/dao/daoVideo.php');
include_once('./src/model/dto/Playlist.php');
include_once('./src/model/dto/Videos.php');
$daoCanal = new daoCanal();
$daoPlaylist = new daoPlaylist();
$daoVideo = new daoVideo();
$canales = $daoCanal->listAll('Canales');
foreach ($canales as $canal) {
echo ($canal->getNombre()."\n");
//buscamos las playlist del canal
$playlistsYT = getPlaylists($canal->getUrl());
foreach ($playlistsYT['items'] as $playlistYT) {
//obtenemos los valores y buscamos en BD si ya existe esa playlist
$idcanal = $canal;
$idYoutube = $playlistYT["id"];
$titulo = $playlistYT["snippet"]["title"];
$descripcion = $playlistYT["snippet"]["description"];
$imagen = $playlistYT["snippet"]["thumbnails"]["high"]["url"];
$ingles = $canal->getIngles();
$playlist = new model\dto\Playlist($idYoutube, $descripcion, $ingles, $titulo, $imagen, $idcanal);
$playlistBD = $daoPlaylist->findByIdYoutube('Playlist',$playlist);
if(!$playlistBD){
//si no existe, se crea
echo ("-->".$playlist->getTitulo()."\n");
$daoPlaylist->insert($playlist);
$playlistBD = $daoPlaylist->findByIdYoutube('Playlist',$playlist);
}
//actualizamos el objeto $playlist para guardar el id generado
$playlist->setId($playlistBD->getId());
//buscamos los videos correspondientes a esa playlist
$videosYT = getVideosByPlaylists($playlist->getidYoutube());
foreach ($videosYT['items'] as $videoYT) {
//obtenemos los valores
$idPlaylist = $playlist;
$idYoutube = $videoYT["snippet"]["resourceId"]["videoId"];
$titulo = $videoYT["snippet"]["title"];
//si el video no tiene thumbnails, es que ha sido eliminado o es privado
if(isset($videoYT["snippet"]["thumbnails"])){
$imagen = $videoYT["snippet"]["thumbnails"]["high"]["url"];
$video = new model\dto\Videos($idYoutube, $titulo, $imagen, $idPlaylist);
//buscamos en BD si ya existe ese video
$existe = $daoVideo->findByIdYoutube('Videos',$video);
var_dump($video);
if(!$existe){
echo ("-->".$video->getTitulo()."\n");
$daoVideo->insert($video);
}
}
}
}
}
?>
执行此行时我的问题出现了
$ daoVideo-&GT;插入($视频);
我可以在播放列表中插入和查询步骤并在视频步骤中查询但不插入视频。这是错误
Fatal error: Uncaught exception 'ReflectionException' with message 'Class Canales does not exist' in /Applications/MAMP/htdocs/yoga/vendor/doctrine/common/lib/Doctrine/Common/Proxy/ProxyGenerator.php:960
Stack trace:
#0 /Applications/MAMP/htdocs/yoga/vendor/doctrine/common/lib/Doctrine/Common/Proxy/ProxyGenerator.php(960): ReflectionParameter->getClass()
#1 /Applications/MAMP/htdocs/yoga/vendor/doctrine/common/lib/Doctrine/Common/Proxy/ProxyGenerator.php(908): Doctrine\Common\Proxy\ProxyGenerator->getParameterType(Object(Doctrine\ORM\Mapping\ClassMetadata), Object(ReflectionMethod), Object(ReflectionParameter))
#2 /Applications/MAMP/htdocs/yoga/vendor/doctrine/common/lib/Doctrine/Common/Proxy/ProxyGenerator.php(780): Doctrine\Common\Proxy\ProxyGenerator->buildParametersString(Object(Doctrine\ORM\Mapping\ClassMetadata), Object(ReflectionMethod), Array)
#3 [internal function]: Doctrine\Common\Proxy\ProxyGenerator->generateMethods(Object(Doctrine\ORM\Mapping\ClassMetadata))
#4 /Applications/MAMP/htdocs/yoga/vendor/doctrine/c in /Applications/MAMP/htdocs/yoga/vendor/doctrine/common/lib/Doctrine/Common/Proxy/Exception/UnexpectedValueException.php on line 57
关于此错误的任何想法或建议?
提前致谢
答案 0 :(得分:0)
我找到了解决方案。在我的班上,setCanal就是这样的
public function setIdcanal(Canales $idcanal = null) {
...
}
我必须为此改变它:
public function setIdcanal(\model\dto\Canales $idcanal = null) {
...
}