简体   繁体   English

通过学说实体插入外键?

[英]inserting foreign key via doctrine entity?

I'm using Doctrine2+CodeIgniter2, and am attempting to create a simple test of a join table. 我正在使用Doctrine2 + CodeIgniter2,并尝试创建一个简单的联接表测试。

Here is my schema for the two tables involved: 这是我涉及的两个表的架构:

CREATE TABLE test_lastnames (id INT AUTO_INCREMENT NOT NULL, last_name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; 创建表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; 创建表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 ADD CONSTRAINT FK_23D7305696EC0FA4 FOREIGN KEY (mylastname_id) REFERENCES test_lastnames (id) ALTER TABLE test_firstnames添加约束FK_23D7305696EC0FA4外键(mylastname_id)参考test_lastnames(id)

Here are my YAML mappings 这是我的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

and

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

I am attempting to write data to the tables. 我正在尝试将数据写入表。

$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();

It returns the following errors: 它返回以下错误:

Message: Argument 1 passed to ORM\\Testing\\Firstnames::setMylastname() must be an instance of ORM\\Testing\\Lastnames, integer given, called in /[PATH]/applicationFolder/controllers/testing/test_namejoins_insert.php on line 31 and defined 消息:传递给ORM \\ Testing \\ Firstnames :: setMylastname()的参数1必须是ORM \\ Testing \\ Lastnames的实例,给定整数,在第[31]行的/[PATH]/applicationFolder/controllers/testing/test_namejoins_insert.php中调用定义的

Filename: Testing/Firstnames.php 文件名:Testing / Firstnames.php

Line Number: 66 行号:66

As well as a bunch of Message: spl_object_hash() expects parameter 1 to be object, integer given errors. 以及一堆Message: spl_object_hash() expects parameter 1 to be object, integer given错误。

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

I have not started hacking at it - is the problem right there with '$mylastname = null'? 我还没有开始对此进行黑客攻击-'$ mylastname = null'问题在那里吗?

How does one insert a foreign key value by entity? 如何按实体插入外键值?

$new_firstname->setMyLastName($new_lastname);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM