我有两个表。第一个表是users
,第二个表是datas
。Datas有useridx
列,与user
的idx
无关。(主唯一键(。
这些是表结构:
表users
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
);
表datas
:
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
我得到了这个结果:
数据.php
namespace Acme'DemoBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* Acme'DemoBundle'Entity'Datas
*
* @ORM'Table(name="datas")
* @ORM'Entity
*/
class Datas
{
/**
* @var bigint $idx
*
* @ORM'Column(name="idx", type="bigint", nullable=false)
* @ORM'Id
* @ORM'GeneratedValue(strategy="SEQUENCE")
* @ORM'SequenceGenerator(sequenceName="datas_idx_seq", allocationSize="1", initialValue="1")
*/
private $idx;
/**
* @var string $phrase
*
* @ORM'Column(name="phrase", type="string", length=100, nullable=true)
*/
private $phrase;
/**
* @var string $response
*
* @ORM'Column(name="response", type="string", length=100, nullable=true)
*/
private $response;
/**
* @var Users
*
* @ORM'ManyToOne(targetEntity="Users")
* @ORM'JoinColumns({
* @ORM'JoinColumn(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 Acme'DemoBundle'Entity'Users $useridx
*/
public function setUseridx('Acme'DemoBundle'Entity'Users $useridx)
{
$this->useridx = $useridx;
}
/**
* Get useridx
*
* @return Acme'DemoBundle'Entity'Users
*/
public function getUseridx()
{
return $this->useridx;
}
}
?>
用户.php
<?php
namespace Acme'DemoBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* Acme'DemoBundle'Entity'Users
*
* @ORM'Table(name="users")
* @ORM'Entity
*/
class Users
{
/**
* @var bigint $idx
*
* @ORM'Column(name="idx", type="bigint", nullable=false)
* @ORM'Id
* @ORM'GeneratedValue(strategy="SEQUENCE")
* @ORM'SequenceGenerator(sequenceName="users_idx_seq", allocationSize="1", initialValue="1")
*/
private $idx;
/**
* @var string $name
*
* @ORM'Column(name="name", type="string", length=250, nullable=true)
*/
private $name;
/**
* @var string $surname
*
* @ORM'Column(name="surname", type="string", length=250, nullable=true)
*/
private $surname;
/**
* @var boolean $isactive
*
* @ORM'Column(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();
?>
我收到一个错误,说Call to a member function getDatas() on a non-object...
.我知道这很清楚。在用户中.php我没有getDatas((。
但是我从Symfony2和Doctrine文档中读到的是它应该在那里,因为它们是相关的。我想做的只是在用户内部获取数据。
我在这里犯了什么错误?我错过了什么?
更新:我把这行添加到Users.php
<?php
/**
* @var 'Acme'DemoBundle'Entity'Datas
*
* @ORM'OneToMany(targetEntity="Datas", mappedBy="datas", cascade={"all"})
*/
private $datas;
public function __construct()
{
$this->datas = new 'Doctrine'Common'Collections'ArrayCollection();
}
/**
* Add phrases
*
* @param Acme'DemoBundle'Entity'Datas $datas
*/
public function addPhrases('Acme'DemoBundle'Entity'Datas $datas)
{
$this->datas[] = $datas;
}
/**
* Get datas
*
* @return Doctrine'Common'Collections'Collection
*/
public function getDatas()
{
return $this->datas;
}
?>
而这些线到Datas.php
<?php
/**
* @ORM'ManyToOne(targetEntity="Users", inversedBy="users", cascade={"all"})
*/
protected $users;
/**
* Set users
*
* @param Acme'DemoBundle'Entity'Users $users
*/
public function setUsers('Acme'DemoBundle'Entity'Users $users)
{
$this->users = $users;
}
/**
* Get users
*
* @return Acme'DemoBundle'Entity'Users
*/
public function getUsers()
{
return $this->users;
}
?>
现在getDatas()
在工作,但里面没有$user->getDatas()->getPhrase();
。我收到此错误:
Call to undefined method Doctrine'ORM'PersistentCollection::getPhrase()
结论:我收到集合错误,因为它返回集合 - 当然 - 。迭代(如foreach(,您将访问数据。(如果您遇到这样的问题。
如果查看@JoinColumn
的文档
此批注用于@ManyToOne、@OneToOne字段中的关系上下文以及嵌套在@ManyToMany中的@JoinTable上下文中。此注释不是必需的。如果未指定,则从表和主键名称推断属性名称和引用的列名称。
因此,当您使用ManyToOne
关系时,您的关系定义将是,
/**
* @var Users
*
* @ORM'ManyToOne(targetEntity="Users")
* @ORM'JoinColumn(name="useridx", referencedColumnName="idx")
*/
private $useridx;
编辑:
如果你想从用户端获取数据,那么你必须创建OneToMulti关系。
在数据中.php
/**
* @var Users
*
* @ORM'ManyToOne(targetEntity="Users", inversedBy = "datas")
* @ORM'JoinColumn(name="useridx", referencedColumnName="idx")
*/
private $useridx;
在"用户"中.php添加以下行,
/**
* @ORM'OneToMany(targetEntity="Datas", mappedBy="useridx", cascade={"persist"})
*/
protected $datas;
然后执行doctrine:generate:entities
命令。要对关系进行操作,请检查此文档条目。