0

我正在使用 Doctrine2+CodeIgniter2,并试图创建一个连接表的简单测试。

这是我涉及的两个表的架构:

CREATE TABLE test_lastnames (id INT AUTO_INCREMENT NOT NULL, last_name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;

CREATE TABLE test_firstnames (id INT AUTO_INCREMENT NOT NULL, mylastname_id INT DEFAULT NULL, first_name VARCHAR(255) NOT NULL, INDEX IDX_23D7305696EC0FA4 (mylastname_id), PRIMARY KEY(id)) ENGINE = InnoDB;

ALTER TABLE test_firstnames 添加约束 FK_23D7305696EC0FA4 外键 (mylastname_id) 参考 test_lastnames (id)

这是我的 YAML 映射

ORM\Testing\Firstnames:
  type: entity
  table: test_firstnames
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    firstname:
      type: string
      column: first_name
  manyToOne:
    mylastname:
      targetEntity: ORM\Testing\Lastnames

ORM\Testing\Lastnames:
  type: entity
  table: test_lastnames
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    lastname:
      type: string
      column: last_name

我正在尝试将数据写入表。

$new_lastname = new ORM\Testing\Lastnames;
$new_lastname -> setLastName ('Shakespear');
$this->doctrine->em->persist($new_lastname);
$this->doctrine->em->flush();

$new_firstname = new ORM\Testing\Firstnames;
$new_firstname->setFirstname('William');
$new_firstname->setMyLastName($new_lastname ->getID());
$this->doctrine->em->persist($new_firstname);
$this->doctrine->em->flush();

它返回以下错误:

消息:传递给 ORM\Testing\Firstnames::setMylastname() 的参数 1 必须是 ORM\Testing\Lastnames 的一个实例,给定整数,在第 31 行的 /[PATH]/applicationFolder/controllers/testing/test_namejoins_insert.php 中调用,并且定义

文件名:Testing/Firstnames.php

行号:66

以及一堆Message: spl_object_hash() expects parameter 1 to be object, integer given错误。

这是 Firstnames.php 的第 66 行: public function setMylastname(\ORM\Testing\Lastnames $mylastname = null)

我还没有开始破解它 - '$ mylastname = null'是否存在问题?

如何按实体插入外键值?

4

1 回答 1

1
$new_firstname->setMyLastName($new_lastname);

而不是$new_firstname->setMyLastName($new_lastname ->getID());

于 2012-04-20T18:51:49.367 回答