Ожидаемое значение типа «UserBundle \ Entity \ User» для поля ассоциации «AppBundle \ Entity \ contacts # $ User», вместо этого получено «integer» в symfony - PullRequest
0 голосов
/ 27 апреля 2018

у кого-то была такая же проблема? эта ошибка появляется, когда я хочу добавить новый контакт

Ожидаемое значение типа «UserBundle \ Entity \ User» для поля ассоциации "AppBundle \ Entity \ contacts # $ User", вместо этого получил "integer"

контактное лицо

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);
}
}

отношение к контакту yml:

    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(),
            ));
        }

Ответы [ 2 ]

0 голосов
/ 27 апреля 2018

В дополнение к Пиотреку Заторскису ответят: В вашем контактном лице вместо

/**
 * @var integer
 */
private $User;

должно быть

/**
 * @var User
 */
private $User;
0 голосов
/ 27 апреля 2018

изменение

$userses = $this->get("security.token_storage")->getToken()->getUser()->getid()

до

$userses = $this->get("security.token_storage")->getToken()->getUser()
...