I'm trying to do the following :
/**
* #Entity
*/
class Player {
/**
*#Column
*#Id
*/
private $uuid; //gets assigned a Uuid in the constructor
/**
* #ManyToOne(targetEntity="Team", cascade={"persist"})
* #JoinColumn(referencedColumnName="uuid")
*/
private $team;
public function setTeam(Team $team) {
$this->team = $team;
}
//...
}
/**
* #Entity
*/
class Team {
/**
* #Column
* #Id
*/
private $uuid; //gets assigned a Uuid in the constructor
//...
}
$player = new Player;
$team = new Team;
$player->setTeam($team);
$entityManager->persist($player);
$entityManager->flush();
The team is not persisted in to the database.
I do not want to call $entityManager->persist($team) as in my case, the Team is created in a part of the code where I don't have knowledge of persistence.
My expectation is that the cascade={"persist"} option should make the EntityManager also persist the Team.
Why is my expectation wrong or what am I doing wrong?
I was not mistaken... This works as expected!
Taking a second look, there was never a flush after a added the Team to the Player.
Reading my own question again, I realised what that I missed it...
I should talk to my rubber duck more often, so it seems...
Related
I have two entities - User and UserSettings. In User entity, I want to have UserSettings as an attribute. That would be OK, I would add a OneToOne relation but there's a problem - because UserSettings is an owning side of the relation, every time I load User entity, Doctrine has to load the UserSettings entity too.
Is there a way how to load User but not UserSettings?
I made maybe a weird solution - there's no relation between these entities and the settings are loaded by method of Facade. For example:
/**
* #ORM\Entity
*/
class User
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(type="string")
*/
private $name;
/** #var UserSettings */
private $settings;
public function __construct()
{
$this->settings = new UserSettings();
}
public function setSettings(UserSettings $settings)
{
$this->settings = $settings;
}
}
/**
* #ORM\Entity
*/
class UserSettings
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(name="user_id", type="integer")
*/
private $userId;
}
class UserFacade
{
/**
* #var EntityManager
*/
private $em; // is injected automatically by DI
public function loadSettings(User $user)
{
$settings = $this->em->getRepository("UserSettings")->findOneBy(array("userId" => $user->id));
$user->setSettings($settings);
}
}
$user = $em->find("User", 1);
// if I want user's settings
$userFacade->loadSettings($user); // now I can use $user->getSettings()->something;
Side note: UserFacade is a service class that manipulates with users' data like adding new user, editing, deleting etc. In my MVC application, controller classes communicate with Facades, not with EntityManager directly.
That's OK - settings are loaded only when I want to. However, there are two possible problems:
a) I don't think this is a clear way
b) When I want a list of users, I cannot JOIN a table where settings are, because entities are not associated, so I have to make an extra SQL for each user.
My question is - how to solve the problem with OneToOne relation? I don't have much experience with Doctrine, so it may be a stupid question - sorry for that.
Thanks!
I'm trying to do bi-directional one-to-one with doctrine.
User model
<?php
/**
* #Entity
* #Table(name="User")
*/
class User {
/**
* #Id
* #Column(type="integer")
* #GeneratedValue
*/
protected $id;
/**
* #OneToOne(targetEntity="Cart",mappedBy="user",cascade={"persist"})
*/
public $cart;
}
Cart model
<?php
/**
* #Entity
* #Table(name="Cart")
*/
class Cart {
/**
* #Id
* #Column(type="integer")
* #GeneratedValue
*/
protected $id;
/**
* #OneToOne(targetEntity="User",inversedBy="cart")
* #JoinColumn(name="user_id", referencedColumnName="id")
*/
public $user;
}
And I want to set the cart like that
<?php
$user->cart = new Cart;
$entityManager->flush();
The cart has been created, but the user_id is not set. (NULL)
What's wrong ?
(I've not created getters/setters for test)
So I did some extended testing to see what could be done.
Your relationship code appears correct. I'd personally drop the join column annotation, as it defaults to that anyway.
You DO need getters/setters inside each of the entities such as:
// In User entity
public function setCart(Cart $cart) {
$this->cart = $cart;
}
// In Cart entity
public function setUser(User $user) {
$this->user = $user;
}
You also need to do something like:
$user = new User();
$em->persist($user);
$cart = new Cart();
$cart->setUser($user);
$em->persist($cart);
$user->setCart($cart);
$em->flush();
Using this snippet I was able to save two relationships to the DB and load them again. The documentation specifically states that new owners with cascade do not cascade.
"Doctrine 2 does not cascade the persist operation to all nested
entities that are new as well."
In my example, I've specifically set and persisted both the new items. Existing parents with new cascading children don't have this problem.
Hopefully you can use this information to figure out exactly what needs to change.
To simplify:
You DO need getters/setters inside each of the entities such as:
// In User entity
public function setCart(Cart $cart) {
$this->cart = $cart;
}
// In Cart entity
public function setUser(User $user) {
$user->setCart($this);
$this->user = $user;
}
You also need to do something like:
$user = new User();
$em->persist($user);
$cart = new Cart();
$cart->setUser($user);
$em->persist($cart);
$em->flush();
This process "$user->setCart($cart);" is deported to setter of User in cart;
It's a pretty old question but I faced something similar and found another alternative to kinghfb answer. You can also do the following on your inverse side's entity (User in case of this question).
/**
* User's Constructor
*/
public function __construct()
{
$this->cart= new Cart();
$this->cart->setUser($this);
}
Note that you will need getters and setters as suggested in accepted answer but after doing above, you do not need to create and persist the Cart entity every time you are saving User.
Caution: This solution is for the case where a user will always have a cart.
Hope this helps someone facing similar issues. Thanks!
To make it simple, let's say I have two objects with one-to-many relation:
User --(1:n)--> Request
with User defined as
class User {
...
/** #OneToMany(targetEntity="Request", mappedBy="user", cascade={"all"}) */
private $request;
...
}
and Request defined as
class Request {
...
/** #ManyToOne(targetEntity="User", inversedBy="request", cascade={"persist"}) */
private $user;
...
}
Is it possible to create a method that removes all Requests associated with User from within User entity?
What I need is something like this:
class User {
....
public function removeAllMyRequests() {
foreach ($this->getAllMyRequests() as $req)
$this->em->remove($req);
}
....
}
But apparently I'm not supposed to invoke entity manager from within entity.
You can mark the association with "Orphan Removal":
/**
* #Entity
*/
class User
{
/**
* #OneToMany(
* targetEntity="Request",
* mappedBy="user",
* cascade={"all"},
* orphanRemoval=true
* )
*/
private $requests;
}
Any Request object removed from the User#requests collection will be marked for removal during the next EntityManager#flush() call.
To remove all items at once, you can simply use Doctrine\Common\Collections\Collection#clear():
public function removeAllMyRequests() {
$this->requests->clear();
}
I think you are looking for the "cascade" option : http://docs.doctrine-project.org/en/2.0.x/reference/working-with-associations.html#transitive-persistence-cascade-operations
I have question about inserting entity into a database. I have two models:
class News {
/**
* #Column(type="string", length=100)
* #var string
*/
protected $title;
/**
* #ManyToOne(targetEntity="User", inversedBy="news")
* #JoinColumn(referencedColumnName="id")
*/
protected $author;
}
class User {
/**
* #Id #GeneratedValue #Column(type="integer")
* #var integer
*/
protected $id;
/**
* #OneToMany(targetEntity="News", mappedBy="author")
*/
protected $news;
public function __construct() {
$this->news = new \Doctrine\Common\Collections\ArrayCollection;
}
}
To add new news I must include both User and News classes (if they're in separate files, for ex. UserModel.php and NewsModel.php) and write a code:
$news = new News()
$news->setTitle('TEST title');
$news->setAuthor($database->find('User', 1));
$database->persist($news);
My question is: Is there any way to insert news without including User class?
You don't need to actually load the User.
Instead, you can use a reference proxy:
<?PHP
$news = new News()
$news->setTitle('TEST title');
$news->setAuthor($em->getReference('User',1));
$em->persist($news);
one other thing you could do (thinking in a more object-oriented kinda way) is add a method called addNews($news) on your user entity:
public function addNews($news) {
// you should check if the news doesn't already exist here first
$this->news->add($news);
$news->setAuthor($this);
}
and add cascade persist to your mapping:
/**
* #OneToMany(targetEntity="News", mappedBy="author", cascade={"persist"})
*/
protected $news;
then fetch your user, add the news, and merge the changes:
$news = new News()
$news->setTitle('TEST title');
$author = $database->find('User', 1);
$author->addNews($news);
//merge changes on author entity directly
$em->merge($author);
I preferr this approach because it gives you the opportunity to do extra checks or controls while adding the news, making for reusable and easy to read code
i think my question is not clear but i try to illustrate my point here. assuming i have a many to many, self referencing relationship where a user can be a teacher (say u post answers at SO) and a teacher can be a student (u may answer questions but may ask too) too.
namespace Entities;
/** #Entity #Table(name="users")) */
class User {
/**
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #Column(type="string", length="30")
*/
private $name;
/**
* #ManyToMany(targetEntity="User", inversedBy="teachers")
* #JoinTable(name="Teachers_Students",
* joinColumns={#JoinColumn(name="teacher", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="student", referencedColumnName="id")}
* )
*/
private $students;
/**
* #ManyToMany(targetEntity="User", mappedBy="students")
*/
private $teachers;
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
function getStudents() {
return $this->students;
}
function getTeachers() {
return $this->teachers;
}
}
say i have a few users
$user1 = new User;
$user1->setName("user 1");
$user2 = new User;
$user2->setName("user 2");
$user3 = new User;
$user3->setName("user 3");
$user4 = new User;
$user3->setName("user 4");
and i like to setup teacher-student relationships between them, i was reading up doctrine reference, saw that u can use the Collections::add() to add elements to a collection
// user1 is a teacher to user2 & 3
$user1->getStudents()->add($user2);
$user1->getStudents()->add($user3);
// user2 is a teacher to user3
$user2->getStudents()->add($user3);
// user4 is a student to user2
// tests if adding something from the inverse side works
$user4->getTeachers()->add($user2);
but this fails with
Fatal error: Call to a member function
add() on a non-object in
D:\ResourceLibrary\Frameworks\Doctrine\tools\sandbox\index.php
on line 70
how can i add elements to a collection or a relationship?
Remember that your collection variables are just regular ol' class properties. Which means they'll be null until you initialize them. The typical thing to do is instantiate them using Doctrine's ArrayCollection class, which will allow you to use the methods you described.
Try this:
public function __construct()
{
$this->students = new \Doctrine\Common\Collections\ArrayCollection();
$this->teachers = new \Doctrine\Common\Collections\ArrayCollection();
}