如果我有关系OneToOne->ManyToOne,如何在DB中JoinTable?
我有数据库MySQL,在表users中,我需要查看行client_id。但是现在我在表客户端中有了行user_id,仅此而已。但是需要在表用户中使用clien_id。我把这个问题贴在github的原理捆绑包中,因为这是注释@ORM\JoinColumn
在entity when ManyToOne中,我已经在DB entity_id中提交了文件,但我也需要在OneToMany中使用entity_id,我尝试
* @ORM\JoinColumn(name="developer_id", nullable = true, referencedColumnName="id")但在数据库中看不到此列
这是我在DB中的实体客户端,我有user_id
/**
* Clients.
*
* @ORM\Table(name="clients")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\ClientsRepository")
* @ExclusionPolicy("all")
*/
class Clients
{
use Timestampable;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Artel\ProfileBundle\Entity\Users", inversedBy="client", cascade={"persist", "remove"}, fetch="EAGER")
*/
protected $user;和这个my entity用户(不是列client_id):
/**
* Users.
*
* @ORM\Table(name="users")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\UsersRepository")
* @ExclusionPolicy("all")
*/
class Users
{
use Timestampable;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Clients", mappedBy="user", orphanRemoval=true, cascade={"persist"})
* @ORM\JoinColumn(name="client_id", nullable = true, referencedColumnName="id")
*/
public $client;发布于 2016-01-15 02:28:39
我觉得你对你的亲戚有点迷惑...
试试这个:
客户端:
class Clients
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}用户:
class Users
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Clients")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
public $client;
}现在,如果您要运行:php bin/console doctrine:schema:update --force在您的数据库中将创建表:
CREATE TABLE `clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`client_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_1483A5E919EB6921` (`client_id`),
CONSTRAINT `FK_1483A5E919EB6921` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`)
) ENGINE=InnoDB;假设这正是你所期望的.
当然这一切都是真的,如果我没弄错的话...
(顺便说一句,最好将实体重命名为User和Client。)
https://stackoverflow.com/questions/34787633
复制相似问题