简体   繁体   中英

Argument 1 passed to?

I tries to submit form in symfony2.1 but i got following error, i create form student registration and try to submit it, i reviewed may forums for this but can't got any proper solution.

Error:Catchable Fatal Error: Argument 1 passed to
Frontend\EntityBundle\Entity\StudentRegistration::setIdCountry()
must be an instance of Frontend\EntityBundle\Entity\MasterCountry, string given,
called in C:\wamp\www\careerguide\src\Frontend\HomeBundle\Controller\RegistrationController.php
on line 41 and defined in C:\wamp\www\careerguide\src\Frontend\EntityBundle\Entity\StudentRegistration.php line 1253 

In controller i have:

$student_account = new \Frontend\EntityBundle\Entity\StudentRegistration();
$params = $request->get('student_registration');
$student_account->setIdCountry($params['idCountry']);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($student_account);
$em->flush();

Entity class:

/**
 * @var MasterCountry
 *
 * @ORM\ManyToOne(targetEntity="MasterCountry")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id_country", referencedColumnName="id_country")
 * })
 */
private $idCountry;

Please suggest me what i do to resolved this error?

When you set up a Many to one relationship using doctrine, the property holding this relationship IS an object of the related entity, not an id. It is saved in the database as an id, but Doctrine will create the full object when you fetch it and will convert the object to an id when you persist it. So, to reflect this, the property should not be called $idCountry, but $country instead (this is not mandatory, you can call it however you want but this makes everything more clear). The setter should then be setCountry() and it should accept a MasterCountry object.

So, when you receive the country id from the form, you should convert this to a MasterCountry object (by fetching it from the database), set this object in studentRegistration and then persist it. Something like:

$student_account = new \Frontend\EntityBundle\Entity\StudentRegistration();
$params = $request->get('student_registration');
$country = $this->getDoctrine()->getRepository('AcmeStoreBundle:MasterCountry')
        ->find($params['idCountry']);
$student_account->setCountry($country);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($student_account);
$em->flush();

Though this should work, this is not the Symfony way to handle forms. You should be creating a Form object and then bind and validate it. You should not then have to deal with the request parameters, etc... I suggest you read carefully this chapter of the Symfony docs:

http://symfony.com/doc/current/book/forms.html

I think the problem is that $params are not the request params :

$params = $request->get('student_registration'); // looks like a String http param value
$student_account->setIdCountry($params['idCountry']); //what could be $params['idCountry'] 

You should probably want

$studentRegistrationId = $request->get('student_registration');
$studentRegistration = getStudentFromId( $studentRegistrationId); // I don't know how you retrieve the $studentRegistration object
$idCountry = $request->get('idCountry');
$student_account->setIdCountry($idCountry);

I'm sure it's not exactly that, but for me, it makes more sense.

The problem is that by setting the relationship using doctrine, you declare that "$idCountry" is the Country object.

If you set the idCountry itself it will work as a shortcut (Doctrine allows you to set the id instead of the object), though by convention, the property should be named $country, and not $idCountry, because the idea is to abstract you of the existance of ids while you're coding by just refering to objects.

This error is shown because there's probably a type hint forcing it to be the object, so look for something like this in the StudentRegistration class:

public function setIdCountry(MasterCountry $idCountry)

Or something similar, you want to remove the type-hinting (MasterCountry before $idCountry) if you want to be able to set an id. If you don't want to touch that, then you might need to retrieve the country object and use that instead of just the id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM