Question

I got problem when I want to fetch data from the table that have OneToOne relationships. Here I have table tbl_userand I make a relationship to a table named tbl_admin.

User.php

<?php

namespace Eifl\MainBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 * @ORM\Entity
 * @ORM\Table(name="tbl_user")
 */
class User extends BaseUser
{
    /**
     * @var string
     * @ORM\Id
     * @ORM\Column(name="user_id",type="string")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="Eifl\AdminBundle\Entity\Admin", mappedBy="userAdmin", cascade="remove")
     */
    public $userAdminType;

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

Admin.php

<?php

namespace Eifl\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Admin
 *
 * @ORM\Entity
 * @ORM\Table(name="tbl_admin")
 * @ORM\Entity(repositoryClass="Eifl\AdminBundle\Entity\AdminRepository")
 */
Class Admin
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="Eifl\MainBundle\Entity\User", inversedBy="userAdminType")
     * @ORM\JoinColumn(name="User_Admin", referencedColumnName="user_id")
     */
    public $userAdmin;

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

    /**
     * @return string
     */
    public function getUserAdmin()
    {
        return $this->userAdmin;
    }

    /**
     * @param mixed $userAdmin
     * @return string
     */
    public function setUserAdmin($userAdmin)
    {
        $this->userAdmin = $userAdmin;

        return $this;
    }

    /**
     * @param string $position
     * @return string
     */
    public function setPosition($position)
    {
        $this->position = $position;

        return $this;
    }

    /**
     * @return string
     */
    public function getPosition()
    {
        return $this->position;
    }

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

Now, in my controller.php, when I fetch data from admin with this code:

$admin = $this->getDoctrine()->getRepository('EiflAdminBundle:Admin')->findOneBy(array('userAdmin'=>'MyIdUser'));

$username= $admin->getUserAdmin()->getUsername();

I can use the function getUserAdmin() to fetch some function that I declared in class user such as function getUsername() to get the username or function getId() to get the user id. But the code above only fetch one record. I want to fetch multiple record from table admin, so I change the code above into these code:

$admin = $this->getDoctrine()->getRepository('EiflAdminBundle:Admin')->findAll();

$username= $admin->getUserAdmin()->getUsername();

And I got the problem. The error shows:

Error: Call to a member function getUserAdmin() on a non-object in ...

I can't use the function getUserAdmin() to call another function in class User. How can I fix this problem?

Note: the function of class user extends from FOSUserBundle

Was it helpful?

Solution

findAll returns an array. For example this will work:

$admins = $this->getDoctrine()->getRepository('EiflAdminBundle:Admin')->findAll();

$usernames = array();
foreach ($admins as $admin) {
    $usernames[] = $admin->getUserAdmin()->getUsername();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top