Symfony2的Doctrine并没有为我创建一对多的结构

时间:2012-05-05 12:06:44

标签: php symfony doctrine-orm

我有两张桌子。第一个表格为users,第二个表格为datas。数据包含useridx列,其中user的{​​{1}}为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)它,你将访问数据。 (如果遇到这样的问题。)

1 个答案:

答案 0 :(得分:3)

如果查看@JoinColumn

documentation

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

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

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

编辑:

如果您想从用户端获取数据,则必须创建OneToMany关系。 e.g

在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 doc entry执行操作。