1
votes

I have a problem with Doctrine2 in Symfony2 and two relationed entities.

I have a class Users

class Users
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=50)
 */
private $username;

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

/**
 * @var boolean
 *
 * @ORM\Column(name="type", type="boolean")
 */
private $type;

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

/**
 * @var string
 *
 * @ORM\Column(name="first_name", type="string", length=100)
 */
private $firstName;

/**
 * @var string
 *
 * @ORM\Column(name="last_name", type="string", length=100)
 */
private $lastName;

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

/**
 * @var string
 *
 * @ORM\Column(name="url_img", type="string", length=255)
 */
private $urlImg;

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

/**
 * @ORM\OneToOne(targetEntity="Providers", mappedBy="userId", cascade={"persist"})
 */
protected $provider;


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

/**
 * 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 type
 *
 * @param boolean $type
 * @return Users
 */
public function setType($type)
{
    $this->type = $type;

    return $this;
}

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

/**
 * Set idPrest
 *
 * @param integer $idPrest
 * @return Users
 */
public function setIdPrest($idPrest)
{
    $this->idPrest = $idPrest;

    return $this;
}

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

/**
 * Set idEmbaucher
 *
 * @param integer $idEmbaucher
 * @return Users
 */
public function setIdEmbaucher($idEmbaucher)
{
    $this->idEmbaucher = $idEmbaucher;

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set provider
 *
 * @param Providers $provider
 * @return Users
 */
public function setProvider($provider = null)
{
    $this->provider = $provider;

    return $this;
}

/**
 * Get provider
 *
 * @return Providers 
 */
public function getProvider()
{
    return $this->provider;
}



}

in relation with class provider on OneToOne (inheriting)

class Providers
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;


/**
 * @ORM\OneToOne(targetEntity="Users", inversedBy="provider")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $userId;

/**
 * @ORM\OneToOne(targetEntity="Advertisement", mappedBy="provider")
 */
protected $advertisement;

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

/** * Set userId * * @param \leBonCoinDesServices\MainBundle\Entity\Users $userId * @return Providers */ public function setUserId($userId = null) { $this->userId = $userId;

    return $this;
}

/**
 * Get userId
 *
 * @return \leBonCoinDesServices\MainBundle\Entity\Users 
 */
public function getUserId()
{
    return $this->userId;
}

}

I just want to get the fields user_id created from the relationship for be able to when I create a user get the id of this one and give it to the user_id field.

And when I test my controller to create a user and a provider :

public function newUserAction()
{

    $user = new Users();
    $user->setUsername("gokusan");
    $user->setPassword("test");
    $user->setType("p");
    $user->setEmail("[email protected]");
    $user->setFirstName("KHALIL");
    $user->setLastName("Ahmed");
    $user->setTel("0142021148");
    $user->setUrlImg("img/test/");
    $user->setCompany("Push&Pull");

    $this->getDoctrine()->getEntityManager()->persist($user);
    $this->getDoctrine()->getEntityManager()->flush();

    $provider = new Providers();
    $id = $user->getId();

    $provider->setUserId($id);

    $this->getDoctrine()->getEntityManager()->persist($provider);
    $this->getDoctrine()->getEntityManager()->flush();

    die("User Added");
}

that puts me error :

Warning: spl_object_hash() expects parameter 1 to be object, integer given in F:\Programs Files\Program Files\wamp\www\leboncoindesservices\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1367

1

1 Answers

0
votes

Your Providers::setUserId($userId) expects $userId to be of type \leBonCoinDesServices\MainBundle\Entity\Users and not of type integer. So you should be able to pass your Users object to the $provider like

$user = new Users();
...

$provder = new Providers();
...
$provider->setUserId($user);