Reputation: 393
I am using Symfony2 for my application and I am using two Entity Managers; one is for read and other is for write.
I am creating entity manager object like that:
$em = $this->getDoctrine()->getEntityManager('write');
$em = $this->getDoctrine()->getEntityManager('read');
Initially it was working fine but now the following error is coming:
A new entity was found through the relationship '
AppBundle\Entity\ProfileViewer#viewer
' that was not configured to cascade persist operations for entity:shamsi
. Explicitly persist the new entity or configure cascading persist operations on the relationship.
Here is my ProfileViewer Entity:
/**
* AppBundle\Entity\ProfileViewer
*
* @ORM\Table(name="profile_viewer")
* @ORM\Entity
*/
class ProfileViewer
{
/**
* @var bigint $id
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var bigint $userId
*
* @ORM\Column(name="user_id", type="bigint", nullable=false)
*/
private $userId;
/**
* @var datetime $viewedAt
*
* @ORM\Column(name="viewed_at", type="datetime", nullable=true)
*/
private $viewedAt;
/**
* @ORM\ManyToOne(targetEntity="user", inversedBy="viewers")
* @ORM\JoinColumn(name="viewer_id", referencedColumnName="id")
*/
private $viewer;
public function __construct()
{
$this->viewedAt = new \DateTime();
}
/**
* Get id
*
* @return bigint
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* @param bigint $userId
*/
public function setUserId($userId)
{
$this->userId = $userId;
}
/**
* Get UserId
*
* @return bigint
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set viewedAt
*
* @param datetime $viewedAt
*/
public function setViewedAt($viewedAt)
{
$this->viewedAt = $viewedAt;
}
/**
* Get viewedAt
*
* @return datetime
*/
public function getViewedAt()
{
return $this->viewedAt;
}
/**
* Set viewer
*
* @param AppBundle\Entity\User $viewer
*/
public function setViewer(AppBundle\Entity\User $viewer)
{
$this->viewer = $viewer;
}
/**
* Get viewer
*
* @return AppBundle\Entity\User
*/
public function getViewer()
{
return $this->viewer;
}
}
This error comes when I have created two entity managers.
Upvotes: 3
Views: 10473
Reputation: 31919
By default, no operations are cascaded in Doctrine2.
You can add cascade={"persist"}
to your association:
/**
* @ORM\ManyToOne(targetEntity="user", inversedBy="viewers", cascade={"persist"})
* @ORM\JoinColumn(name="viewer_id", referencedColumnName="id")
*/
You can read this to understand cascade operations on associations in doctrine. This is important to underline:
Cascade operations are performed in memory. That means collections and related entities are fetched into memory, even if they are still marked as lazy when the cascade operation is about to be performed. However this approach allows entity lifecycle events to be performed for each of these operations.
However, pulling objects graph into memory on cascade can cause considerable performance overhead, especially when cascading collections are large. Makes sure to weigh the benefits and downsides of each cascade operation that you define.
Upvotes: 7