我是symfony和doctrine的新手。我在doctrine中有2个实体,一个是Users,另一个是ActivityLogs,这里是类。
Users.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* @ORM\Table(name="ms_users")
* @ORM\Entity
*/
class Users
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=50)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="verification_tokken", type="string", length=100)
*/
private $verificationTokken;
/**
* @var string
*
* @ORM\Column(name="tokken_expiry_time", type="string", length=50)
*/
private $tokkenExpiryTime;
/**
* @var integer
*
* @ORM\Column(name="is_two_way_auth_enabled", type="boolean")
*/
private $isTwoWayAuthEnabled;
/**
* @var string
*
* @ORM\Column(name="verification_pin", type="string", length=50)
*/
private $verificationPin;
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=250)
*/
private $slug;
/**
* @var string
*
* @ORM\Column(name="date_string", type="string", length=50)
*/
private $dateString;
/**
* @var string
*
* @ORM\Column(name="date_time_string", type="string", length=50)
*/
private $dateTimeString;
/**
* @var integer
*
* @ORM\Column(name="is_active", type="integer")
*/
private $isActive;
/**
* @var integer
*
* @ORM\Column(name="is_deleted", type="integer")
*/
private $isDeleted;
/**
* @var integer
*
* @ORM\Column(name="is_editable", type="integer")
*/
private $isEditable;
/**
* @var string
*
* @ORM\Column(name="last_updated_on", type="string", length=50)
*/
private $lastUpdatedOn;
/**
* @var integer
*
* @ORM\Column(name="school_id", type="integer")
*/
private $schoolId;
/**
* @var integer
*
* @ORM\Column(name="updated_by", type="integer")
*/
private $updatedBy;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 verificationTokken
*
* @param string $verificationTokken
*
* @return Users
*/
public function setVerificationTokken($verificationTokken)
{
$this->verificationTokken = $verificationTokken;
return $this;
}
/**
* Get verificationTokken
*
* @return string
*/
public function getVerificationTokken()
{
return $this->verificationTokken;
}
/**
* Set tokkenExpiryTime
*
* @param string $tokkenExpiryTime
*
* @return Users
*/
public function setTokkenExpiryTime($tokkenExpiryTime)
{
$this->tokkenExpiryTime = $tokkenExpiryTime;
return $this;
}
/**
* Get tokkenExpiryTime
*
* @return string
*/
public function getTokkenExpiryTime()
{
return $this->tokkenExpiryTime;
}
/**
* Set isTwoWayAuthEnabled
*
* @param integer $isTwoWayAuthEnabled
*
* @return Users
*/
public function setIsTwoWayAuthEnabled($isTwoWayAuthEnabled)
{
$this->isTwoWayAuthEnabled = $isTwoWayAuthEnabled;
return $this;
}
/**
* Get isTwoWayAuthEnabled
*
* @return integer
*/
public function getIsTwoWayAuthEnabled()
{
return $this->isTwoWayAuthEnabled;
}
/**
* Set verificationPin
*
* @param string $verificationPin
*
* @return Users
*/
public function setVerificationPin($verificationPin)
{
$this->verificationPin = $verificationPin;
return $this;
}
/**
* Get verificationPin
*
* @return string
*/
public function getVerificationPin()
{
return $this->verificationPin;
}
/**
* Set slug
*
* @param string $slug
*
* @return Users
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set dateString
*
* @param string $dateString
*
* @return Users
*/
public function setDateString($dateString)
{
$this->dateString = $dateString;
return $this;
}
/**
* Get dateString
*
* @return string
*/
public function getDateString()
{
return $this->dateString;
}
/**
* Set dateTimeString
*
* @param string $dateTimeString
*
* @return Users
*/
public function setDateTimeString($dateTimeString)
{
$this->dateTimeString = $dateTimeString;
return $this;
}
/**
* Get dateTimeString
*
* @return string
*/
public function getDateTimeString()
{
return $this->dateTimeString;
}
/**
* Set isActive
*
* @param integer $isActive
*
* @return Users
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return integer
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set isDeleted
*
* @param integer $isDeleted
*
* @return Users
*/
public function setIsDeleted($isDeleted)
{
$this->isDeleted = $isDeleted;
return $this;
}
/**
* Get isDeleted
*
* @return integer
*/
public function getIsDeleted()
{
return $this->isDeleted;
}
/**
* Set isEditable
*
* @param integer $isEditable
*
* @return Users
*/
public function setIsEditable($isEditable)
{
$this->isEditable = $isEditable;
return $this;
}
/**
* Get isEditable
*
* @return integer
*/
public function getIsEditable()
{
return $this->isEditable;
}
/**
* Set lastUpdatedOn
*
* @param string $lastUpdatedOn
*
* @return Users
*/
public function setLastUpdatedOn($lastUpdatedOn)
{
$this->lastUpdatedOn = $lastUpdatedOn;
return $this;
}
/**
* Get lastUpdatedOn
*
* @return string
*/
public function getLastUpdatedOn()
{
return $this->lastUpdatedOn;
}
/**
* Set schoolId
*
* @param integer $schoolId
*
* @return Users
*/
public function setSchoolId($schoolId)
{
$this->schoolId = $schoolId;
return $this;
}
/**
* Get schoolId
*
* @return integer
*/
public function getSchoolId()
{
return $this->schoolId;
}
/**
* Set updatedBy
*
* @param integer $updatedBy
*
* @return Users
*/
public function setUpdatedBy($updatedBy)
{
$this->updatedBy = $updatedBy;
return $this;
}
/**
* Get updatedBy
*
* @return integer
*/
public function getUpdatedBy()
{
return $this->updatedBy;
}
}
AcitivityLogs.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ActivitLogs
*
* @ORM\Table(name="ms_activity_logs")
* @ORM\Entity
*/
class ActivitLogs
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="user_id", type="integer")
*
* @ORM\ManyToOne(targetEntity="Users")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $userId;
/**
* @var string
*
* @ORM\Column(name="activity_log", type="text")
*/
private $activityLog;
/**
* @var string
*
* @ORM\Column(name="user_agent", type="string", length=250)
*/
private $userAgent;
/**
* @var string
*
* @ORM\Column(name="os_platform", type="string", length=250)
*/
private $osPlatform;
/**
* @var string
*
* @ORM\Column(name="ip_address", type="string", length=250)
*/
private $ipAddress;
/**
* @var string
*
* @ORM\Column(name="referer", type="text")
*/
private $referer;
/**
* @var string
*
* @ORM\Column(name="date_string", type="string", length=50)
*/
private $dateString;
/**
* @var string
*
* @ORM\Column(name="date_time_string", type="string", length=50)
*/
private $dateTimeString;
/**
* @var integer
*
* @ORM\Column(name="is_active", type="integer")
*/
private $isActive;
/**
* @var integer
*
* @ORM\Column(name="is_deleted", type="integer")
*/
private $isDeleted;
/**
* @var integer
*
* @ORM\Column(name="is_editable", type="integer")
*/
private $isEditable;
/**
* @var string
*
* @ORM\Column(name="last_updated_on", type="string", length=50)
*/
private $lastUpdatedOn;
/**
* @var integer
*
* @ORM\Column(name="school_id", type="integer")
*/
private $schoolId;
/**
* @var integer
*
* @ORM\Column(name="updated_by", type="integer")
*/
private $updatedBy;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* @param integer $userId
*
* @return ActivitLogs
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* @return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set activityLog
*
* @param string $activityLog
*
* @return ActivitLogs
*/
public function setActivityLog($activityLog)
{
$this->activityLog = $activityLog;
return $this;
}
/**
* Get activityLog
*
* @return string
*/
public function getActivityLog()
{
return $this->activityLog;
}
/**
* Set userAgent
*
* @param string $userAgent
*
* @return ActivitLogs
*/
public function setUserAgent($userAgent)
{
$this->userAgent = $userAgent;
return $this;
}
/**
* Get userAgent
*
* @return string
*/
public function getUserAgent()
{
return $this->userAgent;
}
/**
* Set osPlatform
*
* @param string $osPlatform
*
* @return ActivitLogs
*/
public function setOsPlatform($osPlatform)
{
$this->osPlatform = $osPlatform;
return $this;
}
/**
* Get osPlatform
*
* @return string
*/
public function getOsPlatform()
{
return $this->osPlatform;
}
/**
* Set ipAddress
*
* @param string $ipAddress
*
* @return ActivitLogs
*/
public function setIpAddress($ipAddress)
{
$this->ipAddress = $ipAddress;
return $this;
}
/**
* Get ipAddress
*
* @return string
*/
public function getIpAddress()
{
return $this->ipAddress;
}
/**
* Set referer
*
* @param string $referer
*
* @return ActivitLogs
*/
public function setReferer($referer)
{
$this->referer = $referer;
return $this;
}
/**
* Get referer
*
* @return string
*/
public function getReferer()
{
return $this->referer;
}
/**
* Set dateString
*
* @param string $dateString
*
* @return ActivitLogs
*/
public function setDateString($dateString)
{
$this->dateString = $dateString;
return $this;
}
/**
* Get dateString
*
* @return string
*/
public function getDateString()
{
return $this->dateString;
}
/**
* Set dateTimeString
*
* @param string $dateTimeString
*
* @return ActivitLogs
*/
public function setDateTimeString($dateTimeString)
{
$this->dateTimeString = $dateTimeString;
return $this;
}
/**
* Get dateTimeString
*
* @return string
*/
public function getDateTimeString()
{
return $this->dateTimeString;
}
/**
* Set isActive
*
* @param integer $isActive
*
* @return ActivitLogs
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return integer
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set isDeleted
*
* @param integer $isDeleted
*
* @return ActivitLogs
*/
public function setIsDeleted($isDeleted)
{
$this->isDeleted = $isDeleted;
return $this;
}
/**
* Get isDeleted
*
* @return integer
*/
public function getIsDeleted()
{
return $this->isDeleted;
}
/**
* Set isEditable
*
* @param integer $isEditable
*
* @return ActivitLogs
*/
public function setIsEditable($isEditable)
{
$this->isEditable = $isEditable;
return $this;
}
/**
* Get isEditable
*
* @return integer
*/
public function getIsEditable()
{
return $this->isEditable;
}
/**
* Set lastUpdatedOn
*
* @param string $lastUpdatedOn
*
* @return ActivitLogs
*/
public function setLastUpdatedOn($lastUpdatedOn)
{
$this->lastUpdatedOn = $lastUpdatedOn;
return $this;
}
/**
* Get lastUpdatedOn
*
* @return string
*/
public function getLastUpdatedOn()
{
return $this->lastUpdatedOn;
}
/**
* Set schoolId
*
* @param integer $schoolId
*
* @return ActivitLogs
*/
public function setSchoolId($schoolId)
{
$this->schoolId = $schoolId;
return $this;
}
/**
* Get schoolId
*
* @return integer
*/
public function getSchoolId()
{
return $this->schoolId;
}
/**
* Set updatedBy
*
* @param integer $updatedBy
*
* @return ActivitLogs
*/
public function setUpdatedBy($updatedBy)
{
$this->updatedBy = $updatedBy;
return $this;
}
/**
* Get updatedBy
*
* @return integer
*/
public function getUpdatedBy()
{
return $this->updatedBy;
}
}
我在userId字段上的这两个表之间有一个简单的ManyToOne关系。
这是我尝试使用doctrine将数据添加到数据库的代码。
public function checkDatabaseAction(){
$user = new Users();
$user->setDateString(date('Y-m-d'))
->setDateTimeString(date('Y-m-d H:i:s'))
->setIsActive(1)
->setIsDeleted(0)
->setIsEditable(1)
->setIsTwoWayAuthEnabled(0)
->setLastUpdatedOn(date('Y-m-d H:i:s'))
->setSchoolId(1)
->setSlug('abc-123')
->setTokkenExpiryTime(date('Y-m-d'))
->setUpdatedBy(1)
->setUsername('ahmedali5530')
->setVerificationPin('1122')
->setVerificationTokken('hskajdhfkjlh435kh34k5jh435');
$log = new ActivitLogs();
$log->setActivityLog('A log')
->setDateString(date('Y-m-d'))
->setDateTimeString(date('Y-m-d H:i:s'))
->setIpAddress('192.168.1.16')
->setIsActive(1)
->setIsDeleted(0)
->setIsEditable(1)
->setLastUpdatedOn(date('Y-m-d H:i:s'))
->setOsPlatform('Windows 7')
->setReferer('http://localhost/symfony')
->setSchoolId(1)
->setUpdatedBy(1)
->setUserAgent('Chrome')
->setUserId($user);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->persist($log);
$em->flush();
$allocateCourses = $em->getRepository('AppBundle:AllocateCourses')->findBy(array('isActive'=>1));
return $this->render(':main:form.html.php',array(
'data' => $allocateCourses,
));
}
当我执行此代码时,我收到错误: -
执行&#39; INSERT INTO ms_activity_logs(user_id,activity_log,user_agent,os_platform,ip_address,referer,date_string,date_time_string,is_active,is_deleted,is_editable,last_updated_on,school_id,updated_by)VALUES(?,?, ?,?,?,?,?,?,?,?,?,?,?,?)&#39;使用参数[{},&#34;日志&#34;,&#34; Chrome&#34;,&#34; Windows 7&#34;,&#34; 192.168.1.16&#34;,&#34 ; http:// localhost / symfony&#34;,&#34; 2015-12-22&#34;,&#34; 2015-12-22 16:53:55&#34;,1,0,1,& #34; 2015-12-22 16:53:55&#34;,1,1]:
捕获致命错误:类AppBundle \ Entity \ Users的对象无法转换为字符串
如何解决此错误的任何建议。
答案 0 :(得分:0)
好的我得到了答案。
我在ActivityLogs.php
中添加了新的私有属性,并告诉Doctrine生成实体。 Doctrine为该属性创建了getter和setter。这解决了我的问题。
以下是我添加的代码:
/**
* @ORM\ManyToOne(targetEntity="Users", inversedBy="ActivityLogs")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;