2つのテーブルがあります。最初のテーブルはusers
で、2番目のテーブルはですdatas
。データには、 'suseridx
とは異なる列があります。(プライマリ一意キー)。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
私はこの結果を得ました:
Datas.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;
}
}
?>
Users.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のように)反復すると、データにアクセスできます。(このような問題が発生した場合。)