与Doctrine 2的一对一关系无法正常工作(处于保持状态)

问题描述 投票:0回答:5

我对准则2有点大问题,我没有在Symfony中使用它,而是在将它用作库。

代码比说长话要好:)

例如,我声明了2个实体。

<?php
namespace Evengyl\Entity\User;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(
        name = "address",
    )
*/
class Address
{

    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue
    */
    protected $id;


    /**
    * @ORM\Column(type="string", name="localite")
    */
    protected $localite;


    /**
    * @ORM\OneToOne(targetEntity=User::class, cascade={"persist", "remove"}, mappedBy="address")
    */
    protected $user;
    // getters et setters


    public function getUser()
    {
        return $this->user;
    }

    public function setUser($user)
    {
        $this->user = $user;
    }


    public function getId()
    {
        return $this->id;
    }


    public function getLocalite()
    {
        return $this->localite;
    }

    public function setLocalite($localite)
    {
        $this->localite = $localite;
    }

}
<?
namespace Evengyl\Entity\User;


use Doctrine\ORM\Mapping as ORM;


/**
* @ORM\Entity
* @ORM\Table(
        name = "users",
        indexes = {
            @ORM\Index(name = "search_name_lastname", columns = {"name", "last_name"}),
            @ORM\Index(name = "search_role", columns = {"role"})
        }
    )
*/
class User
{

    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue
    */
    protected $id;


    /**
    * @ORM\Column(type="string", name="name")
    */
    protected $name;


    // getters et setters


    /**
    * @ORM\OneToOne(targetEntity=Address::class, cascade={"persist", "remove"}, inversedBy="user")
    */
    private $address;


    public function getAddress()
    {
        return $this->address;
    }

    public function setAddress($address)
    {
        $this->address = $address;
    }

    public function getId()
    {
        return $this->id;
    }


    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

并且我的索引测试类包含

require_once "entity/User.php";
require_once "entity/Address.php";

use Evengyl\Entity\User\User;
use Evengyl\Entity\User\Address;

$userRepo = $entityManager->getRepository(User::class);
$user = $userRepo->find(1);


affiche($user->getAddress());

但是,应要求,我得到了,这就是OneToOne。。

Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 67112960 bytes) in C:\xampp\htdocs\test_doctrine\index.php

我不明白...

模式与命令正确,我的SDB与命令正确。。

我需要您的帮助!

php orm doctrine-orm doctrine one-to-one
5个回答
0
投票

我的关系,* @ORM\JoinColumn(name="address_id", referencedColumnName="id")

但是,即使有了这种关系,它也不会改变任何东西,我使用了学说调试,当我执行getAddress()时得到了这个]


0
投票

但是,当我尝试这个时


0
投票

但是我的关系正确吗?


0
投票

我很快从Symfony Entity Maker


0
投票

甘德,我正在使用您的代码,并为我的文件夹调整名称空间,谢玛还可以,但是当我执行此操作时>

$adRepo = $entityManager->getRepository(User::class);
$ad = $adRepo->findOneBy(["id" => 1]);


67 : ?><pre><?= print_r($ad,1); ?></pre><?
© www.soinside.com 2019 - 2024. All rights reserved.