Symfony实体为空ID

时间:2017-06-02 23:52:53

标签: php symfony orm doctrine

我是Symfony 3的初学者并且遇到了一些严重的问题。

首先,让我们看看我的实体classess。

Client.php< - Bundle / Entity / Client.php

class Client
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    ...

    /**
     * @ORM\OneToMany(targetEntity="ClientBundle\Entity\Client\Address", mappedBy="cliEntity")
     */
    private $addresses;
}

Address.php< - Bundle / Entity / Client / Address.php

class Address
{
    /* same as Client one annotations */
    private $id;

    ...

    /**
     * ID of client that address related to
     * @ORM\Column(type="integer")
     */
    private $client_id;

    ...

    /**
     * @ORM\ManyToOne(targetEntity="ClientBundle\Entity\Client", inversedBy="addresses", cascade={"remove"})
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
     */
     private $cliEntity;
}

在控制器中:

$client = new Client();
$client->set(...)

$em->persist($client);
$em->flush();

/* var_dump($client->getId()); <-- returns 40, 41, 42, etc. */

$addr = new Address();
$addr->setClient($client->getId()); /* There is null, why? */

2 个答案:

答案 0 :(得分:0)

你可以试试这个

$addr->setClient($client);

最后这个

$em->flush();

关系不需要getId()。您可以使用带有教义ORM的对象。

答案 1 :(得分:0)

Duh,我找到了解决方案:

我在地址实体中添加了这个功能:

//uteSQL is my multiline textbox

string[] lines = this.uteSQL.Text.Split(new Char[] { '\n' }, 
StringSplitOptions.RemoveEmptyEntries);

int countLines = lines.Length;

int cursorPos = uteSQL.SelectionStart;

  for(int t = uteSQL.SelectionStart; t <= countLines ; t++)
  {
     if ( find ';' character that i want every lines)
      {
          -- if it finds get the line number
          break;
      }
   }

然后在控制器中:

    public function setCliEntity($cli)
    {
        $this->cliEntity = $cli;
        return $this;
    }

无论如何,感谢您的帮助