I have 2 Tables
Users
Messages
And the Structure of Tables:
Users:
Messages:
Now see there are number of users in Users Table and their messages are stored in Messages table identifying by fk_user_Id.
How can I make One-To-Many Relationship between these two tables or create this SQL Schema using Doctrine/Annotations?
This is a common case and i think you can easily find such examples if you would have searched.
You may refer this example
Your two entity files User.php and Message.php will look something like this
/**
* #ORM\Entity
* #ORM\Table(name="users")
*/
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $username;
/**
* #ORM\OneToMany(targetEntity="Message", mappedBy="user")
*/
protected $messages;
}
Message entity will look like this
/**
* #ORM\Entity
* #ORM\Table(name="messages")
*/
class Message
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $messageDescription;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="messages")
* #ORM\JoinColumn(name="fk_user_id", referencedColumnName="id")
*/
protected $user;
}
Related
I want to have direct relation between two entity fields from same table.
The idea is to add in my entity manager_id, which will point to another user in user table (I tried with OneToOne.
When user calls the action, user_id to whom to, for example, send the message will be pulled as
$user->getManager()->getId();
Error log says:
No mapping found for field ID.
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToOne(targetEntity="App\Entity\User",
mappedBy="id")
*/
private $manager;
Simple way to create such relation is to use undirectional relation type. Code for it looks like this:
class User
{
/**
* #var int
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $manager;
}
Now you can do:
/** #var User $user */
$managerId = $user->getManager()->getId();
Hope this helps.
PS. I added some #var docblocks for hinting.
I have two entities:
Team:
class Teams
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer", name="id")
* #ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="team")
*
*/
private $id;
User:
class User implements UserInterface, \Serializable
{
/**
* #var int
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Teams", inversedBy="id")
* #ORM\JoinColumn(name="team_id", referencedColumnName="id")
*/
private $team;
One team have many users
And I've got these errors:
The association App\Entity\User#team refers to the inverse side field
App\Entity\Teams#id which is not defined as association.
The association App\Entity\User#team refers to the inverse side field
App\Entity\Teams#id which does not exist.
I don't know where I'm wrong... Anyone know ?
Regards
I'm bit confused of what you want to do.
If you want unidirectional relation from user side then remove
* #ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="team")
this code.
And user should have only
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Teams")
*/
private $teams;
On the other hand if you want bidirectional relation then add property user on teams entity.
It doesn't work for you because the mapping you defined is on $id and it should be on the property
Bidirectional way:
class Teams
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer", name="id")
*
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="teams")
* #ORM\JoinColumn(name="team_id", referencedColumnName="id")
*/
private $user;
class User implements UserInterface, \Serializable
{
/**
* #var int
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Teams", mappedBy="user")
*/
private $teams;
It's good practice to name entities with singular name so perhaps you'd like to change Teams to Team entity.
Check also http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional
You want your Team to look like this:
class Team
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer", name="id")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\User")
*/
private $users;
This will generate teams of users. If you want your users to be able to on more than one team change your User call to a ManyToMany as well.
class User implements UserInterface, \Serializable
{
/**
* #var int
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Team")
*/
private $teams;
I'm newbie with PHP. I started work with symfony but i have this problem
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #param \Doctrine\Common\Collections\Collection $carList
* #ORM\OneToMany(targetEntity="AppBundle\CarBundle\Entity\Car", mappedBy="name", cascade={"persist"})
*/
private $carList;
//getters and setters
}
*
* #ORM\Entity(repositoryClass="AppBundle\CarBundle\Repository\Entity\CarRepository")
* #ORM\Table(name="car")
*/
class Car
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*
*
*/
protected $id;
/**
* #ORM\Column(type="string", length=100)
* #ORM\ManyToOne(targetEntity="AppBundle\UserBundle\Entity\User" , inversedBy="carList")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $name;
//getters and setters
}
The stacktrace says:
Symfony\Component\Debug\Exception\ContextErrorException: Notice: Undefined index: name
at n/a
and when i run php bin/console doctrine:schema:validate
[Mapping] FAIL - The entity-class 'AppBundle\UserBundle\Entity\User'
mapping is invalid:
* The association AppBundle\UserBundle\Entity\User#carList refers to the owning side field AppBundle\CarBundle\Entity\Car#name which is not
defined as association, but as field.
*The association AppBundle\UserBundle\Entity\User#carList refers to the owning side field Appbundle\CarBundle\Entity\Car#name which does
not exist
I have no idea whats going on, can you help me?
You are mixing up association names with column names. When you create an association you don't need to manually add the columns for that association, doctrine will work that out for you.
This code (in the Car class) says that the $name field is a normal text column in the car table, which of course is wrong
* #ORM\Column(name="name",type="string", length=100)
What you're describing is that one user can own many cars, and many cars can belong to one user. I'd then call the associations owner and cars, but you are of course free to call them whatever you want. Note that you do not need to define the join columns.
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #param \Doctrine\Common\Collections\Collection $cars
* #ORM\OneToMany(targetEntity="AppBundle\CarBundle\Entity\Car", mappedBy="owner", cascade={"persist"})
*/
private $cars;
public function __construct()
{
$this->cars = new \Doctrine\Common\Collections\ArrayCollection();
}
//getters and setters
}
/**
*
* #ORM\Entity(repositoryClass="AppBundle\CarBundle\Repository\Entity\CarRepository")
* #ORM\Table(name="car")
*/
class Car
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\UserBundle\Entity\User" , inversedBy="cars")
*/
private $owner;
//getters and setters
}
Read more: Doctrine association mapping
Hope it makes sense :)
So given the entity Comment, what is the cleanest way to get the CommentPerson to find out if the CommentPerson isModerator()?
Without getting your fingers dirty using a repository.
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\CommentRepository")
* #ORM\Table(name="comment")
*/
class Comment
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $user_id; //id of entity User
/**
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
**/
protected $user; //a user can have many comments
/**
* #ORM\Column(type="text")
*/
protected $commentText;
}
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\UserRepository")
* #ORM\Table(name="user")
*/
class User //this is the web user
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $person_id; //id of entity Person
/**
* #ORM\ManyToOne(targetEntity="Person")
* #ORM\JoinColumn(name="person_id", referencedColumnName="id")
**/
protected $person; //many web users can map to one actual person
public function getPerson()
{
return $this->person;
}
}
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\PersonRepository")
* #ORM\Table(name="person")
*/
class Person
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="text")
*/
protected $email;
/**
* #ORM\OneToMany(targetEntity="CommentPerson", mappedBy="person")
* #ORM\JoinColumn(name="id", referencedColumnName="person_id")
*/
private $commentPersons; //one person can have many commentPersons (since one person can be part in many different comment sections, not shown in code here, but take it as a fact)
}
/**
* #ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\CommentPersonRepository")
* #ORM\Table(name="comment_Person")
*/
class CommentPerson
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer")
*/
protected $person_id; //this is the id of the entity Person
/**
* #ORM\Column(type="boolean")
*/
protected $isModerator;
}
I've got 2 entites, with its setters and getters:
/**
* #ORM\Entity
* #ORM\Table(name="users")
*/
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToMany(targetEntity="SRC\PurchaseBundle\Entity\Purchase", inversedBy="users")
* #ORM\JoinTable(name="user_purchases",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="purchase_id", referencedColumnName="id")}
* )
*/
protected $purchases;
...
}
/**
* #ORM\Entity
* #ORM\Table(name="purchases")
*/
class Purchase
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #var users
*
* #ORM\ManyToMany(targetEntity="SRC\UserBundle\Entity\User", mappedBy="purchases")
*/
protected $users;
...
}
I'd need to select all missing purchases for the user.
I've already achieved this by getting all existing purchases and checking, one by one, they doesn't exist in $user->getPurchases().
Does exist a better way to get this results?
Is there a way so I could create a method inside USER entity called getMissingPurchases()?
Yes, you are missing
#ORM\JoinColumn(name="{name_of_column}", referencedColumnName="{name_of_fk_column}")