Skip to content
Advertisement

Impossible to access an attribute on a string variable

In my controller i just pass some users on my twig view. Here is my function :

public function viewAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $planningRepo = $em->getRepository('SimonPediBundle:Planning');
        $userRepo = $em->getRepository('SimonUserBundle:User');

        $user = $this->getUser();
        $planningId = $user->getPlanning()->getId();
        $planning = $planningRepo->find($planningId);


        $users = $userRepo->findByPlanning($planningId);

        if (null === $planning) {
            throw new NotFoundHttpException("Le planning d'id ".$id. "n'existe pas.");
        }

        return $this->render('planningAction/planning.html.twig', array('planning' => $planning,
                                                                        'users' => $users,));
    }

And this is my twig view where i want to display some information on a html table :

<tbody>
                    <tr>
                    <th scope="row">Responsable</th>
                     {% for i in 1..5 %}
                        {% set assigned_user = ' ' %}
                        {% for user in users if user.planningday == i%}
                            {% set assigned_user = user %}
                        {% endfor %}
                        <td>{{assigned_user.name}} {{assigned_user.lastname}}</td>
                        {% endfor %}
                    </tr>
                    <tr>
                        <th scope="row">Description</th>
                        {% for i in 1..5 %}
                        {% set assigned_user = ' ' %}
                        {% for user in users if user.planningday == i%}
                            {% set assigned_user = user %}
                        {% endfor %}
                        <td>{{assigned_user.planningcontent}}</td>
                        {% endfor %}
                    </tr>
                </tbody>

And i got this error : Impossible to access an attribute (“name”) on a string variable (” “). for this line :

<td>{{assigned_user.name}} {{assigned_user.lastname}}</td>

Thanks for help!

And here is my User Entity

/**
 * User
 *
 * @ORMTable(name="user")
 * @ORMEntity(repositoryClass="SimonUserBundleRepositoryUserRepository")
 */
class User extends BaseUser
{
    /**
   * @ORMColumn(name="id", type="integer")
   * @ORMId
   * @ORMGeneratedValue(strategy="AUTO")
   */
   protected $id;
   /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", length=255)
     * @AssertNotBlank(message="Merci d'entrer votre prénom.", groups={"Registration", "Profile"})
     * @AssertLength(
     *     min=3,
     *     max=50,
     *     minMessage="Prénom trop court",
     *     maxMessage="Prénom trop long",
     *     groups={"Registration", "Profile"}
     * )
     */
    protected $name;

    /**
     * @var string
     *
     * @ORMColumn(name="lastname", type="string", length=255)
     * @AssertNotBlank(message="Merci d'entrer votre nom.", groups={"Registration", "Profile"})
     * @AssertLength(
     *     min=3,
     *     max=50,
     *     minMessage="Nom trop court",
     *     maxMessage="Nom trop long",
     *     groups={"Registration", "Profile"}
     * )
     */
    protected $lastname;
     /**
     * @var string
     *
     * @ORMColumn(name="phone", type="string", length=12)
     * @AssertNotBlank(message="Merci d'entrer un numéro de téléphone.", groups={"Registration", "Profile"}))
     * @AssertLength(
     *      min=10,
     *      max=10,
     *      minMessage="Entrez un numéro de téléphone valide",
     *      maxMessage="Entrez un numéro de téléphone valide",
     *      groups={"Registration", "Profile"}
     * )
     */
    protected $phone;
    /**
     * @var boolean
     *
     *@ORMColumn(name="hasAdvert", type="boolean", nullable=true)
     */
    protected $hasAdvert;
    /**
     * @var boolean
     *
     * @ORMColumn(name="isAdmin", type="boolean", nullable=true)
     */
    protected $isAdmin;
    /**
     *
     * @ORMManyToOne(targetEntity="SimonPediBundleEntityPlanning", cascade={"persist"})
     *
     */
    protected $planning;
    /**
     * @var int
     *
     * @ORMColumn(name="planningday", type="smallint", nullable= true)
     */
    protected $planningday;
    /**
     * @var text
     *
     * @ORMColumn(name="planningcontent", type="text", nullable= true)
     */
    protected $planningcontent;
    /**
     *@ var string
     * @ORMColumn(name="address", type="string", length=255)
     */
    protected $address;

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

    /**
     * Set name
     *
     * @param string $name
     *
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set lastname
     *
     * @param string $lastname
     *
     * @return User
     */
    public function setLastname($lastname)
    {
        $this->lastname = $lastname;

        return $this;
    }

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

    /**
     * Set phone
     *
     * @param string $phone
     *
     * @return User
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

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

    /**
     * Set hasAdvert
     *
     * @param boolean $hasAdvert
     *
     * @return User
     */
    public function setHasAdvert($hasAdvert)
    {
        $this->hasAdvert = $hasAdvert;

        return $this;
    }

    /**
     * Get hasAdvert
     *
     * @return boolean
     */
    public function getHasAdvert()
    {
        return $this->hasAdvert;
    }

    /**
     * Set isAdmin
     *
     * @param boolean $isAdmin
     *
     * @return User
     */
    public function setIsAdmin($isAdmin)
    {
        $this->isAdmin = $isAdmin;

        return $this;
    }

    /**
     * Get isAdmin
     *
     * @return boolean
     */
    public function getIsAdmin()
    {
        return $this->isAdmin;
    }

    /**
     * Set planningday
     *
     * @param integer $planningday
     *
     * @return User
     */
    public function setPlanningday($planningday)
    {
        $this->planningday = $planningday;

        return $this;
    }

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

    /**
     * Set planningcontent
     *
     * @param string $planningcontent
     *
     * @return User
     */
    public function setPlanningcontent($planningcontent)
    {
        $this->planningcontent = $planningcontent;

        return $this;
    }

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

    /**
     * Set address
     *
     * @param string $address
     *
     * @return User
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

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

    /**
     * Set planning
     *
     * @param SimonPediBundleEntityPlanning $planning
     *
     * @return User
     */
    public function setPlanning(SimonPediBundleEntityPlanning $planning = null)
    {
        $this->planning = $planning;

        return $this;
    }

    /**
     * Get planning
     *
     * @return SimonPediBundleEntityPlanning
     */
    public function getPlanning()
    {
        return $this->planning;
    }
}

Advertisement

Answer

If you are using objects there is no need to set default value to string. Set it to null and check wether an user was found

<tbody>
    <tr>
    <th scope="row">Responsable</th>
     {% for i in 1..5 %}
        {% set assigned_user = null %}
        {% for user in users
            {% if user.planningday == i%}
                {% set assigned_user = user %}
            {% endif %}
        {% endfor %}
        <td>
        {% if not assigned_user is null %}
            {{assigned_user.name}} {{assigned_user.lastname}}
        {% endif %}
        </td>
        {% endfor %}
    </tr>
    <tr>
        <th scope="row">Description</th>
        {% for i in 1..5 %}
            {% set assigned_user = null %}
            {% for user in users %}
                {% if user.planningday == i %}
                    {% set assigned_user = user %}
                {% endif %}
            {% endfor %}
            <td>
            {% if not assigned_user is null %}
                {{assigned_user.planningcontent }}
            {% endif %}     
            </td>
        {% endfor %}
    </tr>
</tbody>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement