samedi 25 juin 2016

Eloquent Relationship with another entity

I have a User entity that has several fields. A user can have a referrer, but may have none. I have a Referrer table that has the following format:

id - int
date_creation - datetime
user_id - int
referrer_id - int

The table has the following entity definition:

namespace App\Entities;

use Doctrine\ORM\Mapping AS ORM;
use LaravelDoctrine\Extensions\Timestamps\Timestamps;
use DateTime;
use URL;

/**
 * @ORM\Entity
 * @ORM\Table(name="user_referral")
 */
class Referrer
{
    use Timestamps;

    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User", fetch="EAGER")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User", fetch="EAGER")
     * @ORM\JoinColumn(name="referrer_id", referencedColumnName="id")
     */
    protected $referrer;

    /**
     * @var DateTime
     * 
     * @ORM\Column(name="date_creation", type="datetime")
     */
    protected $dateCreation;

    /**
     * Auction constructor.
     */
    public function __construct()
    {
    }

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

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

    public function setUser($data)
    {
        $this->user = $data;
    }

    public function getReferrer()
    {
        return $this->referrer;
    }

    public function setReferrer($data)
    {
        $this->referrer = $data;
    }

    public function getDateCreation()
    {
        return $this->dateCreation;
    }

    public function setDateCreation(DateTime $data)
    {
        $this->dateCreation = $data;
    }

}

I added a field to my User definition, so that when I get a user the object would automatically be populated with the corresponding referrer, if it exists. So, I added this to the User entity definition:

/**
 * @var Referrer
 *
 * @ORM\ManyToOne(targetEntity="Referrer", fetch="EAGER")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $referrer;

This generates an error when I try to save a user, since 'user_id' is not a field. I know that, since there is no direct mapping in the User table to this field. In SQL, it would be a left join with the referrer table, which would result in null if the user has no referrer.

I do not know how to represent this relation in laravel, so that there is no representation on the User table to the referrer, but it is only joined when the user is retrieved from the database. Then, when I create a new User, I also create the respective Referrer to be added to the database, if one should be created.

How can I achieve this?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/28V0MrE
via IFTTT

Aucun commentaire:

Enregistrer un commentaire