我有两张桌子。第一个表是users
,第二个是datas
。Datas 的useridx
列与user
's是外来的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...
。我知道这很清楚。在Users.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)它,您将访问数据。(如果你遇到这样的问题。)