php - Symfony2 的 Doctrine 不会为我创建一对多结构

标签 php symfony doctrine-orm

我有两张 table 。第一个表是 users,第二个是 datas。 Datas 有 useridx 列,它与 useridx 不同。 (主唯一键)。

这些是表结构:

用户

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

我得到了这个结果:

数据.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 文件。

现在,当我在我的 Controller 中运行这个命令时:

<?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)它,您将访问数据。 (如果你遇到这样的问题。)

最佳答案

如果查看documentation属于@JoinColumn

此注释用于@ManyToOne、@OneToOne 字段中的关系上下文嵌套在@ManyToMany 中的@JoinTable 上下文。此注释不是必需的。如果未指定属性名称和 referencedColumnName 从表和主键名称推断。

因此,当您使用 ManyToOne 关系时,您的关系定义将是,

/**
 * @var Users
 *
 * @ORM\ManyToOne(targetEntity="Users")
 * @ORM\JoinColumn(name="useridx", referencedColumnName="idx")
 */
private $useridx;

编辑:

如果你想从用户端获取数据,那么你必须创建OneToMany关系。例如

在 Datas.php 中

/**
 * @var Users
 *
 * @ORM\ManyToOne(targetEntity="Users", inversedBy = "datas")
 * @ORM\JoinColumn(name="useridx", referencedColumnName="idx")
 */
private $useridx;

然后在 Users.php 中添加以下行,

/**
 * @ORM\OneToMany(targetEntity="Datas", mappedBy="useridx", cascade={"persist"})
 */
protected $datas;

然后执行 doctrine:generate:entities 命令。对关系检查做操作this文档条目。

关于php - Symfony2 的 Doctrine 不会为我创建一对多结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10461685/

相关文章:

php - Symfony 以数组格式设置请求参数

symfony - 如何在 Symfony 中设置 Doctrine 的类继承?

php - 我如何使用 make :migration with multiple entity managers?

date - 在 Doctrine Querybuilder 中使用 `DATE()`

php - 为 phpdoc 声明大量变量而不用/**开头

php - 如何从 BlueMix 中的 .php 应用程序连接到 SQL Database-s2

php - Composer 内存不足 - 需要替代方法来增加 PHP memory_limit

php - 如何修复因不正确的字节计数长度而损坏的序列化字符串?

php - Doctrine2 Entites - 是否可以将 "dirty"对象与数据库中的对象进行比较

php - Laravel Doctrine 仅从 findAll() 查询返回单个属性