有人有同样的问题吗? 当我想添加新联系人
时出现此错误类型" UserBundle \ Entity \ User"的预期值用于协会领域 " AppBundle \ Entity \ contacts#$ User",得到"整数"代替
class contacts
{
/**
* @var int
*/
private $id;
/**
* @var string
* @Assert\NotBlank()
* )
* @Assert\Regex(
* pattern="/\d/",
* match=false,
* message="Your first name cannot contain a number"
* )
*/
private $firstName;
/**
* @var string
* @Assert\NotBlank()
* @Assert\type(
* type="string"
* )
* @Assert\Regex(
* pattern="/\d/",
* match=false,
* message="Your last name cannot contain a number"
* )
*/
private $lastName;
/**
* @var string
* @Assert\Regex(
* pattern="/\d/",
* match=true,
* message="Your phone number cannot contain a character"
* )
* @Assert\Regex(
* pattern="/\d{11}/",
* match=true,
* message="Your phone number should have 11 digits no more no less :)"
* )
*/
private $phoneNumber;
/**
* @var string
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
*/
private $emailAddress;
/**
* @var integer
*/
private $User;
/**
* @return mixed
*/
public function getUser()
{
return $this->User;
}
/**
* @param mixed $user
*/
public function setUser($user)
{
$this->User = $user;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return contacts
*/
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 contacts
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set phoneNumber
*
* @param string $phoneNumber
*
* @return contacts
*/
public function setPhoneNumber($phoneNumber)
{
$this->phoneNumber = $phoneNumber;
return $this;
}
/**
* Get phoneNumber
*
* @return string
*/
public function getPhoneNumber()
{
return $this->phoneNumber;
}
/**
* Set emailAddress
*
* @param string $emailAddress
*
* @return string
*/
public function setEmailAddress($emailAddress)
{
$this->emailAddress = $emailAddress;
return $this;
}
/**
* Get emailAddress
*
* @return string
*/
public function getEmailAddress()
{
return $this->emailAddress;
}
}
/**
* User
*/
class User implements UserInterface , \Serializable
{
/**
* @var int
*/
private $id;
/**
* @var string
* * @Assert\NotBlank()
* * )
* * @Assert\Regex(
* pattern="/\d/",
* match=false,
* message="Your first name cannot contain a number"
* )
*/
private $firstName;
/**
* @var string
* * @Assert\NotBlank()
* @Assert\type(
* type="string"
* )
* * @Assert\Regex(
* pattern="/\d/",
* match=false,
* message="Your last name cannot contain a number"
* )
*/
private $lastName;
/**
* @var string
* * @var string
* * @Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
*/
public $email;
/**
* @var string
*/
private $password;
/**
* @var string
*/
private $Role;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return User
*/
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 User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
public function setRole($Role = null)
{
$this->Role = $Role;
return $this;
}
/**
* Get Role
*
* @return string
*/
public function getRole()
{
return $this->Role;
}
public function getRoles()
{
return [ $this->getRole()];
}
public function getSalt()
{
return null;
}
public function getUsername()
{
return $this->email;
}
public function eraseCredentials()
{
return null;
}
public function serialize()
{
return serialize(array(
$this->id,
$this->email,
$this->password,
));
}
public function unserialize($serialized)
{
list (
$this->id,
$this->email,
$this->password,
) = unserialize($serialized);
}
}
manyToOne:
User:
targetEntity: UserBundle\Entity\User
inversedBy: contacts
joinColumn:
name: user_Id
nullable: true
referencedColumnName: id
uniqueConstraints:
idx:
columns: phone_number,user_Id
这是我的控制者:
public function addAction(Request $request)
{
$contact = new contacts();
$userses = $this->get("security.token_storage")->getToken()
->getUser()->getid();
$form = $this->createForm(addForm::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$data->setUser($userses);
$em = $this->getDoctrine()->getManager();
$em->persist($data);
$em->flush();
return $this->render('/add/index.html.twig', array(
'form' => $form->createView(),
));
} else return $this->render('/add/index.html.twig', array(
'form' => $form->createView(),
));
}
答案 0 :(得分:2)
更改
$userses = $this->get("security.token_storage")->getToken()->getUser()->getid()
到
$userses = $this->get("security.token_storage")->getToken()->getUser()
答案 1 :(得分:1)
除了Piotrek Zatorskis之外的答案: 在您的联系人实体中,而不是
/**
* @var integer
*/
private $User;
应该有
/**
* @var User
*/
private $User;