Search code examples
symfonydoctrineentity-relationship

symfony 2 Error:Call to a member function * on a non-object in *


I have an entity Users

class Users
{
  //...

/**
 * @ORM\Column(name="firstName", type="string", length=50, nullable=true)
 */
private $firstname;

public function getFirstname()
{
    return $this->firstname;
}

/*
*@ORM\OneToMany(targetEntity='Worksamples', mappedBy='user')
*/
private $worksample;

public function __constraction()
{
    $this->worksample = new ArrayCollection();
}

public function getWorksample()
{
    $this->worksample;
}

}

and another one entity called Worksamples

class Worksamples
{
 //...

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

public function getSamplesource()
{
    return $this->samplesource;
}

/**
 * @ORM\Column(name="UserId", type="integer", nullable=false)
 */
private $userid;   

public function getUserid()
{
    return $this->userid;
}


/*
*@ORM\ManyToOne(targetEntity="Users", inversedBy="worksample")
*@ORM\JoinColumn(name="UserId", referencedColumnName="id")
*/
private $user;

public function getUser()
{
    return $this->user;
}

}

in my controller i have this action

public function indexAction($id)
{
    $user = $this->getDoctrine()
        ->getRepository('AcmeWellcomeBundle:Users')
        ->find($id);

    $sample = $user->getWorksample()->getSamplesource();

    return $this->render('AcmeWellcomeBundle:Default:index.html.twig', array('sample' => $sample));

}

and I have this error

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

it supposed a User has many Worksamples and a Worksample has only one User.

any help?


Solution

  • Before using your code

    Did you run your classes through the console of your app? If not execute this in your terminal or a console through SSH on your Server:

    php app/console doctrine:generate:entities [YourVendorName]
    

    Afterwards update your database using:

    php app/console doctrine:schema:update --force
    

    And clear caches:

    app/console cache:clear
    

    Those steps will make sure that all annotations are actually used and your database is set up correctly.

    Then: You don't need private $userid; in Worksamples and you don't need $this->worksample = new ArrayCollection(); in Users. Doctrine will handle all this for your. Also it would be good to rename $worksample to $worksamples as it will always return an arrayCollection and never a single object.

    When all is set up correctly, you can simply use $workspace->getUser() which will return an object of class User attached to this specific object.

    In your controller

    First of all get the list of worksamples. Then check whether any worksamples are attached to the object. Then for example get the vlue from the first object in the list:

    $samples = $user->getWorksamples();
    if ($samples) {
        $sample = $samples[0]->getSamplesource();
    }
    

    Some notes:

    • The constructor of a class is called __construct() and not __constraction().
    • As a single object represents a user or a workspace, your classes should also be named User and Wokrspace in singular.