php – Symfony2的Doctrine并没有为我创建一对多的结构
我有两张桌子.第一个表是用户,第二个是数据.数据库具有useridx列,该列是外部用户的idx. (主要唯一密钥).
这些是表结构: 表用户 CREATE TABLE public.users ( idx bigint NOT NULL,"name" varchar(250) DEFAULT NULL::character varying,surname varchar(250) DEFAULT NULL::character varying,isactive boolean NOT NULL DEFAULT false,/* Keys */ CONSTRAINT users_pkey PRIMARY KEY (idx),CONSTRAINT users_idx_key UNIQUE (idx) ) WITH ( OIDS = FALSE ); 表数据: CREATE TABLE public.datas ( idx bigint NOT NULL,useridx bigint,phrase varchar(100) DEFAULT NULL::character varying,response varchar(100) DEFAULT NULL::character varying,/* Keys */ CONSTRAINT datas_pkey PRIMARY KEY (idx),CONSTRAINT datas_idx_key UNIQUE (idx),/* Foreign keys */ CONSTRAINT fk_cf180c1a262768b5 FOREIGN KEY (useridx) REFERENCES public.users(idx) ON DELETE NO ACTION ON UPDATE NO ACTION ) WITH ( OIDS = FALSE ); 现在,当我运行这些命令: app/console doctrine:mapping:convert yml ./src/Acme/DemoBundle/Resources/config/doctrine/metadata/orm --from-database --force 和; app/console doctrine:mapping:import AcmeDemoBundle annotation app/console doctrine:generate:entities AcmeDemoBundle 我得到了这个结果: Datas.php namespace AcmeDemoBundleEntity; use DoctrineORMMapping as ORM; /** * AcmeDemoBundleEntityDatas * * @ORMTable(name="datas") * @ORMEntity */ class Datas { /** * @var bigint $idx * * @ORMColumn(name="idx",type="bigint",nullable=false) * @ORMId * @ORMGeneratedValue(strategy="SEQUENCE") * @ORMSequenceGenerator(sequenceName="datas_idx_seq",allocationSize="1",initialValue="1") */ private $idx; /** * @var string $phrase * * @ORMColumn(name="phrase",type="string",length=100,nullable=true) */ private $phrase; /** * @var string $response * * @ORMColumn(name="response",nullable=true) */ private $response; /** * @var Users * * @ORMManyToOne(targetEntity="Users") * @ORMJoinColumns({ * @ORMJoinColumn(name="useridx",referencedColumnName="idx") * }) */ private $useridx; /** * Get idx * * @return bigint */ public function getIdx() { return $this->idx; } /** * Set phrase * * @param string $phrase */ public function setPhrase($phrase) { $this->phrase = $phrase; } /** * Get phrase * * @return string */ public function getPhrase() { return $this->phrase; } /** * Set response * * @param string $response */ public function setResponse($response) { $this->response = $response; } /** * Get response * * @return string */ public function getResponse() { return $this->response; } /** * Set useridx * * @param AcmeDemoBundleEntityUsers $useridx */ public function setUseridx(AcmeDemoBundleEntityUsers $useridx) { $this->useridx = $useridx; } /** * Get useridx * * @return AcmeDemoBundleEntityUsers */ public function getUseridx() { return $this->useridx; } } ?> Users.php <?php namespace AcmeDemoBundleEntity; use DoctrineORMMapping as ORM; /** * AcmeDemoBundleEntityUsers * * @ORMTable(name="users") * @ORMEntity */ class Users { /** * @var bigint $idx * * @ORMColumn(name="idx",nullable=false) * @ORMId * @ORMGeneratedValue(strategy="SEQUENCE") * @ORMSequenceGenerator(sequenceName="users_idx_seq",initialValue="1") */ private $idx; /** * @var string $name * * @ORMColumn(name="name",length=250,nullable=true) */ private $name; /** * @var string $surname * * @ORMColumn(name="surname",nullable=true) */ private $surname; /** * @var boolean $isactive * * @ORMColumn(name="isactive",type="boolean",nullable=false) */ private $isactive; /** * Get idx * * @return bigint */ public function getIdx() { return $this->idx; } /** * Set name * * @param string $name */ public function setName($name) { $this->name = $name; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set surname * * @param string $surname */ public function setSurname($surname) { $this->surname = $surname; } /** * Get surname * * @return string */ public function getSurname() { return $this->surname; } /** * Set isactive * * @param boolean $isactive */ public function setIsactive($isactive) { $this->isactive = $isactive; } /** * Get isactive * * @return boolean */ public function getIsactive() { return $this->isactive; } } ?> 我也有yml文件,但我不认为他们在这里只需要我发布的PHP文件. 现在,当我在我的控制器中运行此命令时: <?php $user = $this->getDoctrine() ->getRepository('AcmeDemoBundle:Users') ->find(24); $phrase = $user->getDatas()->getPhrase(); ?> 我得到一个错误,说在非对象上调用成员函数getDatas()….我知道它很清楚.在Users.php中,我没有getDatas(). 但是我从Symfony2和Doctrine文档中读到的是它应该存在,因为它们是相关的.我想要做的就是在用户中获取数据. 这里我的错误是什么?什么我失踪了? 更新: <?php /** * @var AcmeDemoBundleEntityDatas * * @ORMOneToMany(targetEntity="Datas",mappedBy="datas",cascade={"all"}) */ private $datas; public function __construct() { $this->datas = new DoctrineCommonCollectionsArrayCollection(); } /** * Add phrases * * @param AcmeDemoBundleEntityDatas $datas */ public function addPhrases(AcmeDemoBundleEntityDatas $datas) { $this->datas[] = $datas; } /** * Get datas * * @return DoctrineCommonCollectionsCollection */ public function getDatas() { return $this->datas; } ?> 这些行到Datas.php <?php /** * @ORMManyToOne(targetEntity="Users",inversedBy="users",cascade={"all"}) */ protected $users; /** * Set users * * @param AcmeDemoBundleEntityUsers $users */ public function setUsers(AcmeDemoBundleEntityUsers $users) { $this->users = $users; } /** * Get users * * @return AcmeDemoBundleEntityUsers */ public function getUsers() { return $this->users; } ?> 现在getDatas()正在工作,但内部却没有. ($用户> getDatas() – > getPhrase();) Call to undefined method DoctrineORMPersistentCollection::getPhrase() 结论:我收到了错误,因为它返回了一个集合 – 当然 – .迭代(如foreach)它,你将访问数据. (如果遇到这样的问题.) 解决方法
如果看看@JoinColumn的
documentation
此注释用于@ManyToOne,@ OneToOne字段和嵌套在@ManyToMany中的@JoinTable的上下文中的关系的上下文中.此注释不是必需的.如果未指定,则从表和主键名称推断出属性name和referencedColumnName. 因此当您使用ManyToOne关系时,您的关系定义将是, /** * @var Users * * @ORMManyToOne(targetEntity="Users") * @ORMJoinColumn(name="useridx",referencedColumnName="idx") */ private $useridx; 编辑: 如果要从用户端获取数据,则必须创建OneToMany关系.例如 在Datas.php中 /** * @var Users * * @ORMManyToOne(targetEntity="Users",inversedBy = "datas") * @ORMJoinColumn(name="useridx",referencedColumnName="idx") */ private $useridx; 在Users.php中添加以下行, /** * @ORMOneToMany(targetEntity="Datas",mappedBy="useridx",cascade={"persist"}) */ protected $datas; 然后做一个学说:generate:entities命令.对关系检查this doc条目进行操作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |