Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在基本视图(细枝)和#x2B中使用数据库中的数据;symfony2_Php_Symfony_View_Twig - Fatal编程技术网

Php 在基本视图(细枝)和#x2B中使用数据库中的数据;symfony2

Php 在基本视图(细枝)和#x2B中使用数据库中的数据;symfony2,php,symfony,view,twig,Php,Symfony,View,Twig,我有一个基本的视图,我在每一页上都使用它。我的基本观点是: <div class="container"> {% block body %} {% endblock %} </div> 但是我如何在我的基本视图中使用这些数据呢?(每页都会呈现) 更新: 我的用户实体: <?php namespace VolleyScout\VolleyScoutBundle\Entity; use Doctrine\ORM\Mapping as ORM

我有一个基本的视图,我在每一页上都使用它。我的基本观点是:

<div class="container">
    {% block body %}
    {% endblock %}
</div>
但是我如何在我的基本视图中使用这些数据呢?(每页都会呈现)

更新
我的用户实体:

    <?php

namespace VolleyScout\VolleyScoutBundle\Entity;

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

/**
 * Users
 *
 * @ORM\Table(name="users", indexes={@ORM\Index(name="fk_users_roles1_idx", columns={"role_id"})})
 * @ORM\Entity
 */
class Users implements AdvancedUserInterface
{
    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=45, nullable=false)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=60, nullable=false)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=30, nullable=false)
     */
    private $salt;

    /**
     * @var string
     *
     * @ORM\Column(name="user_firstname", type="string", length=45, nullable=false)
     */
    private $userFirstname;

    /**
     * @var string
     *
     * @ORM\Column(name="user_surname", type="string", length=255, nullable=false)
     */
    private $userSurname;

    /**
     * @var string
     *
     * @ORM\Column(name="user_email", type="string", length=255, nullable=false)
     */
    private $userEmail;

    /**
     * @var string
     *
     * @ORM\Column(name="user_type", type="string", nullable=false)
     */
    private $userType;

    /**
     * @var string
     *
     * @ORM\Column(name="user_token", type="string", length=45, nullable=true)
     */
    private $userToken;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_created", type="datetime", nullable=false)
     */
    private $userCreated;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_modified", type="datetime", nullable=true)
     */
    private $userModified;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_deleted", type="datetime", nullable=true)
     */
    private $userDeleted;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_lastlogin", type="datetime", nullable=true)
     */
    private $userLastlogin;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_confirmed", type="datetime", nullable=true)
     */
    private $userConfirmed;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="user_locked", type="datetime", nullable=true)
     */
    private $userLocked;

    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $userId;

    /**
     * @var \VolleyScout\VolleyScoutBundle\Entity\Roles
     *
     * @ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Roles")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
     * })
     */
    protected $role;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams", inversedBy="user")
     * @ORM\JoinTable(name="user_follows_teams",
     *   joinColumns={
     *     @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
     *   }
     * )
     */
    private $team;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Competitions", inversedBy="user")
     * @ORM\JoinTable(name="user_follows_competitions",
     *   joinColumns={
     *     @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="competition_id", referencedColumnName="competition_id")
     *   }
     * )
     */
    private $competition;

    private $plainPassword;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->team = new \Doctrine\Common\Collections\ArrayCollection();
        $this->competition = new \Doctrine\Common\Collections\ArrayCollection();
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }


    /**
     * Set username
     *
     * @param string $username
     * @return Users
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return Users
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set salt
     *
     * @param string $salt
     * @return Users
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

    /**
     * Get salt
     *
     * @return string
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Set userFirstname
     *
     * @param string $userFirstname
     * @return Users
     */
    public function setUserFirstname($userFirstname)
    {
        $this->userFirstname = $userFirstname;

        return $this;
    }

    /**
     * Get userFirstname
     *
     * @return string
     */
    public function getUserFirstname()
    {
        return $this->userFirstname;
    }

    /**
     * Set userSurname
     *
     * @param string $userSurname
     * @return Users
     */
    public function setUserSurname($userSurname)
    {
        $this->userSurname = $userSurname;

        return $this;
    }

    /**
     * Get userSurname
     *
     * @return string
     */
    public function getUserSurname()
    {
        return $this->userSurname;
    }

    /**
     * Set userEmail
     *
     * @param string $userEmail
     * @return Users
     */
    public function setUserEmail($userEmail)
    {
        $this->userEmail = $userEmail;

        return $this;
    }

    /**
     * Get userEmail
     *
     * @return string
     */
    public function getUserEmail()
    {
        return $this->userEmail;
    }

    /**
     * Set userType
     *
     * @param string $userType
     * @return Users
     */
    public function setUserType($userType)
    {
        $this->userType = $userType;

        return $this;
    }

    /**
     * Get userType
     *
     * @return string
     */
    public function getUserType()
    {
        return $this->userType;
    }

    /**
     * Set userToken
     *
     * @param string $userToken
     * @return Users
     */
    public function setUserToken($userToken)
    {
        $this->userToken = $userToken;

        return $this;
    }

    /**
     * Get userToken
     *
     * @return string
     */
    public function getUserToken()
    {
        return $this->userToken;
    }

    /**
     * Set userCreated
     *
     * @param \DateTime $userCreated
     * @return Users
     */
    public function setUserCreated($userCreated)
    {
        $this->userCreated = $userCreated;

        return $this;
    }

    /**
     * Get userCreated
     *
     * @return \DateTime
     */
    public function getUserCreated()
    {
        return $this->userCreated;
    }

    /**
     * Set userModified
     *
     * @param \DateTime $userModified
     * @return Users
     */
    public function setUserModified($userModified)
    {
        $this->userModified = $userModified;

        return $this;
    }

    /**
     * Get userModified
     *
     * @return \DateTime
     */
    public function getUserModified()
    {
        return $this->userModified;
    }

    /**
     * Set userDeleted
     *
     * @param \DateTime $userDeleted
     * @return Users
     */
    public function setUserDeleted($userDeleted)
    {
        $this->userDeleted = $userDeleted;

        return $this;
    }

    /**
     * Get userDeleted
     *
     * @return \DateTime
     */
    public function getUserDeleted()
    {
        return $this->userDeleted;
    }

    /**
     * Set userLastlogin
     *
     * @param \DateTime $userLastlogin
     * @return Users
     */
    public function setUserLastlogin($userLastlogin)
    {
        $this->userLastlogin = $userLastlogin;

        return $this;
    }

    /**
     * Get userLastlogin
     *
     * @return \DateTime
     */
    public function getUserLastlogin()
    {
        return $this->userLastlogin;
    }

    /**
     * Set userConfirmed
     *
     * @param \DateTime $userConfirmed
     * @return Users
     */
    public function setUserConfirmed($userConfirmed)
    {
        $this->userConfirmed = $userConfirmed;

        return $this;
    }

    /**
     * Get userConfirmed
     *
     * @return \DateTime
     */
    public function getUserConfirmed()
    {
        return $this->userConfirmed;
    }

    /**
     * Set userLocked
     *
     * @param \DateTime $userLocked
     * @return Users
     */
    public function setUserLocked($userLocked)
    {
        $this->userLocked = $userLocked;

        return $this;
    }

    /**
     * Get userLocked
     *
     * @return \DateTime
     */
    public function getUserLocked()
    {
        return $this->userLocked;
    }

    /**
     * Get userId
     *
     * @return integer
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * Set role
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Roles $role
     * @return Users
     */
    public function setRoles(\VolleyScout\VolleyScoutBundle\Entity\Roles $role = null)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get role
     *
     * @return \VolleyScout\VolleyScoutBundle\Entity\Roles
     */
    public function getRoles()
    {
        return array($this->role->getRoleName());
    }

    /**
     * Add team
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
     * @return Users
     */
    public function addTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
    {
        $this->team[] = $team;

        return $this;
    }

    /**
     * Remove team
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
     */
    public function removeTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
    {
        $this->team->removeElement($team);
    }

    /**
     * Get team
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTeam()
    {
        return $this->team;
    }

    /**
     * Add competition
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
     * @return Users
     */
    public function addCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
    {
        $this->competition[] = $competition;

        return $this;
    }

    /**
     * Remove competition
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
     */
    public function removeCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
    {
        $this->competition->removeElement($competition);
    }

    /**
     * Get competition
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCompetition()
    {
        return $this->competition;
    }

    //**********************************
    //  HAD TO IMPLEMENT THESE BY MESELF
    //**********************************//

    private $player;

    /**
     * Get player
     *
     * @return \VolleyScout\VolleyScoutBundle\Entity\Players
     */

    public function getPlayer() {
        return $this->player;
    }
    /**
     * Set player
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Players $player
     * @return Users
     */
    public function setPlayer(\VolleyScout\VolleyScoutBundle\Entity\Players $player = null){
        $this->player = $player;

        return $this;
    }

    public function eraseCredentials()
    {
        $this->setPlainPassword(null);
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($plainPassword)
    {
        $this->plainPassword = $plainPassword;
    }


    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isAccountNonExpired()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isAccountNonLocked()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isEnabled()
    {
        // CHECK IF $this->confirmed is not null
        if($this->userConfirmed != null){
            return true;
        }
    }
}

当前登录的用户存储在twig全局变量
app.user

{% if app.user.player.team %}
    show button
{% else %}
    not
{% endif %}

然后我得到了这个错误:不可能访问空变量(“”)上的属性(“myteam”),但是玩家总是空的,而且当一个玩家有一个用户ID时,你的关系就被破坏了。我不得不添加player和getPlayer(),setPlayer()因为自动生成的映射没有添加它,所以我将自己添加到用户实体。但它仍然提供NULL。。我将我的用户实体添加到我的开始帖子中。
{% if app.user.player.team %}
    show button
{% else %}
    not
{% endif %}