I set up a simple User->Comment association in Doctrine, simple OneToMany (One User can write many Comments).
All works find, but i found a strange behavior of Doctrine. First some Code:
User Entity
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends EntityAbstract {
/**
* @var int
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue
*/
protected $_id;
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\OneToMany(targetEntity="Comment", mappedBy="TodayIsASunnyDay")
* ^^^^^^^^^^^^^^^^ WTF
*/
protected $_commentsAuthored;
public function __construct() {
$this->_commentsAuthored =
new \Doctrine\Common\Collections\ArrayCollection();
}
}
Comment Entity
/**
* @ORM\Entity
* @ORM\Table(name="comments")
*/
class Comment extends EntityAbstract {
/**
* @var int
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue
*/
protected $_id;
/**
* @var User
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
*/
protected $_author;
}
In User, doctrine wants a mappedBy Attribute since its the inversed Side of the association. But as it seems, I can write anything I want as value . (the right value would be "user_id" i think?)
So, when is this value ever used or checked?
Yeah you're right, checked it in my project - no error is issued. It affects the way the entities are loaded (set fetch="EAGER"
inside OneToMany
and you'll get your error), mainly it sets the owner side on a comment object when hydrating data (there's a bit more magic going under the hood which is hard to understand). I'd post an issue in doctrine bug tracker, it's a source of errors that are hard to debug if you misspelled a field name.
inversedBy
as well:
/** * @var User * @ORM\\ManyToOne(targetEntity="User", inversedBy="_commentsAuthored") * @ORM\\JoinColumn(name="user_id", referencedColumnName="id") * */ protected $_author;
It should be used when you do retrieve the value of _commentsAuthored
and try to do something with it (eg, count it)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.