Reputation: 4061
I have entity SourceElanceProfileImport, and I create this entity in action and set some data and when I flush have error:
Catchable Fatal Error: Object of class Proxies\__CG__\Artel\ProfileBundle\Entity\Teams could not be converted to string
and question, why if I find teams like this I have Proxies object
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
I tra hard code write id and have nor entity Teams
$id = '2';
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
// $team entity Teams, not Proxies
what’s happened not right in this action?
action:
public function elanceProfileAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$url = $request->get('url');
$email = $request->get('email');
$firstName = $request->get('firstName');
$user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
$id = $user[0]->getTeams()->getId();
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
if (!empty($user)) {
$elance_import = new SourceElanceProfileImport();
$hepler = $this->container->get('artel.profile.additional_function');
$pass = $hepler->generatePassword();
$elance_import
->setEmail($email)
->setSecurityHash(sha1($pass))
->setElanceUrl($url)
->setTeamId($team)
;
$em->persist($elance_import);
$em->flush();
and entity:
class SourceElanceProfileImport
{
/**
* @var \Teams
*
* @ORM\ManyToOne(targetEntity="Teams")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
* })
*/
private $teamId;
/**
* @var integer
*
* @ORM\Column(name="team_id", type="integer")
*/
public $team;
when I add __toString to entity Teams I have:
public function __toString()
{
return $this->company;
}
Error in one or more bulk request actions:
index: /aog/sourceelanceprofileimport/5 caused MapperParsingException[failed to parse [team_id]]; nested: NumberFormatException[For input string: "Gutkowski LLC"];
why in another entity work fine, what’s wrong I don’t know ((
update I solved but I think this is solved not fine and that’s one I don’t delete this question I add in User entity:
private function _load()
{
// lazy loading code
}
/**
* Get teams
*
* @return \Artel\ProfileBundle\Entity\Teams
*/
public function getLoadTeams()
{
$this->_load();
return parent::getId();
}
and in my action
$user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
$id = $user[0]->getLoadTeams();
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
than I have object Teams in variable $team, BUT when I flush I still have error:
Catchable Fatal Error: Object of class Artel\ProfileBundle\Entity\Teams could not be converted to string
Upvotes: 3
Views: 2468
Reputation: 470
First, you will have proxies every time an entity is not completely populated. Proxies are used to return partial entities. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/partial-objects.html
Looking your code I noticed this:
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
But you can do the same in this way:
$team = $em->find('ArtelProfileBundle:Teams', $id); // EntityManager::find has 2 arguments: entity and id.
Underneath you do this:
$user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
Did you implement some custom code on getCompanyByEmail? If not, you can use Doctrine's methods:
$user = $em->getRepository('ArtelProfileBundle:Users')->findOneByEmail($request->get('email'));
Those are magic methods: findOneBy or findBy. One returns a single object or NULL, the other one returns an array with results. You can also do this:
$user = $em->getRepository('ArtelProfileBundle:Users')->findOneBy(['email' => $request->get('email')]); // you can add more key/value filters on the array
Now just to be sure, did you implement APC as bytecode cache? Because when I make changes on my entities I usually need to flush APC (restarting the webserver) to force the cache to regenerate it. If misterious errors occur maybe APC should be restarted.
Now, regarding the flush error, did you implement a __toString method on any entity? Probably it's not returning a string.
Upvotes: 0
Reputation: 4061
I solved, but problem it was in persist entity and in config fos_elastic I still have Proxies Teams entity but in action
$user = $em->getRepository('ArtelProfileBundle:Users')->getCompanyByEmail($request->get('email'));
$id = $user[0]->getTeams()->getId();
$team = $em->getRepository('ArtelProfileBundle:Teams')->findOneById($id);
if (!empty($user)) {
$elance_import = new SourceElanceProfileImport();
$hepler = $this->container->get('artel.profile.additional_function');
$pass = $hepler->generatePassword();
$elance_import
->setEmail($email)
->setSecurityHash(sha1($pass))
->setElanceUrl($url);
$em->persist($elance_import);
$elance_import->setTeamId($team);
$em->flush();
and flush fine entity create and have id Teams (Proxies object Teams) and teamId this is relationship with entity Teams and in fos_elastica need write like this:
sourceelanceprofileimport:
mappings:
id:
type: integer
elance_url:
type: string
full_name:
type: string
status:
type: string
created:
type: date
approved_at:
type: date
imported_at:
type: date
team_id:
type: "nested"
properties:
id: ~
persistence:
# the driver can be orm, mongodb or propel
# listener and finder are not supported by
# propel and should be removed
driver: orm
model: Artel\ProfileBundle\Entity\SourceElanceProfileImport
provider: ~
listener: ~
finder: ~
and I have entity in my DB and Elastic DB
Upvotes: 0