0
votes

I'm using Doctrine2+CodeIgniter2, and am attempting to create a simple test of a join table.

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;

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 ADD CONSTRAINT FK_23D7305696EC0FA4 FOREIGN KEY (mylastname_id) REFERENCES test_lastnames (id)

Here are my YAML mappings

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

Filename: Testing/Firstnames.php

Line Number: 66

As well as a bunch of Message: spl_object_hash() expects parameter 1 to be object, integer given errors.

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

I have not started hacking at it - is the problem right there with '$mylastname = null'?

How does one insert a foreign key value by entity?

1

1 Answers

1
votes
$new_firstname->setMyLastName($new_lastname);

rather than $new_firstname->setMyLastName($new_lastname ->getID());