我正在使用Doctrine2 + CodeIgniter2,并试图创建一个连接表的简单测试。通过学说实体插入外键?
这里是我所涉及的两个表的模式:
CREATE TABLE test_lastnames(ID INT AUTO_INCREMENT NOT NULL,姓氏 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 FOREIGN KEY(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 \测试\ Firstnames参数1 :: setMylastname()必须ORM \测试\ Lastnames,整数给出的实例,叫做在/[PATH]/applicationFolder/controllers/testing/test_namejoins_insert.php上线31和限定
文件名:测试/ Firstnames.php
行号:66
以及一堆Message: spl_object_hash() expects parameter 1 to be object, integer given
错误。
这里是线66 Firstnames.php:public function setMylastname(\ORM\Testing\Lastnames $mylastname = null)
我还没有开始它黑客 - 是问题就在那里用 '$ mylastname = NULL'?
如何通过实体插入外键值?