【问题标题】:PHPStorm Symfony/Twig ignore a getter from entityPHPStorm Symfony/Twig 忽略来自实体的 getter
【发布时间】:2020-03-25 16:31:11
【问题描述】:

我正在使用带有 Symfony 插件及其依赖项的最新 PhpStorm 版本,但在 Twig 实体方法完成方面遇到了一些问题。

这是我的User 实体(运行良好)

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use App\Entity\UserInterface as BaseInterface;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @UniqueEntity(fields={"email"}, message="Adresse mail déjà utilisée")
 */
class User implements BaseInterface, \Serializable
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];

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

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Profil", mappedBy="user", cascade={"persist", "remove"})
     */
    private $profil;

    // ...
    // setters and getters auto-generated
}

还有我的控制器操作:


/**
 * @Route("/user", name="user_")
 * @IsGranted("IS_AUTHENTICATED_REMEMBERED")
 */
class UserController extends AbstractController
{
    /**
     * @Route("/profil", name="profil")
     */
    public function profil()
    {
        // I tried to load it from doctrine repository and also tried to add @var Profil comment
        $user = $this->getUser();

        return $this->render('security/user/profil.html.twig', [
            'user' => $user
        ]);
    }

    // ...
}

最后是我的模板:

{% block user_content %}
    <div class="card">
        <div class="card-body">
            <table class="table table-hover tabless">
                <tr>
                    <td>Email</td>
                    <td>{{ user.username }}</td>
                </tr>
                <tr>
                    <td>Username</td>
                    <td>{{ user.profil.username }}</td>
                </tr>
                <tr>
                    <td>Date Inscription</td>
                    <td>{{ user.profil.createdAt | date('d/m/Y') }}</td>
                </tr>
                <tr>
                    <td>Dernière Modification</td>
                    <td>{{ user.profil.updatedAt | date('d/m/Y') }}</td>
                </tr>
                <tr>
                    <td>Roles</td>
                    <td>
                        {% for role in user.roles %}
                            <span class="badge badge-primary badge-">{{ role|trans }}</span>
                        {% endfor %}
                    </td>
                </tr>
                <tr>
                    <td>Description</td>
                    <td>{{ user.profil.description }}</td>
                </tr>
            </table>
        </div>
    </div>
{% endblock %}

我的问题是在树枝模板中,PhpStorm 没有从用户变量中找到配置文件获取器。我试图查看用户变量引用,它会将我发送到 Symfony UserInterface,而不是我的,这就是为什么我尝试使用 getProfil() 函数创建自己的。

最奇怪的是,如果我这样做 {{ app.user.profil }} 它完全可以工作并自动完成它。

【问题讨论】:

  • PhpStorm 的 Twig 支持无法从您的 Symfony 控制器中获取变量(因为它不提供自己的框架集成)——这就是 Symfony 插件对 Twig 的额外支持所做的(或应该做的)。 无论如何: Symfony 插件支持 Twig 文件中的类型提示(内联 @var PHPDoc cmets)——请尝试在您的 .twig 文件中为您的 user 变量添加此类类型提示注释:symfony2-plugin.espend.de/languages/twig/index.html#phptypes

标签: php symfony twig phpstorm


【解决方案1】:

只需在控制器中添加注解

   use App\Entity\User;


   /** @var User $user */
   $user = $this->getUser();

【讨论】:

  • 我在我的代码中添加了一条注释来解释我已经测试过了。所以,不,它没有让它工作。
  • 您是否使用“use”导入了用户类?
  • 是的,我也尝试在树枝模板中添加评论
  • 您确定当前项目启用了插件 symfony 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 2017-02-07
  • 2022-01-20
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
相关资源
最近更新 更多