Symfony4&学说:加入不起作用

时间:2018-05-28 19:23:53

标签: doctrine symfony4

我正在尝试Symfony4,Doctrine& PGSQL 我有拥有许多用户的客户,我有一个登录页面和一个个人资料页面(只转储$ user记录和$ user-> getCustomer())

数据库包含: 客户:

+----+------------+
| id |    name    |
+----+------------+
|  2 | Customer 1 |
+----+------------+

用户:

+----+-------------+----------+--------------------------------------------------------------+-----------+
| id | customer_id | username |                           password                           | is_active |
+----+-------------+----------+--------------------------------------------------------------+-----------+
|  1 |           2 | root     | $2y$12$s.J4LbCNSKk6dssRrS2yteBGpQT192JvayICEv9zVfax3DykvR5yW | t         |
+----+-------------+----------+--------------------------------------------------------------+-----------+

当我登录并转到/ profile时,我可以看到用户:

User {#81545 ▼
  -id: 1
  -username: "root"
  -password: "$2y$12$s.J4LbCNSKk6dssRrS2yteBGpQT192JvayICEv9zVfax3DykvR5yW"
  -isActive: true
  -customer: Customer {#81604 ▼
    +__isInitialized__: false
    -id: 2
    -name: null
    -users: null
     …2
  }
}

但是客户的名字是空的。 为什么在客户对象中id正确设置但其他字段不正确? 我如何检索“完整”对象

控制器看起来像:

    /**
     * @Route("/profile")
     */
    public function profile(): Response
    {
        return $this->render('admin/home.html.twig', array(
                    'me' => $this->getUser(),
                    'customer' => null,
        ));
    }

但是我将控制器更新为(仅添加$ customerRepository-> findOneBy)

    /**
     * @Route("/profile")
     */
    public function profile(\App\Repository\CustomerRepository $customerRepository): Response
    {
        $unused = $customerRepository->findOneBy(['id' => $this->getUser()->getCustomer()->getId()]);

    return $this->render('admin/home.html.twig', array(
                'me' => $this->getUser(),
                'customer' => null,
    ));
}

现在正确设置了客户字段。 我不明白

此处是个人资料页面的树枝模板:

{% extends "base.html.twig" %}
{% block body %}
    <b>Authenticated !</b><br/>
    {{ dump(me) }}
    {{ dump(me.customer) }}
{% endblock %}

客户实施:

namespace App\Accounting;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="customers",schema="test")
 * @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
 */
class Customer implements \Serializable
{

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

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

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="customer")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=true)
     */
    private $users;

    public function __construct()
    {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // all setters & getters
}

用户:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Table(name="users",schema="test")
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User implements UserInterface, \Serializable
{

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

    /**
     * @ORM\Column(type="string", unique=true)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=64)
     */
    private $password;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="users")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=true)
     */
    private $customer;

    // all setters & getters

    public function eraseCredentials()
    { 
    }

    /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
                // see section on salt below
                // $this->salt,
        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list (
                $this->id,
                $this->username,
                $this->password,
                // see section on salt below
                // $this->salt
                ) = unserialize($serialized, ['allowed_classes' => false]);
    }

}

1 个答案:

答案 0 :(得分:0)

这称为lazy loading。 这意味着你的obejcts的关系不会被初始化,直到你不需要它们。它用于优化内存和数据库请求。您可以看到ID集,但也可以看到+__isInitialized__: false。首次通过$user->getCustomer()方法要求客户时,orm将执行查询以检索对象。

在树枝模板中,您可以使用属性,即使它们是私有的,如您的情况。

所以将此添加到您的用户类

public function getCustomer(){
    return $this->customer;
}

无论何时您需要“客户”对象,都可以使用该方法获取它。