问题描述
我已经和这个小型的学习项目进行了几天的斗争。这是一个DVD标题记录数据库界面。有两个实体:用户和标题。我想要保存/检索到数据库的标题条目的用户的ID。这里是实体关联详细信息:
类标题
{
// ...
/ **
* @ ORM\ManyToOne(targetEntity =Users,inversedBy =titles)
* @ ORM\JoinColumn(name =user_id,referencedColumnName =id)
* /
protected $ addedBy;
// ...
}
类用户
{
// ...
/ **
*
* @ ORM\OneToMany(targetEntity =标题,mappedBy =addedBy)
* /
protected $ titles = array();
// ...
}
当我尝试提交表单时,我收到以下错误:
警告:spl_object_hash()期望参数1为对象,
整数,给出在C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine
\ORM\UnitOfWork.php行1389
这是FormBuilder方法:
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('title')
- > ;加( '流派');
}
和addAction方法:
public function addAction()
{
//添加标题到db
$ title = new Titles();
$ form = $ this-> createForm(new TitlesType(),$ title);
$ request = $ this-> getRequest();
$ form-> bind($ request);
if($ form-> isValid()){
//持续到db
$ em = $ this-> getDoctrine()
- > ; getManager();
$ em-> persist($ title);
$ em-> flush();
return $ this-> redirect($ this-> generateUrl('dvdLoggerdvdBundle_homepage'));
}
return $ this-> render('dvdLoggerdvdBundle:Page:add.html.twig',array(
'form'=> $ form-> createView()
));
}
任何关于如何调试的提示都不胜感激。我还习惯了Symfony。
更新1
整个标题实体:
/ **
* @ ORM\Entity
* @ ORM\Entity (repositoryClass =dvdLogger\dvdBundle\Entity\Repository\loggerRepository)
* @ ORM\Table(name =titles)
* /
class标题
{
/ **
* @ ORM\Id
* @ ORM\Column(type =integer)
* @ ORM\GeneratedValue(strategy = AUTO)
* /
protected $ id;
/ **
* @ ORM\Column(type =string)
* /
protected $ title;
/ **
* @ ORM\Column(type =string)
* /
protected $ genre;
/ **
* @ ORM\Column(type =string)
*
* /
protected $ dateAdded;
/ **
* @ ORM\ManyToOne(targetEntity =Users,inversedBy =titles)
* @ ORM\JoinColumn(name =user_id ,referencedColumnName =id)
* /
protected $ addedBy;
public function __construct()
{
$ this-> user_id = null; //列的默认值user_id
$ this-> children = new \Doctrine\Common\Collections\ArrayCollection();
$ this-> dateAdded = date(F j,Y,g:i a);
$ this-> addedBy = 1;
}
/ **
*设置流派
*
* @param string $ genre
* @return标题
* /
public function setGenre($ genre)
{
$ this-> genre = $ genre;
return $ this;
}
/ **
*获取类型
*
* @return string
* /
public function getGenre )
{
return $ this->流派;
}
/ **
*设置dateAdded
*
* @param string $ dateAdded
* @return标题
* /
public function setDateAdded($ dateAdded)
{
$ this-> dateAdded = $ dateAdded;
return $ this;
}
/ **
*获取dateAdded
*
* @return string
* /
public function getDateAdded )
{
return $ this-> dateAdded;
}
/ **
*设置addedBy
*
* @param integer $ addedBy
* @return标题
* /
public function setAddedBy($ addedBy)
{
$ this-> addedBy = $ addedBy;
return $ this;
}
/ **
* Get addedBy
*
* @return integer
* /
public function getAddedBy )
{
return $ this-> addedBy;
}
/ **
*获取id
*
* @return integer
* /
public function getId )
{
return $ this-> id;
}
/ **
*设置标题
*
* @param string $ title
* @return标题
* /
public function setTitle($ title)
{
$ this-> title = $ title;
return $ this;
}
/ **
*获取标题
*
* @return string
* /
public function getTitle )
{
return $ this-> title;
}
}
和用户实体:
/ **
* @ ORM\Entity
* @ ORM\Entity(repositoryClass =dvdLogger\dvdBundle\\ \\ entity\Repository\usersRepository)
* @ ORM\Table(name =users)
* /
类用户
{
/ *
* @ ORM\Id
* @ ORM\Column(type =integer)
* @ ORM\GeneratedValue(strategy =AUTO)
* /
protected $ id;
/ **
* @ ORM\Column(type =string)
* /
protected $ username;
/ **
* @ ORM\Column(type =integer)
* /
protected $ rank;
/ **
* @ ORM\Column(type =string)
* /
protected $ email;
/ **
* @ ORM\Column(type =datetime)
* /
protected $ lastLogged;
/ **
*
* @ ORM\OneToMany(targetEntity =标题,mappedBy =addedBy)
* /
protected $ titles = array();
/ **
*获取ID
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}
/ **
*构造函数
* /
public function __construct()
{
$ this-> ; titles = new \Doctrine\Common\Collections\ArrayCollection();
}
/ **
*添加标题
*
* @param \dvdLogger\dvdBundle\Entity\Titles $ titles
* @return用户
* /
public function addTitle(\dvdLogger\dvdBundle\Entity\Titles $ titles)
{
$ this-> titles [] = $ titles;
return $ this;
}
/ **
*删除标题
*
* @param \dvdLogger\dvdBundle\Entity\Titles $ titles
* /
public function removeTitle(\dvdLogger\dvdBundle\Entity\Titles $ titles)
{
$ this-> titles-> removeElement($ titles );
}
/ **
*获取标题
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getTitles()
{
return $ this-> titles;
}
/ **
*设置用户名
*
* @param string $ username
* @return用户
* /
public function setUsername($ username)
{
$ this-> username = $ username;
return $ this;
}
/ **
*获取用户名
*
* @return string
* /
public function getUsername )
{
return $ this-> username;
}
/ **
*设置排名
*
* @param整数$ rank
* @return用户
* /
public function setRank($ rank)
{
$ this-> rank = $ rank;
return $ this;
}
/ **
*获取排名
*
* @return integer
* /
public function getRank( )
{
return $ this-> rank;
}
/ **
*设置电子邮件
*
* @param string $ email
* @return用户
* /
public function setEmail($ email)
{
$ this-> email = $ email;
return $ this;
}
/ **
*获取电子邮件
*
* @return string
* /
public function getEmail )
{
return $ this-> email;
}
/ **
*设置lastLogged
*
* @param \DateTime $ lastLogged
* @return用户
* /
public function setLastLogged($ lastLogged)
{
$ this-> lastLogged = $ lastLogged;
return $ this;
}
/ **
*获取lastLogged
*
* @return \DateTime
* /
public function getLastLogged()
{
return $ this-> lastLogged;
}
}
堆栈跟踪
$在C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php中的b $ b
在第1389行+
在ErrorHandler - > handle('2','spl_object_hash()期望参数1为对象,整数给定','C:\xampp\htdocs\dvdLogger\vendor\\ \\ doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php','1389',array('entity'=>'1','假设'=>'2'))
在spl_object_hash('1')
在C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php在行1389 +
在UnitOfWork - > getEntityState('1','2')
在C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\\ UnitOfWork的行788 +
上的\\Doctrine\ORM\UnitOfWork.php - > computeAssociationCha nges(array('fieldName'=>'addedBy','joinColumns'=> array(array('name'=>'user_id','unique'=> false,'nullable'=> 'onDelete'=> null,'columnDefinition'=> null,'referencedColumnName'=> 'id')),'cascade'=> array(),'inversedBy'=> 'titles','targetEntity'=> 'dvdLogger\dvdBundle\Entity\Users','fetch'=> '2','type'=> '2','mappedBy'=> null,'isOwningSide'=> true,'sourceEntity'=> 'dvdLogger\dvdBundle\Entity\Titles','isCascadeRemove'=> false,'isCascadePersist'=> false,'isCascadeRefresh'=> false,'isCascadeMerge'=> false,'isCascadeDetach'=> false,'sourceToTargetKeyColumns'=> array('user_id'=>'id'),'joinColumnFieldNames'=> array('user_id'=>'user_id'),'targetToSourceKeyColumns'=> array('id'=>'user_id'),'orphanRemoval'=> false),'1')
在C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php第687行+
在UnitOfWork - > computeChangeSet(对象(ClassMetadata),对象(标题))
在C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\\ \\ Doctrine\ORM\UnitOfWork.php在行404 +
在UnitOfWork - > computeScheduleInsertsChangeSets()
在C:\xampp\htdocs\dvdLogger\vendor\doctrine\ orm\lib\Doctrine\ORM\UnitOfWork.php在第711行+
在UnitOfWork - > computeChangeSets()
在C:\xampp\htdocs\dvdLogger\vendor \doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php在第297行+
在UnitOfWork - > commit(null)
在C:\xampp\htdocs第389行的\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php +
在EntityManager - > flush()
在C:\xampp\htdocs\dvdLogger\src\dvdLogger\dvdBundle\Controller\PageController.php第48行+
在PageController - > addAction()
在call_user_func_array(array(object(PageController),'addAction'),array())
在C:\xampp\htdocs\dvdLogger\\ \\ bin \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\第2931行的\\app\bootstrap.php.cache + HttpKernel上的
- > handle(object(Request),'1',true)
在C:\xampp\htdocs\ dvdLogger\app\bootstrap.php.cache at 3080 +
at ContainerAwareHttpKernel - > handle(object(Request),'1',true)
in C:\xampp\htdocs \dvdLogger\app\bootstrap.php.cache在行2330 +
在内核 - >句柄(对象(请求))
在C:\xampp\htdocs\dvdLogger\\第28行的\\web\app_dev.php +
更新2 / p>
public function indexAction()
{
$ em = $ this-> getDoctrine ()
- > getManager();
$ titles = $ em-> getRepository('dvdLoggerdvdBundle:Titles') - > getAllTitles();
return $ this-> render('dvdLoggerdvdBundle:Page:index.html.twig',array(
'titles'=> $ titles
));
}
然后从repo获取getAllTitles
public function getAllTitles()
{
//查看db
$ titles = $ this-> createQueryBuilder(' t')
- > select('t,u')
- > leftJoin('t.addedBy','u')
- > addOrderBy('t.title ','DESC');
返回$ titles-> getQuery() - > getResult();
}
我假设这是问题,它吸引大量的内存查询dbase,但我不明白为什么
完全从你那里删除此行 __ construct() / code>方法在
标题
实体。
$ this - > B = = 1;
然后在您的 PageController
中执行此操作。
$ em = $ this-> getDoctrine() - > getManager();
$ title-> setAddedBy($ this-> getUser());
$ em-> persist($ title);
$ em-> flush();
I have been battling with this small learning project for a few days now. It's a DVD title logging database interface. There are two entities, Users and Titles. I want to be able to save/retrieve to the database the ID of the user that made the title entry.
Here's the entities association details:
class Titles
{
//...
/**
* @ORM\ManyToOne(targetEntity="Users", inversedBy="titles")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $addedBy;
//...
}
class Users
{
//...
/**
*
* @ORM\OneToMany(targetEntity="Titles", mappedBy="addedBy")
*/
protected $titles = array();
//...
}
I'm getting the following error when I try to submit the form:
Warning: spl_object_hash() expects parameter 1 to be object,
integer given in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine
\ORM\UnitOfWork.php line 1389
Here is the formBuilder method:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('genre');
}
and the addAction method:
public function addAction()
{
// add title to db
$title = new Titles();
$form = $this->createForm(new TitlesType(), $title);
$request = $this->getRequest();
$form->bind($request);
if ($form->isValid()) {
// persist to db
$em = $this->getDoctrine()
->getManager();
$em->persist($title);
$em->flush();
return $this->redirect($this->generateUrl('dvdLoggerdvdBundle_homepage'));
}
return $this->render('dvdLoggerdvdBundle:Page:add.html.twig', array(
'form' => $form->createView()
));
}
Any tips regarding how to debug would be appreciated. I'm still getting used to Symfony.
update 1
the whole titles entity:
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="dvdLogger\dvdBundle\Entity\Repository\loggerRepository")
* @ORM\Table(name="titles")
*/
class Titles
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
* @ORM\Column(type="string")
*/
protected $genre;
/**
* @ORM\Column(type="string")
*
*/
protected $dateAdded;
/**
* @ORM\ManyToOne(targetEntity="Users", inversedBy="titles")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $addedBy;
public function __construct()
{
$this->user_id = null; // Default value for column user_id
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
$this->dateAdded = date("F j, Y, g:i a");
$this->addedBy = 1;
}
/**
* Set genre
*
* @param string $genre
* @return Titles
*/
public function setGenre($genre)
{
$this->genre = $genre;
return $this;
}
/**
* Get genre
*
* @return string
*/
public function getGenre()
{
return $this->genre;
}
/**
* Set dateAdded
*
* @param string $dateAdded
* @return Titles
*/
public function setDateAdded($dateAdded)
{
$this->dateAdded = $dateAdded;
return $this;
}
/**
* Get dateAdded
*
* @return string
*/
public function getDateAdded()
{
return $this->dateAdded;
}
/**
* Set addedBy
*
* @param integer $addedBy
* @return Titles
*/
public function setAddedBy($addedBy)
{
$this->addedBy = $addedBy;
return $this;
}
/**
* Get addedBy
*
* @return integer
*/
public function getAddedBy()
{
return $this->addedBy;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Titles
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
and the users entity:
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="dvdLogger\dvdBundle\Entity\Repository\usersRepository")
* @ORM\Table(name="users")
*/
class Users
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $username;
/**
* @ORM\Column(type="integer")
*/
protected $rank;
/**
* @ORM\Column(type="string")
*/
protected $email;
/**
* @ORM\Column(type="datetime")
*/
protected $lastLogged;
/**
*
* @ORM\OneToMany(targetEntity="Titles", mappedBy="addedBy")
*/
protected $titles = array();
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Constructor
*/
public function __construct()
{
$this->titles = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add titles
*
* @param \dvdLogger\dvdBundle\Entity\Titles $titles
* @return Users
*/
public function addTitle(\dvdLogger\dvdBundle\Entity\Titles $titles)
{
$this->titles[] = $titles;
return $this;
}
/**
* Remove titles
*
* @param \dvdLogger\dvdBundle\Entity\Titles $titles
*/
public function removeTitle(\dvdLogger\dvdBundle\Entity\Titles $titles)
{
$this->titles->removeElement($titles);
}
/**
* Get titles
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTitles()
{
return $this->titles;
}
/**
* Set username
*
* @param string $username
* @return Users
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set rank
*
* @param integer $rank
* @return Users
*/
public function setRank($rank)
{
$this->rank = $rank;
return $this;
}
/**
* Get rank
*
* @return integer
*/
public function getRank()
{
return $this->rank;
}
/**
* Set email
*
* @param string $email
* @return Users
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set lastLogged
*
* @param \DateTime $lastLogged
* @return Users
*/
public function setLastLogged($lastLogged)
{
$this->lastLogged = $lastLogged;
return $this;
}
/**
* Get lastLogged
*
* @return \DateTime
*/
public function getLastLogged()
{
return $this->lastLogged;
}
}
Stack Trace
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php at line 1389 +
at ErrorHandler ->handle ('2', 'spl_object_hash() expects parameter 1 to be object, integer given', 'C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php', '1389', array('entity' => '1', 'assume' => '2'))
at spl_object_hash ('1')
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php at line 1389 +
at UnitOfWork ->getEntityState ('1', '2')
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php at line 788 +
at UnitOfWork ->computeAssociationChanges (array('fieldName' => 'addedBy', 'joinColumns' => array(array('name' => 'user_id', 'unique' => false, 'nullable' => true, 'onDelete' => null, 'columnDefinition' => null, 'referencedColumnName' => 'id')), 'cascade' => array(), 'inversedBy' => 'titles', 'targetEntity' => 'dvdLogger\dvdBundle\Entity\Users', 'fetch' => '2', 'type' => '2', 'mappedBy' => null, 'isOwningSide' => true, 'sourceEntity' => 'dvdLogger\dvdBundle\Entity\Titles', 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false, 'sourceToTargetKeyColumns' => array('user_id' => 'id'), 'joinColumnFieldNames' => array('user_id' => 'user_id'), 'targetToSourceKeyColumns' => array('id' => 'user_id'), 'orphanRemoval' => false), '1')
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php at line 687 +
at UnitOfWork ->computeChangeSet (object(ClassMetadata), object(Titles))
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php at line 404 +
at UnitOfWork ->computeScheduleInsertsChangeSets ()
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php at line 711 +
at UnitOfWork ->computeChangeSets ()
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php at line 297 +
at UnitOfWork ->commit (null)
in C:\xampp\htdocs\dvdLogger\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php at line 389 +
at EntityManager ->flush ()
in C:\xampp\htdocs\dvdLogger\src\dvdLogger\dvdBundle\Controller\PageController.php at line 48 +
at PageController ->addAction ()
at call_user_func_array (array(object(PageController), 'addAction'), array())
in C:\xampp\htdocs\dvdLogger\app\bootstrap.php.cache at line 2969 +
at HttpKernel ->handleRaw (object(Request), '1')
in C:\xampp\htdocs\dvdLogger\app\bootstrap.php.cache at line 2931 +
at HttpKernel ->handle (object(Request), '1', true)
in C:\xampp\htdocs\dvdLogger\app\bootstrap.php.cache at line 3080 +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true)
in C:\xampp\htdocs\dvdLogger\app\bootstrap.php.cache at line 2330 +
at Kernel ->handle (object(Request))
in C:\xampp\htdocs\dvdLogger\web\app_dev.php at line 28 +
update 2
public function indexAction()
{
$em = $this->getDoctrine()
->getManager();
$titles = $em->getRepository('dvdLoggerdvdBundle:Titles')->getAllTitles();
return $this->render('dvdLoggerdvdBundle:Page:index.html.twig', array(
'titles' => $titles
));
}
then the getAllTitles from the repo
public function getAllTitles()
{
// view all records in db
$titles = $this->createQueryBuilder('t')
->select('t, u')
->leftJoin('t.addedBy', 'u')
->addOrderBy('t.title', 'DESC');
return $titles->getQuery()->getResult();
}
Im presuming this is the issue, its sucking massive memory when querying the dbase, but I cant see why
Remove this line completely from you __construct()
method in Title
entity.
$this->addedBy = 1;
And do this in your PageController
.
$em = $this->getDoctrine()->getManager();
$title->setAddedBy($this->getUser());
$em->persist($title);
$em->flush();
这篇关于Symfony2 / Doctrine错误提交表单时[新内存错误]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!