学说 2.0 案例错误
我在学说中遇到以下错误...
通过未配置为级联持久操作的关系找到了一个新实体:Default_Model_EventImages@000000004b59e58d000000003ff05ded。显式保留新实体或在关系上配置级联保留操作。
事件模型
<?php
/**
* @Entity
* @Table(name="events")
*/
class Default_Model_Event
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(name="title", type="string")
*/
private $title;
/**
* @Column(name="date_from", type="string")
*/
private $date_from;
/**
* @Column(name="date_to", type="string")
*/
private $date_to;
/**
* @Column(name="venue", type="string")
*/
private $venue;
/**
* @Column(name="address", type="string")
*/
private $address;
/**
* @Column(name="city", type="string")
*/
private $city;
/**
* @Column(name="state", type="string")
*/
private $state;
/**
* @Column(name="zip", type="string")
*/
private $zip;
/**
* @Column(name="country", type="string")
*/
private $country;
/**
* @Column(name="company_logo", type="string")
*/
private $company_logo;
/**
* @Column(name="website", type="string")
*/
private $website;
/**
* @Column(name="email", type="string")
*/
private $email;
/**
* @Column(name="phone_number", type="string")
*/
private $phone_number;
/**
* @Column(name="short_description", type="text")
*/
private $short_description;
/**
* @Column(name="long_description", type="text")
*/
private $long_description;
/**
* @Column(name="status", type="string")
*/
private $status;
/**
* @Column(name="date_created", type="string")
*/
private $date_created;
/**
* @Column(name="date_modified", type="string")
*/
private $date_modified;
/**
* @ManyToOne(targetEntity="Default_Model_User", inversedBy="user_mapper")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
private $event_mapper;
/**
* @OneToMany(targetEntity="Default_Model_EventImages", mappedBy="eventimage_mapper",cascade={"All"})
*/
private $images_mapper;
public function setUser($event_mapper)
{
if (!is_null($event_mapper )) {
$event_mapper->assignedToEvent($this);
$this->event_mapper = $event_mapper;
}
}
public function setEventimage_mapper($name, $order) {
$this->images_mapper[$name] = new Default_Model_EventImages($name, $order, $this);
}
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $title
*/
public function getTitle() {
return $this->title;
}
/**
* @return the $date_from
*/
public function getDate_from() {
return $this->date_from;
}
/**
* @return the $date_to
*/
public function getDate_to() {
return $this->date_to;
}
/**
* @return the $venue
*/
public function getVenue() {
return $this->venue;
}
/**
* @return the $address
*/
public function getAddress() {
return $this->address;
}
/**
* @return the $city
*/
public function getCity() {
return $this->city;
}
/**
* @return the $state
*/
public function getState() {
return $this->state;
}
/**
* @return the $zip
*/
public function getZip() {
return $this->zip;
}
/**
* @return the $country
*/
public function getCountry() {
return $this->country;
}
/**
* @return the $company_logo
*/
public function getCompany_logo() {
return $this->company_logo;
}
/**
* @return the $website
*/
public function getWebsite() {
return $this->website;
}
/**
* @return the $email
*/
public function getEmail() {
return $this->email;
}
/**
* @return the $phone_number
*/
public function getPhone_number() {
return $this->phone_number;
}
/**
* @return the $short_description
*/
public function getShort_description() {
return $this->short_description;
}
/**
* @return the $long_description
*/
public function getLong_description() {
return $this->long_description;
}
/**
* @return the $status
*/
public function getStatus() {
return $this->status;
}
/**
* @return the $date_created
*/
public function getDate_created() {
return $this->date_created;
}
/**
* @return the $date_modified
*/
public function getDate_modified() {
return $this->date_modified;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $title
*/
public function setTitle($title) {
$this->title = $title;
}
/**
* @param field_type $date_from
*/
public function setDate_from($date_from) {
$this->date_from = $date_from;
}
/**
* @param field_type $date_to
*/
public function setDate_to($date_to) {
$this->date_to = $date_to;
}
/**
* @param field_type $venue
*/
public function setVenue($venue) {
$this->venue = $venue;
}
/**
* @param field_type $address
*/
public function setAddress($address) {
$this->address = $address;
}
/**
* @param field_type $city
*/
public function setCity($city) {
$this->city = $city;
}
/**
* @param field_type $state
*/
public function setState($state) {
$this->state = $state;
}
/**
* @param field_type $zip
*/
public function setZip($zip) {
$this->zip = $zip;
}
/**
* @param field_type $country
*/
public function setCountry($country) {
$this->country = $country;
}
/**
* @param field_type $company_logo
*/
public function setCompany_logo($company_logo) {
$this->company_logo = $company_logo;
}
/**
* @param field_type $website
*/
public function setWebsite($website) {
$this->website = $website;
}
/**
* @param field_type $email
*/
public function setEmail($email) {
$this->email = $email;
}
/**
* @param field_type $phone_number
*/
public function setPhone_number($phone_number) {
$this->phone_number = $phone_number;
}
/**
* @param field_type $short_description
*/
public function setShort_description($short_description) {
$this->short_description = $short_description;
}
/**
* @param field_type $long_description
*/
public function setLong_description($long_description) {
$this->long_description = $long_description;
}
/**
* @param field_type $status
*/
public function setStatus($status) {
$this->status = $status;
}
/**
* @param field_type $date_created
*/
public function setDate_created($date_created) {
$this->date_created = $date_created;
}
/**
* @param field_type $date_modified
*/
public function setDate_modified($date_modified) {
$this->date_modified = $date_modified;
}
}//end class
事件图像模型
<?php
/**
* @Entity
* @Table(name="event_images")
*/
class Default_Model_EventImages
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(name="name", type="string")
*/
private $name;
/**
* @Column(name="order", type="integer")
*/
private $order;
/**
* @Column(name="status", type="string")
*/
private $status;
/**
* @Column(name="date_created", type="string")
*/
private $date_created;
/**
* @Column(name="date_modified", type="string")
*/
private $date_modified;
/**
* @ManyToOne(targetEntity="Default_Model_Event", inversedBy="images_mapper", cascade={"All"})
* @JoinColumn(name="event_id", referencedColumnName="id")
*/
private $eventimage_mapper;
public function __construct($name, $order, $event) {
$this->name = $name;
$this->order = $order;
$this->eventimage_mapper = $event;
$this->date_created = new DateTime();
$this->status = 'active';
}
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $status
*/
public function getStatus() {
return $this->status;
}
/**
* @return the $date_created
*/
public function getDate_created() {
return $this->date_created;
}
/**
* @return the $date_modified
*/
public function getDate_modified() {
return $this->date_modified;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $status
*/
public function setStatus($status) {
$this->status = $status;
}
/**
* @param field_type $date_created
*/
public function setDate_created($date_created) {
$this->date_created = $date_created;
}
/**
* @param field_type $date_modified
*/
public function setDate_modified($date_modified) {
$this->date_modified = $date_modified;
}
}//end class
控制器
$user_id = '2'; //(int)$formData['category_id'];
$user_mapper = $this->_em->find("Default_Model_User", (int)$user_id);
$event = new Default_Model_Event();
$event->setUser($user_mapper);
$event->setTitle($title);
$event->setDate_from($date_from);
$event->setDate_to($date_to);
$event->setVenue($venue);
$event->setAddress($address);
$event->setCity($city);
$event->setState($state);
$event->setZip($zip);
$event->setCountry($country);
$event->setCompany_logo($company_logo);
$event->setWebsite($website);
$event->setEmail($email);
$event->setPhone_number($phone);
$event->setStatus('pending');
$event->setShort_description($short_description);
$event->setLong_description($long_description);
$date = new Zend_Date;
$event->setDate_created($date->toString('YYYY-MM-dd HH:mm:ss'));
foreach($images as $key => $value) {
$event->setEventimage_mapper($value, $key);
//$this->_em->persist($eventImages);
}
$this->_em->persist($event);
$this->_em->flush();
I am getting following error in doctrine...
A new entity was found through a relationship that was not configured to cascade persist operations: Default_Model_EventImages@000000004b59e58d000000003ff05ded. Explicitly persist the new entity or configure cascading persist operations on the relationship.
Event model
<?php
/**
* @Entity
* @Table(name="events")
*/
class Default_Model_Event
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(name="title", type="string")
*/
private $title;
/**
* @Column(name="date_from", type="string")
*/
private $date_from;
/**
* @Column(name="date_to", type="string")
*/
private $date_to;
/**
* @Column(name="venue", type="string")
*/
private $venue;
/**
* @Column(name="address", type="string")
*/
private $address;
/**
* @Column(name="city", type="string")
*/
private $city;
/**
* @Column(name="state", type="string")
*/
private $state;
/**
* @Column(name="zip", type="string")
*/
private $zip;
/**
* @Column(name="country", type="string")
*/
private $country;
/**
* @Column(name="company_logo", type="string")
*/
private $company_logo;
/**
* @Column(name="website", type="string")
*/
private $website;
/**
* @Column(name="email", type="string")
*/
private $email;
/**
* @Column(name="phone_number", type="string")
*/
private $phone_number;
/**
* @Column(name="short_description", type="text")
*/
private $short_description;
/**
* @Column(name="long_description", type="text")
*/
private $long_description;
/**
* @Column(name="status", type="string")
*/
private $status;
/**
* @Column(name="date_created", type="string")
*/
private $date_created;
/**
* @Column(name="date_modified", type="string")
*/
private $date_modified;
/**
* @ManyToOne(targetEntity="Default_Model_User", inversedBy="user_mapper")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
private $event_mapper;
/**
* @OneToMany(targetEntity="Default_Model_EventImages", mappedBy="eventimage_mapper",cascade={"All"})
*/
private $images_mapper;
public function setUser($event_mapper)
{
if (!is_null($event_mapper )) {
$event_mapper->assignedToEvent($this);
$this->event_mapper = $event_mapper;
}
}
public function setEventimage_mapper($name, $order) {
$this->images_mapper[$name] = new Default_Model_EventImages($name, $order, $this);
}
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $title
*/
public function getTitle() {
return $this->title;
}
/**
* @return the $date_from
*/
public function getDate_from() {
return $this->date_from;
}
/**
* @return the $date_to
*/
public function getDate_to() {
return $this->date_to;
}
/**
* @return the $venue
*/
public function getVenue() {
return $this->venue;
}
/**
* @return the $address
*/
public function getAddress() {
return $this->address;
}
/**
* @return the $city
*/
public function getCity() {
return $this->city;
}
/**
* @return the $state
*/
public function getState() {
return $this->state;
}
/**
* @return the $zip
*/
public function getZip() {
return $this->zip;
}
/**
* @return the $country
*/
public function getCountry() {
return $this->country;
}
/**
* @return the $company_logo
*/
public function getCompany_logo() {
return $this->company_logo;
}
/**
* @return the $website
*/
public function getWebsite() {
return $this->website;
}
/**
* @return the $email
*/
public function getEmail() {
return $this->email;
}
/**
* @return the $phone_number
*/
public function getPhone_number() {
return $this->phone_number;
}
/**
* @return the $short_description
*/
public function getShort_description() {
return $this->short_description;
}
/**
* @return the $long_description
*/
public function getLong_description() {
return $this->long_description;
}
/**
* @return the $status
*/
public function getStatus() {
return $this->status;
}
/**
* @return the $date_created
*/
public function getDate_created() {
return $this->date_created;
}
/**
* @return the $date_modified
*/
public function getDate_modified() {
return $this->date_modified;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $title
*/
public function setTitle($title) {
$this->title = $title;
}
/**
* @param field_type $date_from
*/
public function setDate_from($date_from) {
$this->date_from = $date_from;
}
/**
* @param field_type $date_to
*/
public function setDate_to($date_to) {
$this->date_to = $date_to;
}
/**
* @param field_type $venue
*/
public function setVenue($venue) {
$this->venue = $venue;
}
/**
* @param field_type $address
*/
public function setAddress($address) {
$this->address = $address;
}
/**
* @param field_type $city
*/
public function setCity($city) {
$this->city = $city;
}
/**
* @param field_type $state
*/
public function setState($state) {
$this->state = $state;
}
/**
* @param field_type $zip
*/
public function setZip($zip) {
$this->zip = $zip;
}
/**
* @param field_type $country
*/
public function setCountry($country) {
$this->country = $country;
}
/**
* @param field_type $company_logo
*/
public function setCompany_logo($company_logo) {
$this->company_logo = $company_logo;
}
/**
* @param field_type $website
*/
public function setWebsite($website) {
$this->website = $website;
}
/**
* @param field_type $email
*/
public function setEmail($email) {
$this->email = $email;
}
/**
* @param field_type $phone_number
*/
public function setPhone_number($phone_number) {
$this->phone_number = $phone_number;
}
/**
* @param field_type $short_description
*/
public function setShort_description($short_description) {
$this->short_description = $short_description;
}
/**
* @param field_type $long_description
*/
public function setLong_description($long_description) {
$this->long_description = $long_description;
}
/**
* @param field_type $status
*/
public function setStatus($status) {
$this->status = $status;
}
/**
* @param field_type $date_created
*/
public function setDate_created($date_created) {
$this->date_created = $date_created;
}
/**
* @param field_type $date_modified
*/
public function setDate_modified($date_modified) {
$this->date_modified = $date_modified;
}
}//end class
Event images model
<?php
/**
* @Entity
* @Table(name="event_images")
*/
class Default_Model_EventImages
{
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(name="name", type="string")
*/
private $name;
/**
* @Column(name="order", type="integer")
*/
private $order;
/**
* @Column(name="status", type="string")
*/
private $status;
/**
* @Column(name="date_created", type="string")
*/
private $date_created;
/**
* @Column(name="date_modified", type="string")
*/
private $date_modified;
/**
* @ManyToOne(targetEntity="Default_Model_Event", inversedBy="images_mapper", cascade={"All"})
* @JoinColumn(name="event_id", referencedColumnName="id")
*/
private $eventimage_mapper;
public function __construct($name, $order, $event) {
$this->name = $name;
$this->order = $order;
$this->eventimage_mapper = $event;
$this->date_created = new DateTime();
$this->status = 'active';
}
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $status
*/
public function getStatus() {
return $this->status;
}
/**
* @return the $date_created
*/
public function getDate_created() {
return $this->date_created;
}
/**
* @return the $date_modified
*/
public function getDate_modified() {
return $this->date_modified;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $status
*/
public function setStatus($status) {
$this->status = $status;
}
/**
* @param field_type $date_created
*/
public function setDate_created($date_created) {
$this->date_created = $date_created;
}
/**
* @param field_type $date_modified
*/
public function setDate_modified($date_modified) {
$this->date_modified = $date_modified;
}
}//end class
controller
$user_id = '2'; //(int)$formData['category_id'];
$user_mapper = $this->_em->find("Default_Model_User", (int)$user_id);
$event = new Default_Model_Event();
$event->setUser($user_mapper);
$event->setTitle($title);
$event->setDate_from($date_from);
$event->setDate_to($date_to);
$event->setVenue($venue);
$event->setAddress($address);
$event->setCity($city);
$event->setState($state);
$event->setZip($zip);
$event->setCountry($country);
$event->setCompany_logo($company_logo);
$event->setWebsite($website);
$event->setEmail($email);
$event->setPhone_number($phone);
$event->setStatus('pending');
$event->setShort_description($short_description);
$event->setLong_description($long_description);
$date = new Zend_Date;
$event->setDate_created($date->toString('YYYY-MM-dd HH:mm:ss'));
foreach($images as $key => $value) {
$event->setEventimage_mapper($value, $key);
//$this->_em->persist($eventImages);
}
$this->_em->persist($event);
$this->_em->flush();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误表明您必须首先保留 Default_Model_EventImages。因此,您不仅需要将 Default_Model_EventImages 推送到图像堆栈,还必须显式地保留这些实体。
The error says that you have to persist Default_Model_EventImages first. So instead of just pushing the Default_Model_EventImages to the images stack - you will also have to explicitly persists these entities.
实际上问题是我必须单独保留所有事件图像对象..
}
并在事件模型中
Actually the problem was i had to persist all the event images object individually..
}
and in event model